如何从表中查找数据并使用文本验证其是否匹配确认数据存在



表有一个列"Code"如果文本框中的数据与列"代码"中的数据匹配,则尝试从访问表单文本框中进行匹配;它必须说数据是存在的…请求您的帮助。我试了,但似乎是错误的

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