• Problem with DLookup

    Author
    Topic
    #461328

    I’m trying to use DLookUp to see if a name exists in a database. I’m using the following code:

    If Me.LastName = DLookup(“LastName”, “tblStudentNames”) Then
    MsgBox “This name already exists in the database.”
    End If

    It seems to only be looking at the first row in the database and not the other rows. I’m not sure what I’m doing wrong or if I’m doing this correctly.

    Thanks,
    Leesha

    Viewing 1 reply thread
    Author
    Replies
    • #1169864

      You must specify the third argument (the condition), otherwise DLookup will return the first name it finds.
      Try

      If Not IsNull(DLookup(“LastName”, “tblStudentNames”, “LastName=” & Chr(34) & Me.LastName & Chr(34))) Then

      or

      If DCount(“*”, “tblStudentNames”, “LastName=” & Chr(34) & Me.LastName & Chr(34)) > 0 Then

    • #1169877

      Perfect! I even tried it using a query instead of a table and it worked!

      Thanks,
      Leesha

    Viewing 1 reply thread
    Reply To: Problem with DLookup

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: