标准表达式中的类型不匹配

Type mismatch in criteria expression

我是 visual basic 新手。我想根据 phone 号删除一条记录。该程序将要求用户输入他的号码,并在将 phone 号码与数据库匹配后删除该记录。我尝试这样做,但它给我一个错误 "Type mismatch in criteria expression"。 我使用的是 ms access 2007,我已将 phone 列的数据类型指定为 Number 。这是我的 vb 代码。请帮我 。这一定是我的愚蠢错误之一。在插入数据模块中,我已将数据类型指定为 Long,在此之前我已将其指定为 Integer,但同样的错误仍然存​​在。

Imports System.Data.OleDb
Public Class Form5
    Public Connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\AMEN\Documents\Railway.accdb"
    Public Conn As New OleDbConnection

    Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Conn.ConnectionString = Connstring
        If Conn.State = ConnectionState.Closed Then
            Conn.Open()
        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim okToDelete As MsgBoxResult = MsgBox("Are you sure you want to cancel your ticket?", MsgBoxStyle.YesNo)

        If okToDelete = MsgBoxResult.Yes Then
            Dim str As String
            str = "Delete from Table2 Where Phone = '" & TextBox1.Text & "'"
            Dim cmd As OleDbCommand = New OleDbCommand(str, Conn)
            Try
                cmd.ExecuteNonQuery()
                cmd.Dispose()
                Conn.Close()

                MsgBox("Your Ticket Cancelled Sucessfully ! ")
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        ElseIf okToDelete = MsgBoxResult.No Then
        End If

    End Sub
End Class

Phone 数字应该存储为字符串而不是数字。 但是,如果您的 Phone 字段是数字,那么您的查询的一部分是错误的。
在那种情况下,您不需要在 TextBox1.Text 周围加上引号。这会将您的输入视为一个字符串,并且该字符串不能用于在数字字段中进行搜索。

最好的方法是使用参数化查询,您可以在其中指定传递的参数的数据类型
(我假定您已在 Access 中将字段 Phone 创建为 Long Number)

If okToDelete = MsgBoxResult.Yes Then

    ' Check if your user types really a number.
    Dim number As Integer
    if Not Int32.TryParse(TextBox1.Text, number) Then
        MessageBox.Show("Not a valid number!")
        return
    End If
    Dim str As String
    str = "Delete from Table2 Where Phone = @phone"
    Dim cmd As OleDbCommand = New OleDbCommand(str, Conn)
    Try
        cmd.Parameters.Add("@phone", OleDbType.Integer).Value = number
        cmd.ExecuteNonQuery()
        ....