Dlookup check

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Abhean
    New Member
    • Mar 2019
    • 32

    Dlookup check

    Need some help/pointer. I am attempting to search a table/field to see if it matches a field in a form.

    This is not working for me. I am just looking to hard, but reaching out.

    Code:
    If Me.Txt_field = DLookup("[field]", "Table", Preset_variable) Then
       'found it
    Else
       'didnt find it
                                
    End If
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32633

    #2
    The basic construct of the code seems fine. We don't see what data you're working with, nor what you mean by it failing, so that's as far as we can go for now.

    What is contained in Preset_variable ?

    Comment

    • jimatqsi
      Moderator Top Contributor
      • Oct 2006
      • 1288

      #3
      Let's double-check a few things. What is the name of the table you are searching? What is the name of the field in the table? "Preset_variabl e" doesn't look like a comparison test.

      Comment

      • MikeTheBike
        Recognized Expert Contributor
        • Jun 2007
        • 640

        #4
        Hi All

        Long time since I posted here!

        Not withstanding the previous replies, may I suggest this or similar may help

        Code:
        If DCount("[field]", "Table", "[field] = " & Me.Txt_field) >= 1 Then
           'found it
        Else
           'didnt find it
        End If
        As you just need to know if it exist (at least once), I would use DCount() as DLookUp() will return Null if nothing is found. This of course can ne worked round if absolutely necessary.

        Above assumes Txt_field is numeric.

        I could be totally wrong, but just a thought.

        Comment

        • Abhean
          New Member
          • Mar 2019
          • 32

          #5
          Sorry for the delay. I am attempting to check a table to see if an email is already in the table.

          It appears that the code is always giving a true response.

          And please do not blast me for the naming. I know. :)

          Code:
          Dim FullEmail As String
          Dim EMName As String
          
          'Prepare the email, Strip first initial from first name
               initial = Left(LTDLastName, 1)
              'Combine first name w/ last name initial
               EMName = [FirstName] & [initial]
              'Add @labtechdiagnostics after combination
              Me.LTDEmail.Value = [EMName] & "@LabtechDiagnostics.net"
              'Check Email
              'set full email for lookup
              FullEmail = Me.LTDEmail
                  If FullEmail = DLookup("[LTDEmail]", "LTDEmail", Me.LTDEmail) Then
                          MsgBox "This Email has already been used. Please modify this one." _
                                 & vbCr & vbCr & "Please modify Email.", vbInformation, "Duplicate information"
                           Me.LTDEmail.SetFocus
                      Else
                          MsgBox "Did not Find that Email"
                      
                  End If

          Comment

          • cactusdata
            Recognized Expert New Member
            • Aug 2007
            • 223

            #6
            Try this modification:

            Code:
            If FullEmail = DLookup("[LTDEmail]", "LTDEmail", "[LTDEmail] = '" & Me!LTDEmail.Value & "'") Then

            Comment

            • Abhean
              New Member
              • Mar 2019
              • 32

              #7
              Thanks @cactusdata
              I don't think Ill ever be able to figure out the damnable quotes.
              Works like a dream

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32633

                #8
                Originally posted by Abhean
                Abhean:
                I don't think Ill ever be able to figure out the damnable quotes.
                See if Quotes (') and Double-Quotes (") - Where and When to use them helps.
                Originally posted by Abhean
                Abhean:
                And please do not blast me for the naming. I know. :)
                I won't. Please understand though, that if/when we do draw your attention to things like naming and code indenting it's because we understand how getting it wrong can lead to extra levels of confusion and difficulty for you. If something's simple then you won't struggle so much with it. Naming and code indentation help you to read what is there and understand it more easily. Their benefits are not restricted to that but they certainly do go to make your life easier, quite apart from ours when we want to help you.

                Comment

                Working...