2016-04-15 74 views
1

SQL數據類型不匹配運行時表達低於我得到一個錯誤說:標準表達式

Data type mismatch in criteria expression 

的消息框是用來識別發生錯誤,它會檢查點1,那麼它停止!

    Dim table2 As New DataTable 
        Dim recordcount2 As Integer 
        Dim command2 As String = "SELECT * FROM [Results] where " & "[TestID] = " & " '" & CInt(LocationID) & "'" 
        Dim adapter2 As New OleDb.OleDbDataAdapter(command2, conn) 
        table2.Clear() 
        MsgBox("Checkpoint 1") 
        recordcount2 = adapter2.Fill(table2) 
        MsgBox("Checkpoint 2") 

的代碼是在我的方案本節:按鈕

Try 
     'Defining variables 
     Dim table As New DataTable 
     Dim command As String 
     Dim recordCount As Integer 
     Dim LocationID As Integer 
     command = "SELECT * FROM [Test] where " & "[MachineID] = " & " '" & machineID & "'" 'SQL command to find if there is a usename stored with that is in username text box 

     Dim adapter As New OleDb.OleDbDataAdapter(command, conn) 'adapter 
     table.Clear() 'adding data to a table. 
     recordCount = adapter.Fill(table) 
     If recordCount <> 0 Then 
      For i = 0 To recordCount 
       Try 
        LocationID = CInt(table.Rows(i)(0)) 
        Dim table2 As New DataTable 
        Dim recordcount2 As Integer 
        Dim command2 As String = "SELECT * FROM [Results] where " & "[TestID] = " & " '" & CInt(LocationID) & "'" 
        Dim adapter2 As New OleDb.OleDbDataAdapter(command2, conn) 
        table2.Clear() 
        MsgBox("Checkpoint 1") 
        recordcount2 = adapter2.Fill(table2) 
        MsgBox("Checkpoint 2") 
        If recordcount2 <> 0 Then 
         For x = 0 To recordcount2 
          MsgBox("yay1") 
          Dim TestID As String = table2.Rows(x)(1) 
          Dim Thickness As String = table2.Rows(x)(2) 
          Dim TargetFilter As String = table2.Rows(x)(9) 
          Dim SNR As String = table2.Rows(x)(3) 
          Dim STD As String = table2.Rows(x)(4) 
          MsgBox("yay2") 
          Dim M1 As String = table2.Rows(x)(5) 
          Dim M2 As String = table2.Rows(x)(6) 
          Dim kVp As String = table2.Rows(x)(7) 
          Dim mAs As String = table2.Rows(x)(8) 
          MsgBox("yay3") 
          Dim CNR As Short = (CLng(M1) - CLng(M2))/2 
          MsgBox("Further") 
          dgvViewData.Rows.Add(TestID, Thickness, CStr(SNR), CStr(STD), CStr(M1), CStr(M2), kVp, mAs, CStr(CNR)) 
         Next 
        Else 
         MsgBox("RIP") 
        End If 
       Catch ex As Exception 
        MessageBox.Show(ex.Message) 
       End Try 
      Next 
     Else 
      MsgBox("There data for this machine.") 
     End If 
    Catch ex As Exception 
     MessageBox.Show(ex.Message) 
    End Try 

代碼

Try 
     Dim table As New DataTable 
     Dim command As String 
     Dim recordCount As Integer 
     Dim TestNum As String = "1" 
     command = "SELECT * FROM [Results] where " & "[TestID] = " & " '" & CStr(TestNum) & "'" 
     Dim adapter As New OleDb.OleDbDataAdapter(command, conn) 
     table.Clear() 
     recordCount = adapter.Fill(table) 
    Catch ex As Exception 
     MessageBox.Show(ex.Message) 
    End Try 

回答

0

你locationID參數必須是要連接的字符串到你的SELECT語句。我假設它是一個整數,導致您的數據類型不匹配。

+0

感謝您的快速性反應,我改變了命令2本: Dim command2 As String =「SELECT * FROM [Results] where」&「[TestID] =」&「'」&CStr(LocationID)&「'」 但是我仍然收到相同的錯誤! 另外在訪問時,TestID是一個數字:https://gyazo.com/5965941b5d3a56678971e42dd5de84a0 –

+0

你確定錯誤是本地的那行代碼?嘗試擺脫一切不需要的東西,然後再次運行,以確保錯誤不是來自腳本中其他地方的錯誤。另外,我注意到你的參數化[TestID]。沒有理由,因爲你很難編碼它。 – 2016-04-15 02:01:45

+0

基本上什麼我的代碼是幹什麼的,用戶給予了機號,我希望所有的那臺機器已經做在DataGridView列出的結果,這裏是aphoto數據庫關係https://gyazo.com/11d9d441031fc76a8f0338d6f8339eff –

0

我敢肯定,你的LocationID是一個整數,因此應該被連接在一起,所以SQL應改爲:

Dim command2 As String = "SELECT * FROM [Results] where [TestID] = " & CStr(LocationID) & "" 
+1

嘿,我有在OP的底部添加了一些代碼,這段代碼仍然返回相同的錯誤。 –

+0

那麼,打印出_command_的內容並學習。 – Gustav

+0

這沒有奏效,但在訪問時,我將兩個表上的TestID從「Number」更改爲「Short Text」,現在它可以工作了! –

相關問題