Dynamically Search for Database

I have a vb6 application that uses a file .dsn to locate the database on the server:

DRIVER=Driver do Microsoft Access (*.mdb)
FIL=MS Access

It has been requested of me to change this or use a different method for locating the database. The reason for the request is because the user wants to be able to avoid errors being thrown if the server changes, etc. The person requesting the change wants to eliminate the "hard coding" so the application won't be dependant upon this directory path. Is it possible to create something in an install package to where it points to the location of the database? Is this possible?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

you don't need to use an ODBC
Using ADO you can use a connection string that can be stored in an INI or CFG file for retrieval by the program.
Then, nothing needs to be hard-coded, but the INI file does need to be maintainable
Gary2397Author Commented:
This sounds like what I am looking for. Will this allow the user to specify the location of the database on installation? I don't have any experience with INI files, is there a good tutorial anywhere.
Personally, INI is the "old way". It works fine in simple cases, but for a more extensible solution, XML is better.
You can try this link:


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

This link has a more VB6-centric approach.

...and if you just want to use an INI, then you'll want to use these Win32 APIs (link to MSDN info included):

'Windows API/Global Declarations for :.INI read/write routines

Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Finally, to extend on the original comment by g_johnson, here's some info on ADO in case you need it. Incidentally, I apologize for the multi-post response. I should have composed my thoughts better before dumping this all on you.



Hopefully that should get you oriented in the right direction.
Is the connection string stored on the local machine, as opposed to a network resource?  If so, you could easily store to the registry with:

    SaveSetting("MyApp", "MyKey", "ConnectionString", "{Provider=...}"

    szConnStr = GetSetting("MyApp", "MyKey", "ConnectionString", "this is the default value")


Naveen SwamyYash Infinite Solutions Private LimitedCommented:
basically u need to have a external file or settings from where you need to load the database, this would be a very good option, if given to the user.

Ini is an old aproach
Registry is good but should be used with caution
XML is a good option, thinking future (its pretty easy to, i learnt basics of xml in 2 hours)
Alternatively, why don't you host your DSN file remotely?  That way, the client app uses the setting to find the DSN and then connects to it; as long as you keep the DSN updated, you can change the database location as much as you want, without affecting the client app and has the advantage that the user does not need to enter the connection string manually (less chance of mistakes) and does not need to know a database username/password.

You could store the DSN location in the registry or a settings file an even allow the user to browse to the DSN using the Common Dialog control.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.