如何从 table 中查找数据并在其匹配确认数据存在时使用文本进行验证

how to find a data from a table and validate with text if its match confirm data is exist

A table 有一个“代码”列,如果文本框中的数据与“代码”列中的数据匹配,则尝试从访问表单文本框中匹配它必须说是数据存在..请求你的那种assistance.i试过但似乎是错误的

ecode = Me.code.Text
Dim dupsql

dupsql = "SELECT Code FROM [BookingTable]WHERE Code ='" & ecode & "'"
'Debug.Print dupsql
If dupsql = ecode Then

MsgBox " The Entered Code is already in Use! ", vbInformation
end if

SELECT SQL 语句用于打开记录集对象。您的代码不会打开记录集对象。仅引用包含 SQL 字符串的变量什么都不做。

不需要记录集对象。 DLookup域聚合函数可以服务。

If Not IsNull(DLookup("Code", "BookingTable", "Code='" & Me.Code & "'") Then
    MsgBox " The Entered Code is already in Use! ", vbInformation
End If

或 DCount.

If DCount("*", "BookingTable", "Code='" & Me.Code & "'") > 0 Then

我使用了下面的代码并且它有效。

ecode = Me.code.Text
Dim datafind As String
datafind = Nz(DLookup("[Code]", "BookingTable", "Code = '" & ecode & "'"), 0)
Debug.Print datafind

If datafind = ecode Then
MsgBox " The Entered Code is already in Use! ", vbInformation
end if