Avatar billede hugopedersen Nybegynder
30. juni 2004 - 17:16 Der er 7 kommentarer og
1 løsning

Skifte connection til SQL server i kode

Findes der en metode til at skifte connection til en SQL server i en Access .ADP applikation ?
Det er den connection man refererer til med CurrentProject.Connection jeg er interesseret i.  Jeg ville gerne have det lavet så jeg f.eks. kan skifte mellem test og drift data i min applikation.
Det må godt være sådan at skiftet sker fra en lille formular hvor man kan angive server, database, bruger og password.  Når man har skiftet db og evt. lukker applikationen ned, skal de nye indstillinger selvfølgelig huskes til næste opstart af applikationen.

Er der nogen der har set sådan funktionalitet et sted på nettet, er jeg meget interesseret i at høre om det.
Avatar billede terry Ekspert
30. juni 2004 - 19:03 #1
Hi Hugo
I can give you some information as to how it can be done but I cant give you the code. What we use is a bit overkill and actually fills quite a bit.

What is important is this.

CurrentProject.BaseConnectionString contains the connection string which you need to alter. The string contains a substring something like this.

DATA SOURCE=ThisIsYourOldServer

You need to replace this sub string with

DATA SOURCE=ThisIsYourNewServer

Then you need to close the current connection using

CurrentProject.CloseConnection

Now open the connection again with the new BaseConnectionString

CurrentProject.OpenConnection ThisIsTheNewBaseConnectionString


Hope that all makes sence!
Avatar billede hugopedersen Nybegynder
01. juli 2004 - 10:25 #2
OK Terry - one step forward.
Now I just need the form with list of servers and db's on them.
Avatar billede terry Ekspert
01. juli 2004 - 11:20 #3
we do it when the application starts up using autoexec macro to call the code. We then look in the registry to see which server to use.
We have chosen this way so that users dont have access to it. For developers it sjust a case of double clicking a .reg file containing the connection information. Then running autoexec again to read the registry and change the connection.
Avatar billede hugopedersen Nybegynder
01. juli 2004 - 11:33 #4
OK - but what if the users wants to test something?
I have a lot of users that would like to have a sandbox to play around with.
I could make it so that only administrators have access to setup the 2 connections neede and then give the users access to a couple of menuitems that allows them to switch.
Avatar billede terry Ekspert
01. juli 2004 - 12:47 #5
So you could just add a simple form with a combo box containing the possible servers. What you have to be carefull of is that the users forgets to change back to production dB, and then YOU have a problem later when they complain that the data they created earlier cant be found (becuase its in the test dB :o) )
Avatar billede hugopedersen Nybegynder
01. juli 2004 - 13:08 #6
Terry> if I could find some code for retrieving the list of servers and the list of db's on the selected one then I would be almost there.

Do you say that users are stupid ?  :-)
I have a plan regarding this issue. I have a hidden form already and I plan to put some timerfunction on this that displays a popup every now and then when the user is working on the test db. Then they can't say that they haven't been warned :-)
Avatar billede terry Ekspert
01. juli 2004 - 13:19 #7
Sounds like a good idea.
I would have thought that the database's had the same names in test and prod. But if you need to alter this too then you need to replace

INITIAL CATALOG=......
Avatar billede hugopedersen Nybegynder
10. september 2004 - 11:09 #8
Haven't got a finished app. yet, but I think it is time to close and since Terry was the only one answering........................
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Dyk ned i databasernes verden på et af vores praksisnære Access-kurser

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester