Search code examples
vb.netms-accessdatetimeoledbdao

Code gives error for accdb files


The below code adds a column of datatype "Date/Time" with format "Long Time" to an access table of .mdb file. But if i replace the mdb file with accdb then adding column won't work. So i want alternative code which can add column of datatype "Date/Time" with format "Long Time".

The code which i use

Dim db As Database
Dim tdf As TableDef
Dim fld As Field

Dim JetEngine As New dao.DBEngine
Dim p 
db = JetEngine.OpenDatabase("D:\Employee.accdb")

tdf = db.TableDefs("detail")
fld = tdf.CreateField()
With fld
     .Name = "MyDateTime"
     .Type = 8
      .Size = 8
End With

tdf.Fields.Append(fld)
p = fld.CreateProperty("Format", 10, "Long Time")
fld.Properties.Append(p)

I get error like "Unrecognized database format 'D:\Employee.accdb'." for the code

db = JetEngine.OpenDatabase("D:\Employee.accdb")

Solution

  • Check the References for your project. If you are using the reference

    Microsoft DAO 3.6 Object Library

    (which appears in the References list as "DAO") then your code will work for .mdb files but not for .accdb files. You can support both file types without major modifications to your code if you simply remove that reference and replace it with

    Microsoft Office 14.0 Access Database Engine Object Library

    (which appears in the References list as "Microsoft.Office.Interop.Access.Dao").