Click here to Skip to main content
15,304,276 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to read an attachment from an Access DB from within infopath.
This is the code that should work according an answer in this community, but it doesn't:

Database DAODataBase;
            Workspace DAOWorkSpace;
            DBEngine DAODBEngine = new DBEngineClass();
            DAOWorkSpace = DAODBEngine.CreateWorkspace("wsInfopath", "Admin", "", WorkspaceTypeEnum.dbUseJet);
            DAODataBase = DAOWorkSpace.OpenDatabase("D:\\_DEV\\Bunker database\\FrameAgreements.accdb", false, false, ";pwd=");
            Recordset rstMain = DAODataBase.OpenRecordset(strSQL, RecordsetTypeEnum.dbOpenTable,RecordsetOptionEnum.dbReadOnly,false);

This is the error I get on the restMain instruction:

System.Runtime.InteropServices.COMException was unhandled by user code
  Message="Invalid argument."
       at Microsoft.Office.Interop.Access.Dao.Database.OpenRecordset(String Name, Object Type, Object Options, Object LockEdit)
       at Combined_form_b.FormCode.CTRL999_7_Clicked(Object sender, ClickedEventArgs e)
       at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent)
       at Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent)

I tried setting different arguments but I keep getting the same error.

I ran out of ideas.
Help is appreciated since I'm stuck here.
Updated 20-Sep-15 9:02am
Dave Kreskowiak 17-Sep-15 10:26am
Why the hell are you using DAO? That technology has been dead for a really long time!

DAO used the Jet database engine, which doesn't work with .accdb files. Those require the ACE database engine, which DAO will not work with. You have to use ADO.NET in order to get at that database, not DAO. That will require a complete rewrite of the code you have to use ADO.NET.
Eddy Sels 17-Sep-15 10:31am
can I read an attachment with ADO? Don't think so
Dave Kreskowiak 17-Sep-15 11:22am
Where did I mention ADO? That's different from ADO.NET.

Again, DAO will NEVER work with a .accdb file. From the code snippet that you posted, this is standard Access database stuff. You should be using the ADO.NET provider for OleDb and the correct connection string for the ACE database engine.

Connection string examples for newer Access databases can be found at
Eddy Sels 17-Sep-15 16:27pm
You're clearly not a fan of DAO, but it is the only way to read attachments from within Access. I thought it would work from outside Acces too. Remember I'm coding in Infopath. I still don't know how to read an Access attachment from outside Access, be it ADO or I know how to connect to Access and read records. I don't know how to read attachments.
Can you answer this?
Dave Kreskowiak 17-Sep-15 17:03pm
DAO has been dead for over 15 years now. That's why there are no fans of it.
Dave Kreskowiak 17-Sep-15 17:11pm
No, it's NOT the only way to read something from Access. I've been telling you but you haven't been listening because you're fixed on DAO.

For example...
Eddy Sels 18-Sep-15 3:41am
Well, you are not listening too. I know how to connect to Access and how to read records. The thing is that with ADO.NET you only get the filename of an attachment and not the actual data. I'm looking for a way to read the actual attachment data.
So if you can't help me its ok, but don't keep sending me information beside the point or information I already know.
Dave Kreskowiak 18-Sep-15 8:07am
Get the filename from the database and the rest is easy. Getting the filename from the database is what's stopping you.

I keep saying this again and again: Your DAO doesn't work with ACCDB (Access 2010 and above) files! There's a little hint in the error message: "jeterr40". That's the JET database driver telling you there is a problem. JET does NOT work with ACCDB files. Those files have to be access with the ACE driver! DAO uses JET, not ACE.

Gustav Brock 18-Sep-15 6:07am
I don't know why Dave believes DAO has been dead for 15 years? It is alive and well and has been and still is the preferred method in Access and VBA from version 1.0 through the upcoming 2016 (excluding one version many years ago).

That corrected, I haven't used DAO from within C#, but a little searching reveals this link which should get you going:

This link is in C++ (and also falsely mentions the death of DAO) - still it could give you some ideas:
Maciej Los 20-Sep-15 15:22pm
To retrive data from ms access database within InfoPath, use DataConnection class[^]

For further information, please see:
Creating an InfoPath Custom Control using C# and .NET[^]

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