Results 1 to 7 of 7
  1. #1
    NightWalker's Avatar
    NightWalker is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2016
    Location
    Midwest USA
    Posts
    253

    Thumbs up Needing an explaination on queries please.

    I have these 2 queries, the first one didn't work. It would give me an "Enter Parameter value" msgbox when I clicked the second cbo. After entering the same item I selected into the msgbox I would get the proper results in the second cbo. I don't understand why the second qry works and the first doesn't. Could someone please explain why the WHERE sections are different?




    Thank you for your time and knowledge
    Walker

    Code:
    Private Sub cboTestItemType_AfterUpdate()
         'Sets the Item Part Number cbo to be limited by Test Item Type
    Me.cboProductionItemPartNumber.RowSource = "SELECT tbl_ProductionItem.ProductionItemPartNumber, tbl_ProductionItem.ProductionItem " & _
                                                    "FROM tbl_ProductionItem " & _
                                                    "Where ProductionItemType = " & Nz(Me.cboTestItemType) & _
                                                    " order by tbl_ProductionItem.ProductionItem"
    
        'Refresh cbo
        Me.cboProductionItemPartNumber.Requery
    
    End Sub
    Code:
    Private Sub cboTestItemType_AfterUpdate()
        Me.cboProductionItemPartNumber.RowSource = "SELECT tbl_ProductionItem.ProductionItemPartNumber, tbl_ProductionItem.ProductionItem " & _
                                                    "FROM tbl_ProductionItem " & _ 
                                                     "WHERE (((tbl_ProductionItem.ProductionItemType)=[forms]![frm_DailyTested].[cboTestItemType])) " & _
                                                    "GROUP BY tbl_ProductionItem.ProductionItemPartNumber, tbl_ProductionItem.ProductionItem ORDER BY tbl_ProductionItem.ProductionItem"
        Me.cboProductionItemPartNumber.Requery
    End Sub
    Last edited by NightWalker; 07-28-2016 at 11:47 AM. Reason: fix typo

  2. #2
    NightWalker's Avatar
    NightWalker is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2016
    Location
    Midwest USA
    Posts
    253
    Can anyone explain why the WHERE statement in the first qry didn't work compared to the second one. I figured out how to fix it myself but I don't understand why it makes a difference.

    Thank you for any help you can give me.

  3. #3
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Are you saying that your second query(below) worked without giving an error????

    Code:
    Private Sub cboTestItemType_AfterUpdate()
        Me.cboProductionItemPartNumber.RowSource = "SELECT tbl_ProductionItem.ProductionItemPartNumber, tbl_ProductionItem.ProductionItem " & _
                                                    "FROM tbl_ProductionItem " & _ 
                                                     "WHERE (((tbl_ProductionItem.ProductionItemType)=[forms]![frm_DailyTested].[cboTestItemType])) " & _
                                                    "GROUP BY tbl_ProductionItem.ProductionItemPartNumber, tbl_ProductionItem.ProductionItem ORDER BY tbl_ProductionItem.ProductionItem"
        Me.cboProductionItemPartNumber.Requery
    End Sub

    NZ()

  4. #4
    NightWalker's Avatar
    NightWalker is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2016
    Location
    Midwest USA
    Posts
    253
    Yes and I don't understand why. I thought Me. was the same as [forms]![frm_DailyTested]. I kept getting an enter parameter msgbox with the first qry.

  5. #5
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Sort of. You'll notice that you have quotation marks surrounding the sql

    Code:
    "SELECT tbl_ProductionItem.ProductionItemPartNumber, tbl_ProductionItem.ProductionItem " & _
             "FROM tbl_ProductionItem " & _ 
             "WHERE (((tbl_ProductionItem.ProductionItemType)=[forms]![frm_DailyTested].[cboTestItemType])) " & _
             "GROUP BY tbl_ProductionItem.ProductionItemPartNumber, tbl_ProductionItem.ProductionItem ORDER BY tbl_ProductionItem.ProductionIt
    When you enclose a string in quotes, that makes it a constant/literal string. So, because this
    [forms]![frm_DailyTested].[cboTestItemType]
    is inside the quotes, it is considered a literal/constant.

    In overview, when vba is processed, it looks for constants/literals, and it looks for expressions to be evaluated. You wanted
    [forms]![frm_DailyTested].[cboTestItemType] to be evaluated, and the result placed within the SQL string. But you enclosed it in quotes, so vba/Access did not try to evaluate it(also referred to as rendering the expression).
    Bottom line is that SQL did not do what you expected because of the quotes. In fact, [forms]![frm_DailyTested].[cboTestItemType] was ignored from SQL rendering, but might have been interpreted as a missing parameter.

    To get the SQL to work the way you intended --get the values of [forms]![frm_DailyTested].[cboTestItemType].

    Code:
     Me.cboProductionItemPartNumber.RowSource = "SELECT tbl_ProductionItem.ProductionItemPartNumber, tbl_ProductionItem.ProductionItem " & _
      "FROM tbl_ProductionItem " & _ 
     "WHERE (((tbl_ProductionItem.ProductionItemType)='" & [forms]![frm_DailyTested].[cboTestItemType])) & "' " & _
      "GROUP BY tbl_ProductionItem.ProductionItemPartNumber, tbl_ProductionItem.ProductionItem ORDER BY tbl_ProductionItem.ProductionItem"
    Here the [forms]![frm_DailyTested].[cboTestItemType] is now outside of the quotes and will be evaluated/rendered before substituting in the SQL.

    In my view you could use Me.cboTestItemType interchangeably with [forms]![frm_DailyTested].[cboTestItemType]. The quotes were the issue as I see things.
    Last edited by orange; 07-29-2016 at 07:41 AM. Reason: spelling

  6. #6
    NightWalker's Avatar
    NightWalker is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2016
    Location
    Midwest USA
    Posts
    253
    Ok so I was on the right track with the first query I just didn't have the quotes around Me.cboTestItemType properly. I would like to Thank you greatly for the datapig videos I found in another one of your replies to someone else. I watched those and built the query through the query builder then cut and pasted it into my VBA code for the proper event and now works like a charm.

    I try not to post questions here without exhausting every other option. Thank you again for explaining that to me and the datapig video link.

  7. #7
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Glad the posts and links were helpful to you.
    Good luck with your project(s).

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

Similar Threads

  1. Beginner needing help!
    By AussieAsh in forum Access
    Replies: 2
    Last Post: 01-23-2013, 11:20 AM
  2. novice needing some help
    By Goli1984 in forum Programming
    Replies: 2
    Last Post: 01-26-2012, 01:54 PM
  3. Needing help please......
    By Kristena in forum Access
    Replies: 4
    Last Post: 01-20-2012, 07:35 AM
  4. Needing help with the below mentioned
    By FCollazo in forum Access
    Replies: 3
    Last Post: 10-28-2011, 04:18 PM
  5. Hey, Needing some help
    By natbatgirl in forum Access
    Replies: 1
    Last Post: 08-18-2011, 11:25 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