Results 1 to 7 of 7
  1. #1
    dweekley is offline Advanced Beginner
    Windows XP Access 2002 (version 10.0)
    Join Date
    May 2010
    Posts
    52

    Dlookup, Checing for duplicates

    I am using dlookup to check the table for a duplicate value. I am getting an invalid use of null. I'm thinking that would be correct, just handling it wrong?

    'Check Duplicate Entry


    Dim CUSTID As String 'Customer Number

    CUSTID = DLookup("[CustomerID]", "t_Customer", "[CustomerID] = '" & Forms!f_Customer!txtCustomerID & "'")

    If Len(txtCustomerID.Value & "") = 0 Then Exit Sub

    If txtCustomerID.Value = CUSTID Then



    If MsgBox("There is a Customer ID by that number!" _
    & Chr$(13) _
    & "You must create a different Customer ID number.", vbOKOnly, _
    "Duplicate Customer ID") = vbOK Then

    txtCustomerID.Value = ""
    txtCustomerID.SetFocus

    End If

    End If

    TIA

  2. #2
    dweekley is offline Advanced Beginner
    Windows XP Access 2002 (version 10.0)
    Join Date
    May 2010
    Posts
    52
    Well...I changed the As String to As Variant (due to the null) and seems to be working after some more reading.

    Now I am trying to set the focus back to the txtCustomerID control. No luck. Keeps going to next in line.

  3. #3
    dweekley is offline Advanced Beginner
    Windows XP Access 2002 (version 10.0)
    Join Date
    May 2010
    Posts
    52
    Ok...here is where I am at. Working less the setfocus. Being used on the Lost Focus Event. Now I am thinking that if someone goes into that record and tabs through the event will trigger when it doesn't need to now. Better event to use...or something to cover the tabbing through?

    'Check Duplicate Entry


    Dim CUSTID As String 'Customer Number
    Dim CUSTNAME As String 'Customer Name

    CUSTID = Nz(DLookup("CustomerID", "t_Customer", "CustomerID = '" & Forms!f_Customer!txtCustomerID & "'"))
    CUSTNAME = Nz(DLookup("Customer_Name", "t_Customer", "CustomerID = '" & Forms!f_Customer!txtCustomerID & "'"))

    If Len(txtCustomerID.Value & "") = 0 Then Exit Sub

    If txtCustomerID.Value = CUSTID Then

    If MsgBox("You must create a different Customer ID number!" _
    & Chr$(13) _
    & CUSTNAME & "...Has already been assigned that number.", vbOKOnly, _
    "Duplicate Customer ID") = vbOK Then

    txtCustomerID.Value = ""
    txtCustomerID.SetFocus

    End If

    End If

  4. #4
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    Code:
    Dim CUSTID As String 'Customer Number
    Dim CUSTNAME As String 'Customer Name
    
    
    If Len(txtCustomerID.Value & "") = 0 Then Exit Sub
    CUSTID = Nz(DLookup("CustomerID", "t_Customer", "CustomerID = '" & Forms!f_Customer!txtCustomerID & "'"))
    CUSTNAME = Nz(DLookup("Customer_Name", "t_Customer", "CustomerID = '" & Forms!f_Customer!txtCustomerID & "'"))
    
    
    If txtCustomerID.Value = CUSTID Then
    
    If MsgBox("You must create a different Customer ID number!" _
    & Chr$(13) _
    & CUSTNAME & "...Has already been assigned that number.", vbOKOnly, _
    "Duplicate Customer ID") = vbOK Then
    
    txtCustomerID.Value = ""
    txtCustomerID.SetFocus
    
    End If
    
    End If
    Makes more sense to move the line as indicated.

  5. #5
    dweekley is offline Advanced Beginner
    Windows XP Access 2002 (version 10.0)
    Join Date
    May 2010
    Posts
    52
    Thanks for the update. I am thinking now that because I have the 'If, End If' as individuals I am going to have to either use some IfElse or Select Case to keep them from running individually. I hope I explained that correctly. Otherwise each will attempt to run. I have this on the LostFocus event.

    I also found the issue with setting the focus after a Messege box. Trying to setfocus back to itself when it actually hasn't left yet.

    'Check Duplicate Entry


    Dim CUSTID As String 'Customer Number
    Dim CUSTNAME As String 'Customer Name

    If Len(txtCustomerID.Value & "") = 0 Then Exit Sub

    If Len(txtCustomerID.Value & "") > 0 And Len(txtCustomerID.Value & "") < 3 Then

    If MsgBox("You have entered an invalid number to be used as a Customer ID!" _
    & Chr$(13) _
    & "Please select no less than 3 numbers.", vbOKOnly, _
    "Bad Number Entry") = vbOK Then

    GoTo CISFCF

    End If

    End If

    CUSTID = Nz(DLookup("CustomerID", "t_Customer", "CustomerID = '" & Forms!f_Customer!txtCustomerID & "'"))
    CUSTNAME = Nz(DLookup("Customer_Name", "t_Customer", "CustomerID = '" & Forms!f_Customer!txtCustomerID & "'"))

    If txtCustomerID.Value = CUSTID Then

    If MsgBox("You must create a different Customer ID number!" _
    & Chr$(13) _
    & CUSTNAME & "...Has already been assigned that number.", vbOKOnly, _
    "Duplicate Customer ID") = vbOK Then

    GoTo CISFCF

    End If

    End If

    CISFCF:


    Me!btnCUSTSRCH.SetFocus
    Me!txtCustomerID.SetFocus
    Me!txtCustomerID.Value = ""

    'Debug.Print CUSTID
    'Debug.Print CUSTNAME

    End Sub

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Validation is typically done in the before update event. You can cancel the update and leave focus with:

    Cancel = True
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    Code:
    Dim CUSTID As String 'Customer Number
    Dim CUSTNAME As String 'Customer Name
    
    If Len(txtCustomerID.Value & "") = 0 Then Exit Sub
    
    If Len(txtCustomerID.Value & "") > 0 And Len(txtCustomerID.Value & "") < 3 Then
    
        MsgBox "You have entered an invalid number to be used as a Customer ID!" _
        & Chr$(13) _
        & "Please select no less than 3 numbers.", vbOKOnly, _
        "Bad Number Entry")
        GoTo CISFCF
    End If
    
    CUSTID = Nz(DLookup("CustomerID", "t_Customer", "CustomerID = '" & Forms!f_Customer!txtCustomerID & "'"))
    CUSTNAME = Nz(DLookup("Customer_Name", "t_Customer", "CustomerID = '" & Forms!f_Customer!txtCustomerID & "'"))
    
    If txtCustomerID.Value = CUSTID Then
    
        MsgBox "You must create a different Customer ID number!" _
        & Chr$(13) _   
        & CUSTNAME & "...Has already been assigned that number.", vbOKOnly, _
        "Duplicate Customer ID") = vbOK Then
        GoTo CISFCF
    End If
    
    CISFCF:
    Me!btnCUSTSRCH.SetFocus
    Me!txtCustomerID.SetFocus
    Me!txtCustomerID.Value = ""
    
    
    'Debug.Print CUSTID
    'Debug.Print CUSTNAME
    End Sub
    You don't need the msgbox function to get the results you want. A display type messagebox will work. See the updated code. It gets rid of 2 IFs.

    pbaldy (and you) are right. A better place for the code is in the before_update event.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 01-18-2016, 01:55 PM
  2. Changing from duplicates OK to no duplicates
    By bellczar in forum Access
    Replies: 2
    Last Post: 11-01-2014, 11:40 AM
  3. Dlookup to prevent duplicates
    By arothacker in forum Access
    Replies: 16
    Last Post: 02-12-2014, 11:40 AM
  4. Replies: 2
    Last Post: 01-04-2013, 09:04 PM
  5. Replies: 15
    Last Post: 12-03-2010, 10:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums