2016-04-15 70 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”,现在它可以工作了! –

相关问题