繁体   English   中英

Excel VBA的语法错误执行创建表

[英]Syntax error with Excel VBA Execute CREATE TABLE

因此,我在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语句中的语法错误。

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))"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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