Results 1 to 11 of 11
  1. #1
    englisap is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2011
    Posts
    9

    using the and function in a dlookup statement


    i want to use the dlookup function to find the uniqe ID of a call in my call log.
    the issue i have is that i need the criteria to mean return ID from Call Information if Time Called field equals Time Called text box AND Date Called field equals Date Called text box AND phone number field equals phone number text box.

    the current code i have is not working i keep getting a runtime error that points to the Date called text box.
    Code:
    Dim ID As String
    ID = DLookup("[ID]", "Call Information", "[Call Date] = DateCallTXT.value AND [Time Called] = TimeCallTXT.value AND [Phone Number] = phoneTXT.value")

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    See if the multi-criteria example at the bottom here helps:

    DLookup Usage Samples

    You have everything within quotes; each of the form values needs to be outside the quotes.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    The actual syntax of the function will depend on the datatype of the field(s) in the underlying table. Values attributed to text fields must be delimited by single apostrophes while values attributable to date and time fields must be delimited by # signs. Numeric values do not require delimiters. Since the values in the form controls are essentially variables they cannot be included within double quotes otherwise Access just sees them as a fixed text value.

    Also, if you are referencing controls on a form from which your code executes, then you should use the "me." shorthand to refer to the current form.

    I would probably suggest a different variable name rather than ID. Also, is the ID field of the table a string as you have declared the ID variable? They should be the same datatype.

    Also, if a table or field name has a space in it, you must enclose it in square brackets. Your Call Information table is not enclosed in square brackets.

    Assuming that [Call Date] and [Time Called] are date/time datatypes in your [Call Information] table then this is how I structure the code:

    Code:
    Dim ID As String
    ID = DLookup("[ID]", "[Call Information]", "[Call Date] =#" & me.DateCallTXT.value & "# AND [Time Called] = #" & me.TimeCallTXT.value & "# AND [Phone Number] ='" & m.phoneTXT.value & "'") 

  4. #4
    englisap is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2011
    Posts
    9

    missing operator

    i have looked into that and i feel like i have changed this to the proper way but i still get a missing operator the new code is as follows
    Code:
    Dim ID As String
    ID = DLookup("[ID]", "Call Information", "[Call Date] = #DateCallTXT.value#" & "AND [Time Called] = TimeCallTXT.value" & "AND [Phone Number] = phoneTXT.value")

  5. #5
    englisap is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2011
    Posts
    9
    i have used your coding, jzwp11, with minor adjustments. it seems to be working but i still get an error now another error below is the complete code and the error i get.

    Run-time Error '3164': Field cannot be updated

    Not sure if it helps but this is on a onclick command of a listbox
    Code:
    DoCmd.OpenForm "Call Info"
    Forms![Call Info]![fnameTXT].Value = Forms![Customer Support Department]![fnametb].Value
    Forms![Call Info]![lnameTXT].Value = Forms![Customer Support Department]![lnametb].Value
    Forms![Call Info]![phonetxt].Value = Forms![Customer Support Department]![CallSearchResults].Column(2)
    Forms![Call Info]![DateCallTXT].Value = Forms![Customer Support Department]![CallSearchResults].Column(0)
    Forms![Call Info]![TimeCallTXT].Value = Forms![Customer Support Department]![CallSearchResults].Column(1)
    ID = DLookup("[ID]", "[Call Information]", "[Call Date] =#" & Forms![Call Info]![DateCallTXT].Value & "# AND [Time Called] = #" & Forms![Call Info]![TimeCallTXT].Value & "# AND [Phone Number] ='" & Forms![Call Info]![phonetxt].Value & "'")
    Forms![Call Info]![DepartmentTXT].Value = DLookup("[Department]", "Call Information", "[ID] = ID.value")
    Forms![Call Info]![Remarkstxt].Value = DLookup("[Remarks]", "Call Information", "[ID] = ID.value")

  6. #6
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    Code:
    Forms![Call Info]![DepartmentTXT].Value = DLookup("[Department]", "Call Information]", "[ID] = ID.value")
    Forms![Call Info]![Remarkstxt].Value = DLookup("[Remarks]", "Call Information", "[ID] = ID.value")
    I see errors in these two statements. You do not have square brackets around the table name. Also you have the ID.Value within the quotes so it is not supplying the actual value of the variable. BTW what ID value are you referring to, the ID variable you declared in the preceeding statement or an ID value from a form control?


    Out of curiosity, why would you pass so much information of the same information from one form to the other? It suggests that you have the same information in two tables which is not a good practice. Generally you would just pass the key field's value.

    What does your table structure look like for the two tables to which the two forms are bound?

  7. #7
    englisap is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2011
    Posts
    9
    the issue is that this database is based off of the coding not the program since one day we plan to convert it to straight vb

  8. #8
    englisap is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2011
    Posts
    9

    semi working

    i got the code working the last issue i have is that the dlookup always brings back the same information

  9. #9
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    On what form event are you running the code?

    The Dlookup() would only return one record that would be based on the record currently visible on the first form. For what were you hoping?

  10. #10
    englisap is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2011
    Posts
    9
    when i click on an item of the listbox i want it to pull up a form that displays the details. this works but only for the lowest of the 2 numbers im testing with.

  11. #11
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    If it works for only some of the data, that indicates that there might be an issue with the data itself. You could add a debug.print ID after the Dlookup() to see if a value is being returned via the Immediate Window.

    Does every record have a call date, call time and phone number?

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

Similar Threads

  1. Help Using Variable in DLookup Statement
    By bcmarshall in forum Access
    Replies: 9
    Last Post: 12-02-2010, 12:44 AM
  2. Format in Dlookup function
    By tpcervelo in forum Forms
    Replies: 6
    Last Post: 10-22-2010, 10:23 AM
  3. dlookup function problem
    By bdaniel in forum Programming
    Replies: 3
    Last Post: 04-26-2010, 05:55 AM
  4. I have Problem in processing Dlookup Function
    By Katada in forum Programming
    Replies: 2
    Last Post: 04-23-2006, 12:07 AM
  5. Need Help for Dlookup function
    By wasim_sono in forum Programming
    Replies: 5
    Last Post: 01-04-2006, 08:18 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