Tuesday, 30 July 2013


With thanks to Paul Clement we were able to create this tip how to create from scratch a Accdb database file in code. Be aware there has to be set a reference (extensions) to the Access Interop
'Set a reference to Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Access.Dao
Module Exercise
    Sub Main()
        Try
            Dim AccessDatabaseEngine As New Microsoft.Office.Interop.Access.Dao.DBEngine
            Dim AccessDatabase As Microsoft.Office.Interop.Access.Dao.Database
            AccessDatabase = AccessDatabaseEngine.CreateDatabase("C:\Test\NewDatabase.accdb"LanguageConstants.dbLangGeneral, DatabaseTypeEnum.dbVersion120)
            AccessDatabase.Close()
        Catch ex As Exception
            Console.Write(ex.Message)
            Console.ReadLine()
        End Try
       
       
        Using conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & _
            "  Data Source=C:\Test\NewDatabase.accdb;Persist Security Info=False;")
            Using cmd As New OleDb.OleDbCommand("CREATE TABLE persons ( " & _
              "AutoId int identity ," & _
              "Id int NOT NULL," & _
              "Name NVarchar(50)," & _
                "BirthDate datetime," & _
               "IdCountry int," & _
                  "CONSTRAINT [pk_AutoId] PRIMARY KEY (AutoId)) ", conn)
                conn.Open()
                Try
                    cmd.ExecuteNonQuery()
                Catch ex As Exception
                    Console.Write(ex.Message)
                    Console.ReadKey()
                End Try
            End Using
        End Using
    End Sub
End Module


0 comments:

Post a Comment