Monday, July 30, 2018

Access How to create an Accdb databasefile with columns

Access How to create an Accdb databasefile with columns



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:TestNewDatabase.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:TestNewDatabase.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





go to link download
download
alternative link download

No comments:

Post a Comment