2012-11-29 43 views
2

此程序背後的前提是獲取幾個不同的Excel表並將它們輸入到訪問數據庫。一切工作,直到我執行語句產生以下錯誤:在查詢表達式插入語句中缺少VBA Excel語法錯誤操作符

Run-time error '-2147217900 (80040e14)': 
Syntax error (missing operator) in query expression 'Alabama AM University' 

這個問題似乎在,與「school」變量交易的執行我的發言部分出現。下面是 是代碼。

Sub updateData() 

Dim cn As New ADODB.Connection 
Dim row As Integer 
Dim col As Integer 
Dim srcRow As Integer 
Dim srcCol As Integer 
Dim filename As String 
Dim targetWorkbook As Workbook 
Dim targetSheet As Worksheet 
Dim reportYear As String 
Dim state As String 
Dim school As String 
Dim campus As String 
Dim dataLabel As String 
Dim dataValue As Long 


On Error Resume Next ' these lines should allow you to connect. Depending on your version of windows, only one will execute without error. 
    cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\universityCrime.mdb" & ";" 
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ThisWorkbook.Path & "\universityCrime.mdb" & ";" 
On Error GoTo 0 

'Starting here! 
row = 6 
col = 3 
srcRow = 1 
srcCol = 1 

Application.ScreenUpdating = False 

filename = Dir(ThisWorkbook.Path & "\data\*.xls") 

'Loops through all the file names in the data directory 
Do Until filename = "" 

    Set sourceWorkbook = Application.Workbooks.Open(ThisWorkbook.Path & "\data\" & filename) 
    Set sourceSheet = sourceWorkbook.Sheets(1) 


    reportYear = Right(sourceSheet.Range("A4").Value, 4) 
    state = Left(sourceSheet.Range("A2").Value, 1) & LCase(Mid(sourceSheet.Range("A2").Value, 2)) 

    Do Until sourceSheet.Cells(row, 1).Borders(xlEdgeBottom).LineStyle = 1 
     school = sourceSheet.Cells(row, 1).Value 
     campus = sourceSheet.Cells(row, 2).Value 

     Do Until sourceSheet.Cells(row, col) = "" 
      dataLabel = sourceSheet.Cells(5, col).Value 
      dataValue = sourceSheet.Cells(row, col).Value 

      If campus = "" Then 
       cn.Execute "insert into crimedata(reportYear, state, school, campus, dataLabel, dataValue)values(" _ 
       & reportYear & ", " _ 
       & state & ", " _ 
       & school & ", " _ 
       & campus & ", " _ 
       & dataLabel & ", " _ 
       & dataValue & ")" 
      Else: 
       cn.Execute "insert into crimedata(reportYear, state, school, campus, dataLabel, dataValue)values(" _ 
       & reportYear & ", " _ 
       & state & ", " _ 
       & school & ", " _ 
       & "NULL, " _ 
       & dataLabel & ", " _ 
       & dataValue & ")" 
      End If 

      srcCol = 1 
      srcRow = srcRow + 1 
      col = col + 1 
     Loop 

     col = 3 
     row = row + 1 
    Loop 

    row = 6 
    sourceWorkbook.Close 
    filename = Dir 
Loop 

cn.Close 
Application.ScreenUpdating = True 

End Sub 

謝謝!

+0

它有助於將sql構建成字符串,然後執行該字符串。額外的好處是你可以把字符串轉儲到某個地方,這樣你就可以看到它是否被破壞了。如果你能做到這一點,幷包括一個實際的sql生成的樣本,這將有助於很多。 – Omnikrys

+0

是否需要爲每個字符串值加上單引號? –

+0

@omnikrys所以這個代碼看起來應該是這樣的代碼插入crimedata(reportYear,state,school,campus,dataLabel,dataValue)values(2011,'Utah','University of Utah',NULL, 'Arson','1')'code'它的輸出當然是「2011年,猶他大學,猶他大學,NULL,Arson,1」我假設這是你正在尋找的東西? –

回答

0

因此@Omnikrys和@patrick choi幫助我做了一件好事。事實證明,我所需要的只是執行語句周圍的單引號。因此,而不是我的執行語句看起來像這樣:

cn.Execute "insert into crimedata(reportYear, state, school, campus, dataLabel, dataValue) values(" _ 
       & reportYear & ", '" _ 
       & state & "', '" _ 
       & school & "', '" _ 
       & campus & "', '" _ 
       & dataLabel & "', " _ 
       & dataValue & ")" 

感謝您的提示傢伙:

cn.Execute "insert into crimedata(reportYear, state, school, campus, dataLabel, dataValue)values(" _ 
      & reportYear & ", " _ 
      & state & ", " _ 
      & school & ", " _ 
      & campus & ", " _ 
      & dataLabel & ", " _ 
      & dataValue & ")" 

我不得不說正好是這樣一個文本變量之間的所有加單引號!它有幫助。

相關問題