Link to home
Start Free TrialLog in
Avatar of Hobart007
Hobart007

asked on

Access Unbound Combobox Lookup on form using 2 parameters

I know how to make a combo box using one parameter to pull records into a form but am working with a table which uses 2 fields as primary key and so I need to do something like this for both PK fields to pull up a unique record.  Is there a way to have 2 combo boxes; one with the first parameter and then the second with the second parameter to pull up the records?

Here are some specifics:

Form Source:  COMBINE - This is a query which pulls two tables, MEMINFO and PROPINFO together.  
Fields:  The query has all of the fields of both tables which are as follows:
                MEMINFO:  MEMNUM (PK), MEMNAMEL, MEMNAMEF
                PROPINFO:  MEMNUM (PK), SFX (PK), STATUSPD, PROPADD1, PROPADD2, PROPADD3,
                                    PROPADD4, FNMA

My goal here is to select the MEMNUM and SFX from the two combo boxes (one would work as well but I think that two would be a more elegant choice with SFX populating based on the record chosen in MEMNUM) and have the rest of the form populate.  The main form ill be used for display rather than data entry with a subform based on the selected record being used to enter transactions associated with these records.  I am not sure if there is a way to do this with the combo boxes alone or if a button needs to run a script once the boxes are populated but I m not adverse to either solution.

Caveat:  I know my way around Access for the most part but am a true novice when it comes to anything relating to coding.

Thank you in advance, Jim.
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

I'd add a function to your form and call that Function in the Afterupdate event of your combos:

Function FindRecord()

If Nz(Me.Combo1.Column(0),"")="" or Nz(Me.Combo2.Column(0),"")="" Then Exit Function
'/if you get to here, you have something selected in each combo:
>> run your filter/query here
End Function

Now just call that Function in the Afterupdate event of each combo:

Sub Combo1_AfterUpdate()
  Findrecord
End Sub

The function will not run unless both combos have a value.

Avatar of Hobart007
Hobart007

ASKER

This seems like it will work once I get the syntax right.  I usually cheat on SQL by building a query in design view to do what I want and then using the code Access generates.  In his case, I cut and pasted the code with minor changes to parameter names to make it fit the form and am getting a syntax error.  I have attached the code for the function below.  All of the syntax errors have been in the SQL part of this which confuses me as I used what Access generated which works well for the existing query.  Does the SQL syntax have to be modified for use here?  I am almost there and appreciate the help I've received so far.

Function FindRecord()
 
If Nz(Me.Combo62.Column(0), "") = "" Or Nz(Me.Combo64.Column(0), "") = "" Then Exit Function
'/if you get to here, you have something selected in each combo:
 
SELECT MEMINFO.MEMNUM, MEMINFO.MEMNAMEL, MEMINFO.MEMNAMEF, PROPINFO.MEMNUM, PROPINFO.SFX, PROPINFO.STATUSPD, PROPINFO.PROPADD1, PROPINFO.PROPADD2, PROPINFO.PROPADD3, PROPINFO.PROPADD4, PROPINFO.FNMA
FROM MEMINFO LEFT JOIN PROPINFO ON MEMINFO.MEMNUM = PROPINFO.MEMNUM WHERE (((PROPINFO.MEMNUM) = [Me.Combo62]) And ((PROPINFO.SFX) = [Me.Combo64]));
 
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
MEMNUM and SFX are numeric characters but the fields are formated as text as they are identifiers rather than figures for calculation.  The reason I used select is that the method I use is to make a query and hav Access write the SQL for me as I am a rank novice with SQL.  So I would replace the SQL string with the following:?

Dim rst As DAO.Recordset
Set rst = Me.RecordsetClone
rst.FindFirst "MEMNUM=" & Me.Combo62 & " AND SFX=" & Me.Combo64

If Not rst.NoMatch Then
  Me.Bookmark = rst.Bookmark
Else
  Msgbox "Record not found"
End If

Is there any other coding I would need?
I am getting the following error when I try to run what I have in the code box below:

Run-time Error '3464':

Data type mismatch in criteria expression.

I think that this might be because the MEMNUM and SFX fields are text with numeric characters and the expression might be set up to look for numbers.  I am looking for the right syntax for text right now.  Either that or I can switch these fields to number fields to make it work.
Function FindRecord()
 
If Nz(Me.Combo62.Column(0), "") = "" Or Nz(Me.Combo64.Column(0), "") = "" Then Exit Function
'/if you get to here, you have something selected in each combo:
 
Dim rst As DAO.Recordset
Set rst = Me.RecordsetClone
rst.FindFirst "MEMNUM=" & Me.Combo62 & " AND SFX=" & Me.Combo64
 
If Not rst.NoMatch Then
  Me.Bookmark = rst.Bookmark
Else
  MsgBox "Record not found"
End If
 
End Function

Open in new window