Results 1 to 7 of 7
  1. #1
    argsemapp is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    18

    Help creating queries from combobox input

    Access newbie here. I have a form with two tables for Record Sources [tbl_Composition] & [tbl_ProductRelationships], and a combobox (cmb_SelProdName) whose Row Source is a ProductName field from [tbl_ProductRelationships].

    When a user selects a choice from the combobox I'd like to query [tbl_Composition] for the ProductName (they should match) and get the entire row for that product which contains information I need to feed to a report. Here's my vba for the AfterUpdate on the combobox:

    Code:
    Private Sub cmb_SelProdName_AfterUpdate()
    
    
    Dim db As Database
    Dim rec As DAO.Recordset
    Dim RecQry As String
    
    
    Set db = CurrentDb
    RecQry = "SELECT * FROM [tbl_composition] WHERE [tbl_composition].ALLOY_DESC = " & Me.cmb_SelProdName
    
    
    Set rec = db.OpenRecordset(RecQry)
    
    
    End Sub
    This is giving me a Variable Not Defined error when I make a selection on my combobox, and I'm a bit stuck. I'm asking myself, what does "Set db=CurrentDb" do when the form has two dependencies? Any help would be greatly appreciated.

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Is tbl_composition in the current DB or is it somewhere else?

    Maybe the value of the combobox is not the correct type. Are there multiple columns in the combo? Are you trying to match a PK like a long integer or a text data type with your WHERE clause?

  3. #3
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    If "Me.cmb_SelProdName" holds a text value rather than a number, you may need:

    RecQry = "SELECT * FROM [tbl_composition] WHERE [tbl_composition].ALLOY_DESC = '" & Me.cmb_SelProdName & "'"

    What are you going to do with the recordset "rec".
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  4. #4
    argsemapp is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    18
    tbl_composition should be- it's in the Record Source of the form (SELECT tbl_composition.*, tbl_ProductRelationships.* FROM tbl_composition, tbl_ProductRelationships.

    There's just a single column in my combo. The combobox value and the associated value on the other table are both Text, and as far as I can tell they are character for character...

    However I did some cleaning up and I'm getting "Run-time Error '3464': Data type mismatch in criteria expression. Everything should be strings & be playing nicely together...

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Are you using the code example in Post # 3? CurrentDB should be fine since the table is in the Current DB. Although, if you are going to declare db and Bind it you should probably use
    Dim db As DAO.Database

  6. #6
    argsemapp is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    18
    The quotations, and calling db a DAO.Database seemed to fix the problem! (or at least stop any errors).

    If I wanted to bind the first couple contents of this new recordset to a textbox on my form what would be the easiest way to do this?

  7. #7
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    By Bind, do you mean you want to display it, or do you mean you want the user to be able to update it?

    If you want to display it, then you just need to check if there are any records, then read the first record and assign the value to the unbound text box.That looks roughly like this, although it's aircode.
    Code:
    rec.movelast 
    rec.movefirst 
    IF rec.recordcount>0 then 
    '  txtMyField = rec.Fields("MyField").value
    '       maybe the above format 
    '       or maybe the below format ->
       txtMyField = rec!MyField
    Else
       txtmyField = ""
    End if

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

Similar Threads

  1. ComboBox - Input with Key issue
    By floyd in forum Forms
    Replies: 1
    Last Post: 08-21-2013, 02:10 PM
  2. ComboBox with input mask to display dropdown
    By wetsnow13 in forum Forms
    Replies: 2
    Last Post: 06-10-2013, 05:59 AM
  3. Replies: 7
    Last Post: 10-31-2011, 02:21 PM
  4. Replies: 0
    Last Post: 02-22-2011, 04:18 PM
  5. Creating input forms for queries?
    By Brujeria5 in forum Queries
    Replies: 1
    Last Post: 08-14-2009, 08:48 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