Solved

Access Unbound Combobox Lookup on form using 2 parameters

Posted on 2008-10-13
5
324 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
  • 3
  • 2
5 Comments
 
LVL 84
Comment Utility
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
Comment Utility
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 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now