Don't Want To In Visual Basic Hard Code Complete Connection Path To Database

  • Thread starter Thread starter clusardi2k
  • Start date Start date
C

clusardi2k

When I create a connection to a database do I have to use the complete
path to the database. E.G.:

ACCESS.C:\Documents and
Settings\Christopher\Desktop\TEST\Test_database.mdb

I developed the VB application on my PC, and would like to give it to
others to use. So, am I forced to go to other PC's and create the
connection for them? Can I use something like the shorter Unix syntax
such as

"..\..\My_folder\the_access_database"?

Second, how could I automate this process by prompting the user for the
path and using that to create the connection?

Thank you,
Christopher Lusardi
 
On 23 May 2006 06:23:25 -0700, (e-mail address removed) wrote:

¤ When I create a connection to a database do I have to use the complete
¤ path to the database. E.G.:
¤
¤ ACCESS.C:\Documents and
¤ Settings\Christopher\Desktop\TEST\Test_database.mdb
¤
¤ I developed the VB application on my PC, and would like to give it to
¤ others to use. So, am I forced to go to other PC's and create the
¤ connection for them? Can I use something like the shorter Unix syntax
¤ such as
¤
¤ "..\..\My_folder\the_access_database"?
¤
¤ Second, how could I automate this process by prompting the user for the
¤ path and using that to create the connection?

If the database is relative to your application path you could use:

Application.StartupPath

Otherwise you can prompt the user with a Dialog Box:

http://msdn2.microsoft.com/en-us/library/system.windows.forms.openfiledialog.aspx


Paul
~~~~
Microsoft MVP (Visual Basic)
 
Place the database in a folder with your application. And use either
Application.StartupPath or GetExecutingAssembly.
I use it in this manner:
( be sure to include : Imports System.IO and Imports System.Reflection at
the top of your Class)

Then call the Function, mypath like so:

Dim openFile As New OpenFileDialog

openFile.FileName = ""

openFile.InitialDirectory = mypath() + "\Database Files"

openFile.Filter = "Microsoft Access Application (*.mdb)|*.mdb"

Dim res As System.Windows.Forms.DialogResult = openFile.ShowDialog()

If res = System.Windows.Forms.DialogResult.Cancel Then

Return

End If

accessConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source= " + openFile.FileName


Then the rest of your code here............


Private Function mypath() As String

Return _

Path.GetDirectoryName([Assembly].GetExecutingAssembly().Location)

End Function



So far this works fine for me.
james
 
Back
Top