Solved

Access Unbound Combobox Lookup on form using 2 parameters

Posted on 2008-10-13
5
327 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
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 84

Accepted Solution

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Tags from access to excel 3 28
data analyst 3 49
VBA pass value between two fields different tables 10 37
Sub Reports 8 22
This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

910 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

15 Experts available now in Live!

Get 1:1 Help Now