Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to write a program that uses CDatabase to connect to a local Access database without setting up a DSN (it works fine when I do use a DSN but I don't want to have to create one for every database). I haven't found anything that works without bringing up a dialog box.

What I have tried:

C++
CDatabase db;
db.OpenEx("ODBC;Driver={Microsoft Access Driver (*.mdb)};Dbq=XXX;Uid=Admin;Pwd=;");

taken from www.connectionstrings.com

and

C++
CDatabase db;
db.OpenEx("ODBC;DSN=working;DBQ=XXX;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;");

taken from db.GetConnect() after successfully opening a database with
C++
db.OpenEx("DSN=YYY");


Note that my database filename XXX contains spaces; I have tried enclosing this in single quotes, likewise the "MS Access" in the latter example but this doesn't help. I have also tried adding "SERVER=localhost;" but that doesn't help either.

Can anyone explain what's wrong or missing, please?
Posted
Comments
CHill60 20-Feb-24 8:56am    
ConnectionStrings.com actually suggests Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\mydatabase.accdb;Uid=Admin;Pwd=;
So try
1. Lose the "ODBC;"
2. use the right driver {Microsoft Access Driver (*.mdb, *.accdb)};
3. Ensure the XXX is the fully pathed name of your MS Access database, including the .accdb
4. If your MS Access database is not password protected then lose the Uid and Pwd parts, they're not required.
5. Consider using the standard security connection Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;Persist Security Info=False;
NigelSHoult 20-Feb-24 16:26pm    
Thanks - that worked (except that my database was actually xxx.mdb rather than xxx.accdb). There was a rather strange warnng message
AppMsg - Warning: ODBC Success With Info, D:\a\_work\1\s\src\vctools\VC7Libs\Ship\ATLMFC\Src\MFC\dbcore.cpp(175) : AppMsg - Driver's SQLSetConnectAttr failed

but that didn't seem to cause a problem.
Dave Kreskowiak 20-Feb-24 10:19am    
DSNs are nothing but a connection string in a text file. You really do not need them at all if you know how to build a connection string yourself.

1 solution

Posting solution from comments to close off the question.

Try using
C#
db.OpenEx("Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=XXX");
Points to note:
- The parameter pair Uid= and Pwd= are only required if the MS Access database is password protected, otherwise omit them entirely
- You need the *.accdb in the Driver because that is its name - even if your database is an .mdb
- the XXX must be the fully pathed name of your database including the extension

Unless you are explicitly using features only available in .mdb files you might want to consider updating to the .accdb format. More detail on the differences can be found at Which Access file format should I use? - Microsoft Support[^]
 
Share this answer
 

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