2015-12-23 54 views
0

我想在數據庫中創建新的訪問表的名字,但是當我試圖從文本框它給錯誤命名創建一個新的訪問表,然後從文本框

子CREATETABLE()

Dim strCreate As String = "CREATE TABLE"& TxtBoxTb1Name.Text &(" & _ 

「國家或地區名稱爲varchar(120)主鍵,」 & _ 「大陸整數,」 & _ 「區龍」 & _ 「人口龍」 & _ 「資本VARCHAR(80),」 & _ 「Code char(2));」

Dim conDatabase As OleDbConnection = New OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;" & _ 
    "Data Source=""Data Source=" & filename & ".mdb;") 
    Dim cmdDatabase As OleDbCommand = New OleDbCommand(strCreate, conDatabase) 
    conDatabase.Open() 
    cmdDatabase.ExecuteNonQuery() 
    conDatabase.Close() 
    MessageBox.Show("Table Created Sucessfully") 
End Sub 
+1

[命名一個訪問表的可能的複製從文本框的文本](http://stackoverflow.com/questions/34433029/naming-a-access-table-from-textboxs-text) –

回答

0

您必須修改sintax來定義主鍵,並在創建SQL語句時注意空格和逗號。以下是可用的固定代碼。

進口System.Data.OleDb 進口System.IO 公共類Form1中 私人文件名作爲字符串= 「SampleDB.mdb」

Private Sub CreateTable() 
    Dim strCreate As String = "CREATE TABLE " & 
     TxtBoxTb1Name.Text & "(_Continent Integer, " & "_Area Long, " & "_Population Long," & "_Capital varchar(80)," & "_Code Char(2)," & "_CountryName varchar(120) CONSTRAINT MyFieldConstraint PRIMARY KEY)" 

    Dim conDatabase As OleDbConnection = New OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0; " & 
     "Data Source=" & Path.GetFullPath(filename) & "; User Id=admin; 
     Password =;") 
    conDatabase.Open() 
    Dim cmdDatabase As OleDbCommand = New OleDbCommand(strCreate, conDatabase) 

    cmdDatabase.ExecuteNonQuery() 
    conDatabase.Close() 
    MessageBox.Show("Table Created Sucessfully") 

End Sub 

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    CreateTable() 
End Sub 

末級

相關問題