Click here to Skip to main content
15,746,107 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a VB database project (Access). I tried source=Application.Startuppath & "\filename.accdb" . It does not work. And |Data Directory|\filename.accdb doesn't work either. Any suggestions?

What I have tried:

I tried source=Application.Startuppath & "\filename.accdb" . It does not work. And |Data Directory|\filename.accdb doesn't work either.
Updated 18-Apr-23 11:10am
Maciej Los 18-Apr-23 16:59pm    
Have you tried to check out what Application.Startuppath returns?
BobbyStrain 18-Apr-23 19:25pm    
Thank you. I shall do that.

1 solution

If you put the database anywhere under "Program Files" or "Program Files (x86)", you will NOT be able to use the database. These folder locations are ReadOnly to normal users, and hence you will not be able to update the database.

You can keep an original copy of the database (preferably in a state that defines the entire database but does not have data in it), then copy that file to a more appropriate place where the user has read/write permissions, like in a folder under %PROGRAMDATA% or a folder under %APPDATA% (the users roaming profile) or %LOCALAPPDATA% (the users local profile).

Which folder you pick depends on your application. Will all users on the same machine use the same database? If so, use a folder under %PROGRAMDATA%. If the data is going to be unique to each user on the same machine but does not follow the user around from machine to machine, use a folder under %LOCALAPPDATA%. If the data has to follow each user around to every machine they use, then %APPDATA% would be more appropriate. If using roaming profiles, you would need to have each machine in an Active Directory environment and network locations setup as appropriate for romain profiles and data.

In most cases, you would use a folder under %PROGRAMDATA%.

When building the connection string to the database, you could just use string manipulation to insert the path to the database file:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=%PROGRAMDATA%\myAppFolder\myAccessFile.accdb;Persist Security Info=False;
Share this answer
BobbyStrain 18-Apr-23 19:26pm    
Thank you, Dave. You are always helpful.
BobbyStrain 19-Apr-23 15:44pm    
Dave, I added the database to the project and that created a connection string with |Data Directory| as the location. So, now the location is resolved at run time. And I don't need to specify a folder path.
Dave Kreskowiak 19-Apr-23 15:46pm    
Whether or not that's going to work for you depends on where the Access database ends up and what you use to build an installer for the app. You may not end up with the database in a desirable place.
BobbyStrain 19-Apr-23 16:07pm    
I don't use an installer for this simple project. I just distribute the folder in a zip file. Along with instructions to unzip it wherever they wish. Except for the Program files.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900