Results 1 to 10 of 10
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919

    Unexpected results with DLookup

    The code below is a "one time" module intended to audit cell numbers in a table to determine if they are in fact numbers listed with mobile carriers in one of the northern regions in Nevada, area code 775. If not, they are to be moved to the landline field of the table and the cell field set to a zero-length string. If the cell "area code - prefix" isn't listed, I'm expecting DLookup to return a Null but never does. I've included a snippet of the tables screenshot at the bottom so you can see the form of the table being searched.

    [/CODE]
    Option Compare Database
    Option Explicit

    Public Function Cells()
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    ' CELL NUMBER AUDIT...............
    ' Loop through the mobile numbers listed in the Registry. If they're not found in the
    ' list of area code 775 mobile carriers, assume they are land lines move to the land
    ' line column and clear the Cell field.
    '
    ' The format of the tblMobilePrefix is a single field of the form xxx-xxx
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    Dim rsReg As DAO.Recordset
    Dim strTemp As String
    Set rsReg = DBEngine(0)(0).OpenRecordset("QRegistry")

    With rsReg
    .MoveFirst
    While Not .EOF
    If Not IsNull(!Cell) Then
    If left(!Cell, 5) = "(775)" Then
    strTemp = Mid(!Cell, 2, 3) & "-" & Mid(!Cell, 7, 3) 'strTemp of the form xxx-xxx
    If IsNull(DLookup("MobilePrefix", "tblMobilePrefix", "[MobilePrefix] = " & strTemp)) Then
    'Current number not listed as being a mobile phone


    .Edit
    !LandLine = !Cell
    !Cell = ""
    .Update
    End If
    End If
    End If

    .MoveNext
    Wend

    End With

    rsReg.Close
    Set rsReg = Nothing

    End Function

    Click image for larger version. 

Name:	000.jpg 
Views:	9 
Size:	18.3 KB 
ID:	44214
    [/CODE]

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    The numbers listed don't have ( ) around them?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    Neither does strTemp, which is the same format as the table being searched: xxx-xxx Moreover, if there's an inconsistency with the format of the search key and the table format, I would expect Dlookup to return null every time.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,925
    Quote Originally Posted by GraeagleBill View Post
    Neither does strTemp, which is the same format as the table being searched: xxx-xxx
    What does that matter?, you are checking for length of 5 with (775) ?
    Walk through the code line by line and inspect the variables/fields. Just very basic debugging 1-0-1.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The value looks like text so I'd expect delimiters:

    If IsNull(DLookup("MobilePrefix", "tblMobilePrefix", "[MobilePrefix] = '" & strTemp & "'")) Then

    If it still doesn't work, set a breakpoint and see what the variable contains, and test it with that value in the Immediate Window.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Quote Originally Posted by GraeagleBill View Post
    Neither does strTemp, which is the same format as the table being searched: xxx-xxx Moreover, if there's an inconsistency with the format of the search key and the table format, I would expect Dlookup to return null every time.
    But the DLookup won't ever get reached because the initial If criteria isn't met?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  7. #7
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    I must really be missing something here. The table being searched is of the form xxx-xxx. The !Cell numbers in the recordset are of the form (xxx) xxx-xxxx. The search key "strTemp" is formatted xxx-xxx and I've verified that repeatedly in debug. I've verified manually in Debug a match with between strTemp and a record in the the table "tblMobilePrefix", so Dlookup should not be returning null.

  8. #8
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    If IsNull(DLookup("MobilePrefix", "tblMobilePrefix", "[MobilePrefix] = '" & strTemp & "'")) Then
    That's what I suspected from the very beginning of this caper but it didn't seem to matter, I'll look at that again. I've done exactly what your suggesting, putting the breakpoint right on the Dlookup and examining the value of all the variables involved.

  9. #9
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    Thanks Paul,
    I must have done something else earlier this morning wrapping strTemp in quotes, as when I just did that again the code behaved as expected.
    Thanks again,
    Bill

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem my friend. With the code paused, I would not only have checked the variable but used it in the Immediate Window:

    ?DLookup("MobilePrefix", "tblMobilePrefix", "[MobilePrefix] = '775-123'")

    Sometimes shows something relevant.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Unexpected sort results
    By George in forum Modules
    Replies: 17
    Last Post: 07-21-2016, 08:00 PM
  2. Unexpected results on Inner Join
    By Access_Novice in forum Access
    Replies: 2
    Last Post: 09-05-2014, 12:50 AM
  3. Unexpected results with DAO recordset
    By GraeagleBill in forum Programming
    Replies: 1
    Last Post: 10-07-2012, 07:37 PM
  4. Unexpected Results from Curdir?
    By bginhb in forum Programming
    Replies: 6
    Last Post: 08-17-2011, 03:58 PM
  5. Query showing unexpected results
    By johnmerlino in forum Queries
    Replies: 30
    Last Post: 10-25-2010, 07:08 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