Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Access Unbound Combobox Lookup on form using 2 parameters

Posted on 2008-10-13
5
Medium Priority
?
339 Views
Last Modified: 2013-11-28
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.
0
Comment
Question by:Hobart007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 85
ID: 22707619
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.

0
 

Author Comment

by:Hobart007
ID: 22710062
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

0
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 1500 total points
ID: 22710480
What are you doing with the SELECT statement? Do you want to use this to set your form's Recordsource? This probably isn't a great idea, since it will return (supposedly) only 1 record, but you want:

Me.Recordsource = "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] & "));"

If you want to simply find and move to the record, do this

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

Are MEMNUM and SFX Numeric values?
 
0
 

Author Comment

by:Hobart007
ID: 22716481
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?
0
 

Author Comment

by:Hobart007
ID: 22717292
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

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question