Results 1 to 5 of 5
  1. #1
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296

    Can DLookup be used to set a value to a specific field?

    Hey I know this is probably a dumb question but I was wondering if DLookup can be used to set a value to a specific field?
    I have a huge list of records which has 3 primary keys and a bunch of other fields attached to them. I want to find the specific record that matches all 3 of the entered keys so that I can change one of the fields attached to that record only.

    I was trying to do something like the following:


    Code:
    'StatusCriteria is defined elsewhere based on input but is always something like (Key1 = 5, Key2 = 7, Key3 = 8). I know the criteria isn't the problem because it works perfectly elsewhere in the code.
    
    Dim Status_Err As String
    Nz(DLookup("Status", "WorkTickets", StatusCriteria), Status_Err) = "COM"
    
    If Not Status_Err = "" Then
        Debug.Print "Error setting status in workticket. Check 'StatusCode' DLookup"
    End If
    But I keep getting "Runtime error: '424' Object Required" on the DLookup line. I assume this is happening because the correct syntax is supposed to be
    Code:
    Object = "COM"
    and DLookup is a function not an object.

    Am I right in thinking that? If so does that mean I should use recordset and how do I get recordset to filter to that one specific record?

    Thank you in advance.

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    I would simply use a Update SQL statement:
    Code:
    CurrentDb.Execute "UPDATE WorkTickets SET Status ='Com' Where Key1 =" & Me.Key1 & " AND Key2 = '" & Me.Key2 & "' AND Key3 = #" & Me.Key3 & "#",dbFailonError
    'I showed how to properly wrap the variables with key1 being a number, key2 a string and key3 a date
    To use your already built StatusCriteria you can build a query to include the Status field to be updated and a calculated field to replicate the StatusCriteria; then you just use that in the Update statement above:
    Code:
    CurrentDb.Execute "UPDATE qryWorkTickets SET Status ='Com' Where StatusCriteria = '" & StatusCriteria & "'",dbFailOnError
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Not the correct syntax for dlookup()
    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

  4. #4
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Gicu View Post
    I would simply use a Update SQL statement:
    Code:
    CurrentDb.Execute "UPDATE WorkTickets SET Status ='Com' Where Key1 =" & Me.Key1 & " AND Key2 = '" & Me.Key2 & "' AND Key3 = #" & Me.Key3 & "#",dbFailonError
    'I showed how to properly wrap the variables with key1 being a number, key2 a string and key3 a date
    To use your already built StatusCriteria you can build a query to include the Status field to be updated and a calculated field to replicate the StatusCriteria; then you just use that in the Update statement above:
    Code:
    CurrentDb.Execute "UPDATE qryWorkTickets SET Status ='Com' Where StatusCriteria = '" & StatusCriteria & "'",dbFailOnError
    Cheers,
    This worked perfectly! Thank you very much! Will have to keep this on my toolbelt.
    I am gonna look into dbFailonError too because I just had it wrapped in a NZ

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    You're very welcome, good luck with your project!
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

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

Similar Threads

  1. Replies: 3
    Last Post: 11-28-2016, 03:17 PM
  2. Replies: 4
    Last Post: 05-18-2016, 03:32 PM
  3. Replies: 2
    Last Post: 01-14-2015, 12:00 PM
  4. Replies: 1
    Last Post: 11-20-2014, 08:34 AM
  5. Replies: 4
    Last Post: 06-18-2013, 01:44 PM

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