Re: Connection Stings
From: Terry Kreft (terry.kreft_at_mps.co.uk)
Date: 01/19/05
- Next message: Tony Proctor: "Re: Copying string to byte array"
- Previous message: J French: "Re: Question on ActiveX Controls"
- In reply to: Vince: "Connection Stings"
- Next in thread: Vince: "Re: Connection Stings"
- Reply: Vince: "Re: Connection Stings"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 19 Jan 2005 10:36:39 -0000
For ADO look at .UDL files.
In code it then becomes as simple as
...
.ConnectionString = "File Name=" & strUDLPath
...
Where strUDLPath contains the path to the UDL file.
To change where you are connecting to just double click on the UDL file and
change the settings.
As a general piece of advice, always consider carefully when using
constants, whether formally declared or in-line, if the constant could be
changed by external requirements then don't use it think of a way in which
to store the value externally and retrieve it, in this way it is easily
changed.
One way to manage this is to use functions in order to hide the fact that
you are using a const, then if at a later date you find that you need to get
the value in a different way you just need to change the function and the
rest of your code works.
To use your connection string as an example, I would have worked it in this
way.
Function GetConnect() as string
Const CONNECT_STRING = "Provider= ..."
GetConnect = CONNECT_STRING
End Function
Then in code you would do...
Dim loCon as ADODB.Connection
set loCon = New ADODB.Connection
With locon
.ConnectionString = GetConnect
...
End With
If I then decided to use the .UDL file all I would need to change would be
the GetConnect function
Function GetConnect() as string
Dim strFileName as String
Const UDL_FILE_NAME = "/MyUDL.udl"
strFileName = App.Path & UDL_FILE_NAME
GetConnect = "File Name=" & strFileName
End Function
The rest of the code then just works
--
Terry Kreft
MVP Microsoft Access
"Vince" <sdsad@fsd.com> wrote in message
news:uqGc30e$EHA.1084@tk2msftngp13.phx.gbl...
> This could be easy, but I am a little confused about it.
>
> In my software, I delcare the connection string in a module as a Public
> Constant
>
> Public constant ConnectionString="Data Source=SQLSERVER1;........."
>
> Now, there are three more SQL servers (mainly for testing purposes). I
would
> like to be able to write a INI file or something so that the server name
can
> be changed easily without changing the code / recompiling. Like:
>
> [Connection String]
> Server=SQLSERVER2
>
> and when the program loads, I could read the INI and change the connection
> string. Of course, I know I cannot do something like Public Constant
> ConnectionString="Data Source=" & GetINISetting("Connection
> String","Server") because this is only valid in a procedure and not in the
> General Declaration area. So, I was thinking I would call a sub routine
> during the form load event and then use the connection string as a global
> string rather than a global constant. I do not want a box at the beginning
> prompting the user to choose the server because it's a bit of
inconvenience
> and 90% of them use the real server.
>
> My question - I get the feeling that there is a better way than my INI
> method. Is this approach right? Is there a better and/or easier method?
>
> Thanks a lot.
> Vince
>
>
- Next message: Tony Proctor: "Re: Copying string to byte array"
- Previous message: J French: "Re: Question on ActiveX Controls"
- In reply to: Vince: "Connection Stings"
- Next in thread: Vince: "Re: Connection Stings"
- Reply: Vince: "Re: Connection Stings"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|