简体   繁体   中英

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

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

At the end the code looks like this:

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=C:\Test\DDL.mdb;Jet OLEDB:Engine Type=5")
        cat = Nothing
        Using myconnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Test\DDL.mdb ")
            myconnection.Open()
            str = "CREATE TABLE [Users] ([Username] varchar(50), [Password] varchar(50), [E-mail] varchar(50))"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM