簡體   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