Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2024
    Posts
    3

    Post DLookup returning null value no matter what I do


    I have a table 'tblContacts'. It has what you would expect to find with regards to fields in a contact db. It includes the [ID] field as well as a [CompanyLogo] field that looks up the value from a table called 'tblCompanyProfiles'

    I am attempting what I would think is a very simple lookup to search the tblContacts, locating the contact ID I want and then returning the value in field [CompanyLogo].
    No matter what I have tried, it seems to always result in a null value. I can confirm that the contact ID that I am using is valid and it has data in [CompanyLogo].
    My tired eyes would appreciate your help.

    Code:
    Private Sub Command56_Click()
    
    
      Dim companyID As Variant
      Dim ownerID As Long
    
    On Error GoTo Command56_Click_Err
    
    
        On Error Resume Next
        If (Form.Dirty) Then
            DoCmd.RunCommand acCmdSaveRecord
        End If
        If (MacroError.Number <> 0) Then
            Beep
            MsgBox MacroError.Description, vbOKOnly, ""
            Exit Sub
        End If
        If (Form.NewRecord) Then
            Beep
            Exit Sub
        End If
    
    
      
    
    
        ownerID = Me.Owner.Value ' From the current form and is a number
        MsgBox "ownerID " & ownerID  ' I get the correct value here.
        
    
    
        companyID = DLookup("First Name", "tblContacts", "ID = " & ownerID)  ' I have attempted numerous variations of this statement, but it always returns nothing.
        MsgBox "companyID " & companyID
    
    
    Command56_Click_Exit:
        Exit Sub
    
    
    Command56_Click_Err:
        MsgBox Error$
        Resume Command56_Click_Exit
    End Sub

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    You are pulling value from First Name, not CompanyLogo.

    Also, since field name has space, need to use []: [First Name]. Don't use spaces and punctuation/special characters nor reserved words as names in naming convention and you can avoid this frustration in most cases.

    What purpose does it serve to annoy user with a popup message just to tell them this info? Is this just for testing? What do you really plan to do with this value? Should be able to show this info in a textbox on form.

    If you want to provide db for analysis, follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    'On error Resume Next' will mask any error that exists. Comment out that line and see if it shows you an error. Does the field ownerID exist in tblContacts? Seems more logical that it would be in tblCompany, if you have it.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    When you get like that. Stop, and come back to it later. Generally you will spot the error then.
    As mentioned, you are not even using the correct field, but you still should be bringing back something unless they have no first name for that ID?
    Though if it is a company perhaps there is no first name?
    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
    Join Date
    Mar 2024
    Posts
    3
    My apologies. I was testing different fields to see if there was something specific about the actual field I was targeting. I failed to change it before I sent the cry for help.

    I will re-test with brackets [CompanyLogo]

    Thank you

  6. #6
    Join Date
    Mar 2024
    Posts
    3
    Thank you. [] did the trick. Something so simple, but when you can't see the trees from the forest because its been frustrating you for so long.

    Thanks again.

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    CompanyLogo does not have space nor special characters. Brackets are not required but don't hurt. CompanyLogo without brackets should have worked.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Returning 0 value if count() is null
    By scott0_1 in forum Queries
    Replies: 5
    Last Post: 05-14-2018, 06:37 PM
  2. Replies: 8
    Last Post: 03-30-2018, 03:58 PM
  3. Replies: 1
    Last Post: 04-15-2015, 10:52 AM
  4. DLookUp Not Returning Anything
    By Bkper087 in forum Access
    Replies: 5
    Last Post: 04-08-2015, 11:54 AM
  5. DMax returning Null
    By Markb384 in forum Access
    Replies: 1
    Last Post: 05-01-2014, 09:11 AM

Tags for this Thread

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