Results 1 to 8 of 8
  1. #1
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352

    How to Perform a DLookUp for something Not in table


    As title states..
    I want to perform a DLookup for something NOT already in a table.
    I want to add records via button but do not want to add it if a certain field is already included in that table.

    First pass at possible code??
    Code:
    Set V = CurrentDb.OpenRecordset("tblCommodity") 
    If DLookup("Commodity_PK", "tblCommodity", "Commodity_PK <>") Then
    V.AddNew
    V![Commodity_PK] = Me.txtCommodity.Value
    V.Update
    Else
    End If
    Set V = Nothing

  2. #2
    knarfreppep is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Feb 2015
    Location
    Adelaide, Australia
    Posts
    106
    Won't DLookup return Null if what you look for isn't there?

    E.G.
    If IsNull(DLookup("lngID","tblItems","lngID = " & lngIDIWantToAdd)) then
    'add the ID
    End If

  3. #3
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352
    If i do:
    Code:
    If IsNull(DLookup("Commodity_PK", "tblCommodity", "Commodity_PK =" & Forms!frmAddDefect!txtCommodity.Value)) Then
    Then I get an error "The expression you entered as a query parameter produced this error:" and the it has written whatever I typed in as the thing i want to add

  4. #4
    knarfreppep is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Feb 2015
    Location
    Adelaide, Australia
    Posts
    106
    Try -

    If IsNull(DLookup("Commodity_PK", "tblCommodity", "Commodity_PK ='" & Forms!frmAddDefect!txtCommodity.Value & "'")) Then

  5. #5
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352
    Awesome Thanks!

  6. #6
    knarfreppep is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Feb 2015
    Location
    Adelaide, Australia
    Posts
    106
    Pleasure

    You need to know when you're dealing with Commodity_PK being a string field -

    "Commodity_PK = '" & Forms!frmAddDefect!txtCommodity.Value & "'" or "Commodity_PK = """ & Forms!frmAddDefect!txtCommodity.Value & """"

    as opposed to a number field -

    "Commodity_PK = " & Forms!frmAddDefect!txtCommodity.Value

  7. #7
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352
    Can you explain that a little more? Getting quite confused with all the " and ' s

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Can use apostrophe or doubled quote marks to delimit text parameters. So this:

    DLookup("Commodity_PK", "tblCommodity", "Commodity_PK ='" & Forms!frmAddDefect!txtCommodity.Value & "'")

    or this

    DLookup("Commodity_PK", "tblCommodity", "Commodity_PK =""" & Forms!frmAddDefect!txtCommodity.Value & """")

    Doubling a special character so it will be treated as text is called 'escaping' a special character.

    I prefer the apostrophe and never use doubled quote to delimit parameters.
    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. Using checkboxes to perform a search
    By zcrox69 in forum Forms
    Replies: 1
    Last Post: 05-16-2013, 01:18 PM
  2. Replies: 1
    Last Post: 03-22-2013, 03:33 AM
  3. Replies: 4
    Last Post: 05-09-2012, 07:20 AM
  4. Replies: 1
    Last Post: 08-13-2011, 12:03 AM
  5. SQL expression to perform a calculation
    By springboardjg in forum Queries
    Replies: 1
    Last Post: 05-20-2011, 06:57 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