Search code examples
vb.netvbaddladox

create table in access database using Adox in vb


I'm using a Windows form application in Visual Basic 2012 to create a new Microsoft Access database using .ADOX. I can create the database but can't create a table in the database.

My code is :

Imports ADOX
Imports System.Data.OleDb
Public Class Form1
Dim mycommand As OleDbCommand
Dim myconnection As OleDbConnection
Dim myReader As OleDbDataReader
Dim str As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim cat As New Catalog()
    Dim tablename As String = "Users"
    cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\important\DDL.mdb;Jet OLEDB:Engine Type=5")
    cat = Nothing
    myconnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\important\DDL.mdb ")
    myconnection.Open()
    str = "CREATE TABLE [ " & tablename & "] ([Username] varchar(50)), ([Password] varchar(50)), ([E-mail] varchar(75))"
    mycommand = New OleDb.OleDbCommand(str, myconnection)
    mycommand.ExecuteNonQuery()
    MsgBox("Database created")
End Sub
End Class

The error is get is:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Syntax error in CREATE TABLE statement.

Any help is appreciated.

EDITED: The following code gets past the first error but i now get a field definition error, i know the code should work as it works with adding a number field, possible because the field type in access is short text and long text but anything i've tried doesn't seem to work.

Imports ADOX
Imports System.Data.OleDb
Public Class Form1
Dim mycommand As OleDbCommand
Dim myconnection As OleDbConnection
Dim myReader As OleDbDataReader
Dim str As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim cat As New Catalog()
    cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\important\DDL.mdb;Jet OLEDB:Engine Type=5")
    cat = Nothing
    myconnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\important\DDL.mdb ")
    myconnection.Open()
    str = "CREATE TABLE [Users] (Username Varchar(50), Password Varchar(50), E-mail Varchar(50))"

    mycommand = New OleDb.OleDbCommand(str, myconnection)
    mycommand.ExecuteNonQuery()
    MsgBox("Database created")
End Sub
End Class

Solution

  • Looks like you have too many parenthesizes, but I think the actual error is coming from the leading space you have in your query:

                         V
    str = "CREATE TABLE [ " & tablename & "]
    

    Try changing it to:

    str = "CREATE TABLE [" & tablename & "] ([Username] varchar(50), [Password] varchar(50), [E-mail] varchar(75))"
    

    Make sure you dispose of your objects, preferably in a using block:

    Example:

    Using cn As New OleDb.OleDbConnection(mdb)
      cn.Open()
      Using cmd As New OleDb.OleDbCommand(str, cn)
        cmd.ExecuteNonQuery()
      End Using
    End Using