Excel VBA语法错误执行CREATE TABLE

时间:2018-01-12 00:01:06

标签: excel-vba vba excel

所以我在Excel VBA中作为一些统计测试的前端计算器。完成所有步骤后,我希望能够保存计算所涉及的原始数据。但是,随着最终使用的数据量的增加,文件将变得越来越大,因此我想导出到Access数据库。我有大部分工作都可以获取我可以创建预制表的信息,但是我遇到了CREATE TABLE功能的语法问题。

这是我的代码:

Private Sub EndAndExport_Click()

Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim dbPath As String
Dim Tblname As String

'Single path name, position of the file should never change
dbPath = "\\qs-nas1\Data1\QA\Bottling Level Check Database.accdb"

'Connect to new database
Set cnn = New ADODB.Connection

'Protection against no information
If Sheet5.Range("A6").Value = "" Then
MsgBox "There is no data, please enter sample data"
Exit Sub
End If

'Opening the connection
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath & ";"

'Entering data into a pre-existing table
Set rst = New ADODB.Recordset

rst.Open "Bottling_Information", cnn, adOpenDynamic, adLockOptimistic, adCmdTable
rst.AddNew
rst.Fields("Lot") = Cells(1, 2).Value
rst.Fields("Manufacturing Date") = Cells(1, 4).Value
rst.Fields("Bottling Date") = Cells(1, 6).Value
rst.Fields("Bottle Fill Amount") = Cells(3, 2).Value
rst.Update
rst.Close

'Setting variable as new table names value
Tblname = Sheet5.Cells(1, 2).Value

'Attempting to create the new table with the afformentioned string
'Where I get my syntax error
With cnn
 .Execute "CREATE TABLE " & Tblname & " ([MeasuredVolume] text(25), " & _
        "[MeasuredWeight] text(25), " & _
        "[CalculatedVolume] text(25))"
End With

'Rest of the code which should populate the newly created table
Set rst = New ADODB.Recordset

rst.Open " & Tblname & ", cnn, adOpenDynamic, adLockOptimistic, adCmdTable

For i = 1 To SampleLabel5
    rst.AddNew
    For j = 1 To 3
    rst(Cells(1, j).Value) = Cells(i + 5, j + 1).Value
    Next j
    rst.Update
Next i

'Clearing the memory for my connection and recordset variables
Set rst = Nothing
Set cnn = Nothing

'Clears data that was exported
Sheet5.Range(Cells(6, 1), Cells(SampleLabel5 + 5, 4)).ClearContents
Sheet5.Cells(1, 2).ClearContents
Sheet5.Cells(1, 4).ClearContents
Sheet5.Cells(1, 6).ClearContents
Sheet5.Cells(3, 2).ClearContents
Sheet5.Cells(3, 5).ClearContents
Sheet5.Range("H3:J3").ClearContents
Sheet5.Range("H6:J6").ClearContents
Sheet5.Range("H8:J10").ClearContents
Sheet5.Range("H14:J14").ClearContents
Sheet5.Range("H17:J17").ClearContents
Sheet5.Range("H19:J21").ClearContents
Sheet1.Activate

End Sub

我得到的错误是:

运行时错误' -2147217900(80040e14)': CREATE TABLE语句中的语法错误。

1 个答案:

答案 0 :(得分:0)

SQL语句

CREATE TABLE CSF1802/1 (
       [MeasuredVolume] text(25), 
       [MeasuredWeight] text(25), 
       [CalculatedVolume] text(25)
                       )
由于表名中的正斜杠(/),

无效。你需要一份

的陈述
CREATE TABLE [CSF1802/1] (
       [MeasuredVolume] text(25), 
       [MeasuredWeight] text(25), 
       [CalculatedVolume] text(25)
                       )

要创建该语句,您需要VBA代码

.Execute "CREATE TABLE [" & Tblname & "] ([MeasuredVolume] text(25), " & _
    "[MeasuredWeight] text(25), " & _
    "[CalculatedVolume] text(25))"