Results 1 to 6 of 6
  1. #1
    tgall is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2015
    Location
    na
    Posts
    38

    DLookup and SQL statement for Control Source

    Short Trip: Neither of the following work as a Control Source for an unbound text box. What am I doing wrong?

    The following DLookup runs but gives a #Name? error:
    =Nz(DLookUp("ACMinQuant","dttblAcceptanceCriteria" ,(("RepositoryID = " & "[Forms]![frm02EnterTestData]![RepositoryID]") AND ("MeasAttribute = "FlowRateLPMaxcc"))))

    The following SQL, which is a copy and paste from a query, does not run in the Control Source and gives a "The syntax of the subquery in tis expression in incorrect." error:
    SELECT dttblAcceptanceCriteria.RepositoryID, dttblAcceptanceCriteria.MeasAttribute, dttblAcceptanceCriteria.ACMinQuant
    FROM dttblAcceptanceCriteria
    WHERE (((dttblAcceptanceCriteria.RepositoryID)=[Forms]![frm02EnterTestData]![RepositoryID]) AND ((dttblAcceptanceCriteria.MeasAttribute)="FlowRate LPMaxcc"));

    Scenic View: On a form, I'm using an unbound text box to hold a number. The number is extracted from dttblAcceptanceCriteria:

    MEASATTRIBUTE ... ACMINQUANT ... ACMAXQUANT ... REPOSITORYID
    FlowRateLPMaxcc .. 7.2 .................. 30 ................... 184
    FlowRateUPMaxcc .. 6.2 .................. 25 ................... 184



    I first tried the following DLookup. It runs, but the result is #Name? and not 7.2. The function doesn't like the AND ~~~~
    =Nz(DLookUp("ACMinQuant","dttblAcceptanceCriteria" ,(("RepositoryID = " & "[Forms]![frm02EnterTestData]![RepositoryID]") AND ("MeasAttribute = "FlowRateLPMaxcc"))))

    So, I tried SQL code. I built the code in a query, which runs. But, when I copy the SELECT statement into the Control Source, the following pithy error message pops up:
    The syntax of the subquery in this expression incorrect.
    SELECT dttblAcceptanceCriteria.RepositoryID, dttblAcceptanceCriteria.MeasAttribute, dttblAcceptanceCriteria.ACMinQuant
    FROM dttblAcceptanceCriteria
    WHERE (((dttblAcceptanceCriteria.RepositoryID)=[Forms]![frm02EnterTestData]![RepositoryID]) AND ((dttblAcceptanceCriteria.MeasAttribute)="FlowRate LPMaxcc"));

    What am I doing wrong?

  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,652
    You can't have SQL as the control source. See if this helps with the DLookup():

    http://access.mvps.org/access/general/gen0018.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    You have the forms!... reference inside quotation marks, which causes Access to interpret it as text. Try this:

    =Nz(DLookUp("ACMinQuant","dttblAcceptanceCriteria" ,"RepositoryID = " & [Forms]![frm02EnterTestData]![RepositoryID] & " AND MeasAttribute = 'FlowRateLPMaxcc'"))

    Note the single quotes around FlowRateLPMaxcc.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    And there's your fish.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    tgall is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2015
    Location
    na
    Posts
    38
    Paul, thank you for responding. The models did not help. I have lots of models and they confuse me:
    * always use NZ; no, NZ is unnecessary
    * use braces; no, use double quotes
    * use double quotes; no, use single quotes enclosed in double quotes
    * use bangs; no, use periods
    * use quotes for table but braces for field
    * ensure there's space before and after the = sign; no, Access will insert the correct spaces
    * if it's Wednesday but the moon is full, use squiggly braces otherwise use ancient Sumerian

    I believe it's the AND ~~~ portion that is the discombobulation, but I don't know what Access wants.
    When I set this up in a query, I get the correct answer (see SQL above).

    When I remove the AND~~~~ portion, a number appears (7.2). But when I set up an unbound text
    box for the second MeasAttribute for RepositoryID = 184 the number is 7.2 (not 6.2). I'm assuming
    DLookup is extracting the first instance it finds.

    So, I gotta have the AND~~~~~ portion. What does Access want?

  6. #6
    tgall is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2015
    Location
    na
    Posts
    38
    John, thank you!!! The code works. I'm dancin' an Irish jig.

    You've done so much, but I have to call upon you again, please.
    I need to learn when to use bangs and periods and braces and double-quotes and single-quotes.
    Do you know of a website that explains that (video tutorials are not good websites
    for me -- I need documentation and examples).

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

Similar Threads

  1. Dlookup as control source on continuous form.
    By Ramun_Flame in forum Programming
    Replies: 4
    Last Post: 10-24-2012, 10:26 AM
  2. DLookup in Control Source
    By bgephart in forum Forms
    Replies: 2
    Last Post: 08-28-2012, 02:06 PM
  3. DLookup() for Text Box Control Source
    By tylerg11 in forum Forms
    Replies: 3
    Last Post: 02-06-2012, 02:21 PM
  4. Dlookup as Control Source
    By alsoto in forum Forms
    Replies: 1
    Last Post: 08-28-2011, 07:05 PM
  5. IIF statement in control source of text box
    By LilMissAttack in forum Reports
    Replies: 11
    Last Post: 08-18-2011, 10:02 PM

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