Linking forms to database not working.

MagicAardvark

Member
Joined
Sep 10, 2010
Messages
11
Programming Experience
Beginner
I currently have an application that links to an access database. The problem is I wt to run it from a cd or flash drive and the directory cannot find the file on other machines due to the spacific URL.

My code looks like
cnData.ConnectionString = " Provider=Microsoft.ACE.OLEDB.12.0;Data Source=../Data/PhoneBook.accdb;Persist Security Info=False;"
strSQL = " SELECT [LastName]+', '+[FirstName]+' '+[MiddleName] AS FullName, TblContact.* FROM TblContact ORDER BY [LastName]+', '+[FirstName]+' '+[MiddleName];"
daClient.SelectCommand = New OleDbCommand(strSQL, cnData)
daClient.Fill(dsClient, "TblContact")
Me.dtClient = dsClient.Tables("TblContact")

However this code looks specifically through the C drive when I build a .exe file.

Ive never published a program before so I am very new to this.
 
Very soon I would have been able to direct you to a blog post I'm writing that is very relevant to your situation, but it's not quite finished so I'll just give you the broad strokes. Basically, you're not managing your data file as you really should be. You should be adding your data file to your project and then, when the project gets built, having the data file copied to the output folder. That happens by default, so you don't have to do anything extra. It's the copy that you connect to, so it is always in or under the output folder. You then simply deploy the contents of the output folder and your data file is always in the same location relative to the executable, either in the same folder or a subfolder. In your connection string, you use |DataDirectory| to represent the program folder and then append any subfolders and the file name to that.
 
Back
Top