Click here to Skip to main content
15,794,848 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:

C#
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
  HelpLink="jeterr40.chm#5003001"
  Message="Invalid argument."
  Source="DAO.Database"
  ErrorCode=-2146825287
  StackTrace:
       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.
Posted
Updated 20-Sep-15 10:02am
v2
Comments
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 http://www.connectionstrings.com/access-2010/.
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 ADO.net. 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.

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