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
Solved

Access Unbound Combobox Lookup on form using 2 parameters

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

791 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