标准表达式

时间:2016-04-15 01:47:30

标签: sql vb.net access-vba

运行下面的表达式时,我收到错误消息:

 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

2 个答案:

答案 0 :(得分:0)

您的locationID参数必须是要连接到select语句的字符串。我假设它是一个导致数据类型不匹配的整数。

答案 1 :(得分:0)

我很确定你的LocationID是一个整数,因此应该按连接,所以SQL应该是:

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