Solved Dlookup works with long not with string? (1 Viewer)

chizzy42

Registered User.
Local time
Today, 10:01
Joined
Sep 28, 2014
Messages
115
Hi, Hope all is well. Just a question if anyone has time. I have a piece of code i was using to check if a record existed in a table and seemed to be OK . I had thought of using it in another entry form , the ID on this new form having letters and numbers instead of just an integer. The code below works ok with an integer input and the field in the table as a number
Code:
If Len(Me.ResID.Value & vbNullString) > 0 Then

Dim RPSno As Long

RPSno = Nz(DLookup("[ResID]", "tblOp", "[ResID]= " & Me.ResID), 0)
    If RPSno <> 0 Then
    MsgBox (RPSno)
     MsgBox ("number DOES exist in database")
        Cancel = True

        Else

        MsgBox ("Number Does NOT exist in the Database")
        Dim strSQL As String
        strSQL = "INSERT INTO tblOp (Surname,FirstName,ResID) Values(text2,text4,ResID)"
        DoCmd.RunSQL strSQL
        Me.Text2.Value = ""
        Me.Text4.Value = ""
        Me.ResID.Value = ""
    End If

End If

When i tried changing the variable to Dim RPSno As String and the table field to text. I get errors.

if i enter E4567 i get RTE 2471 . The expression you entered as a query paramater produced this error E4567
and if i enter 9999 i get the error RTE 3464 Data type mismatch in criteria expression.

Confused as to why if i change to string i get errors from the Dlookup as i thought it wouldn't matter what the lookup was comparing as long as the table field matched the comparison?

Any explantion would be appreciated as always
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:01
Joined
Oct 29, 2018
Messages
21,449
Hi. In VBA, data types may need delimiters. Numbers don't, but Text and Dates do. Try it this way.
Code:
RPSno = Nz(DLookup("[ResID]", "tblOp", "[ResID]= '" & Me.ResID & "'"), 0)
 

chizzy42

Registered User.
Local time
Today, 10:01
Joined
Sep 28, 2014
Messages
115
Bingo...caught out again with delimiters...thanks DBguy works a dandy
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:01
Joined
Oct 29, 2018
Messages
21,449
Bingo...caught out again with delimiters...thanks DBguy works a dandy
You're welcome. When checking for duplicates, I prefer using the DCount() function. It just seems funny to me to use DLookup() to look up something you already have or know.

Good luck with your project.
 

Users who are viewing this thread

Top Bottom