Search code examples
vbams-accessms-access-2016

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


A table has a column "Code" and trying to match from access form text box if the data in the textbox match with data in column "Code" it has to say yes data is exist.. request your kind assistance.i tried but seems to be wrong

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

Solution

  • A SELECT SQL statement is used to open a recordset object. Your code does not open a recordset object. Just referencing variable that holds SQL string does nothing.

    Don't need a recordset object. DLookup domain aggregate function can serve.

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

    Or DCount.

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