Re: Connection Stings

From: Terry Kreft (terry.kreft_at_mps.co.uk)
Date: 01/19/05


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
>
>


Relevant Pages

  • Re: help with changing some code from mdb to adp
    ... At the end of this post is an example of an ADO connection string to a SQL ... Server database, and '' to the UNC path/name of your instance of SQL ... Windows, you can't create a new UDL file directly, but you can create a new, ...
    (microsoft.public.access.adp.sqlserver)
  • Re: Deployment + Vista
    ... The problem is it's connection string is always hard ... Server settings form. ... guess it's a good thing I do not use that for settings. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Connection Stings
    ... > Where strUDLPath contains the path to the UDL file. ... > Function GetConnectas string ... > Dim loCon as ADODB.Connection ... I delcare the connection string in a module as a Public ...
    (microsoft.public.vb.general.discussion)
  • Linked server: MAS 90 URGENT!
    ... Has anyone successfully created a linked server to a MAS90 database? ... tried creating a UDL file and copying the connection string into the linked ...
    (microsoft.public.sqlserver)
  • Re: Server Does Not Exist or Access Denied error
    ... This is the first time I have tried to access remote server - ie it has ... Try to fill in the text boxes to connect to the server. ... open this .udl file in the notepad. ... You will see the connection string here. ...
    (microsoft.public.sqlserver.connect)