access vb.net and app path (1 Viewer)

jaytheguru

Registered User.
Local time
Today, 15:42
Joined
Nov 13, 2007
Messages
43
Dear all, as always I am back with yet again a simple question.

I have developed an application in vb.net which has VB interface as front end and access 2003 as backend. The application path which I setup initially is :

Code:
 Dim MyConn As ADODB.Connection
            MyConn = New ADODB.Connection
            MyConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= C:\Lensesdb.mdb;"
            MyConn.Open()

here the access file is refering to C drive however, I want it to be within
Application folder so that when I create the application, the file should be installed and ran directly from the application directory and not from C drive.

I have added the file into the compilation and it created the application (exe) fine however when I install the program and run it, it asks for the C:\Lensesdb.mdb file (says its missing) whereas the application file is within application folder.

I have found few ways online:

1st Example

Code:
    Public Function App_Path() As String
        Return System.AppDomain.CurrentDomain.BaseDirectory()
    End Function

2nd Example

Code:
Private Function GetAppPath() As String
Dim i As Integer
Dim strAppPath As String
strAppPath = System.Reflection.Assembly.GetExecutingAssembly.Location()
i = strAppPath.Length - 1
Do Until strAppPath.Substring(i, 1) = "\"
i = i - 1
Loop
strAppPath = strAppPath.Substring(0, i)
Return strAppPath
End Function

I did bring them into my coding but I don't know how to refer to my application path when I type

Code:
MyConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= C:\Lensesdb.mdb;"

how can I declare the the path so that I don't have to manually put it into the C drive of destination pc each time I install the application.

I hope I made some sense here:D

Regards

J
 

Banana

split with a cherry atop.
Local time
Today, 07:42
Joined
Sep 1, 2005
Messages
6,318
You'd need to make the path to the Access file a variable that can be added to the Connection String, and have a sanity check before a connection is opened and asks the users to locate the file if it isn't where the application expected it to be.
 

jaytheguru

Registered User.
Local time
Today, 15:42
Joined
Nov 13, 2007
Messages
43
Thanks Banana, however, I do not wish an end user to locate the file as they have no clue about computers (0, nada).

All I want is to setup a path in my code so that when an application is installed onto their machines, the application should pick up the source/destination file automatically from the application folder.

Currently when I deploy the application the file "Lensesdb.mdb" deploys onto the application folder [that is under Program files\application\lensesapp]

however, when I run the application it tells me the file Lensesdb.mdb is not located in C: drive. I know why it is saying this (that is because I ref it in the code to look at C: drive)

Can you please help me idenfiy how I should go about it (programmatically). maybe using the examples I provided above?

many thanks

Regards

J
 

Banana

split with a cherry atop.
Local time
Today, 07:42
Joined
Sep 1, 2005
Messages
6,318
Two ways I can think of:

1) Use a text file to give the path. When you install the application, modify the text file to give out the path to the Access database so the application reads it and use that.

2) Statically link the Access, give it a path within the application's path. There should be options in VS to tell that you want a particular file built with the whole program, and that it should be statically linked, so when it's built, it'll come with a lib folder with the mdb in it.
 

jaytheguru

Registered User.
Local time
Today, 15:42
Joined
Nov 13, 2007
Messages
43
Yes! it did ask me if I wanted to add the mdb file into the project but at that time I said No. Now I don't know how to add it back into the project.

Any ideas will help alot!

Regards

J
 

Banana

split with a cherry atop.
Local time
Today, 07:42
Joined
Sep 1, 2005
Messages
6,318
Don't have VS in front of me, but if you go to Project -> Add files, then select the mdb file. It should then be in the Solution Explorer. Select that mdb and make the properties has a line saying that it will be built along with the rest of the project.

HTH.
 

jaytheguru

Registered User.
Local time
Today, 15:42
Joined
Nov 13, 2007
Messages
43
Morning Banana,

There is no add files option under Project however there are other options like Add windows form, user control, module, class etc.

I did manually added the mdb file under bin -->debug folder and used

Code:
MyConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & Application.StartupPath & "\Lensesdb.mdb;"

however this still doesn't work. I mean this doesnt work on client pc and I still have to put the mdb file in C: drive.

I am wondering what could make it not work even though I did mention the startup path which should be within the application.

any more ideas which might work will be welcomed

Regards

J
 

Users who are viewing this thread

Top Bottom