2017-04-17 91 views
0

我有下面的代碼,當我運行它的代碼行結束時,我得到它規定VB從Access數據庫讀取

System.Data.OleDb.OleDbException一個錯誤:「語法錯誤(缺少運算符) 查詢表達式

,但我不知道這意味着什麼,我使用VB(Visual Studio中)和訪問數據庫。該錯誤出現在我聲明dr變量(我用於我的數據讀取器)的行中。 任何幫助將不勝感激非常感謝,

Private Function CarFinder(b As String, m As String, s As String, d As Integer, e As String) 
    myConnection.ConnectionString = My.Forms.Main.connString 
    myConnection.Open() 
    Dim match As String 
    match = "SELECT * FROM CarFigures WHERE CBrand = '" & cbBrand.Text & "' CModel = '" & 
     cbModel.Text & "' and CSpec = '" & cbSpec.Text & "'" 


Using cmd As New OleDb.OleDbCommand(match, myConnection) 
cmd.Parameters.AddWithValue("@b", b) 
cmd.Parameters.AddWithValue("@m", m) 
     cmd.Parameters.AddWithValue("@s", s) 
     cmd.Parameters.AddWithValue("@d", d) 
     cmd.Parameters.AddWithValue("@e", e) 
     Dim dr = cmd.ExecuteReader() 

     If dr.Read Then 
      CO2e = dr("CCO2") 
      Ins = dr("InsuranceBand") 
      mpg = dr("CMPG") 
      time = dr("C060Time") 
      ncap = dr("CarNCAP") 
      bik = dr("Bik Rating") 

     End If 

回答

0

它看起來像你的SQL查詢字符串是錯誤的。在「cbBrand.Text」和「CModel =」之間缺少AND。它應該像下面...

match = "SELECT * FROM CarFigures WHERE CBrand = '" & cbBrand.Text & "' and CModel = '" & cbModel.Text & "' and CSpec = '" & cbSpec.Text & "'" 
+0

稀釋是謝謝你,我錯過了,當我重讀的代碼謝謝 – Courteney

+0

另一個問題是什麼錯誤意味着「System.IndexOutOfRangeException」 – Courteney