Solved

how to update a combo box based off values in another combo box?

Posted on 2011-02-28
3
337 Views
Last Modified: 2013-11-28
I am trying to make selecting information easier on my form so I'd like to have combo boxes that update after a selection has been made.

I have a combo box that displays names of audits. The row source has this query in it:
SELECT [tbl_Audit].[Audit_ID], [tbl_Audit].[Audit_Name] FROM tbl_Audit ORDER BY [Audit_Name];

I want a second combo box to be based off the tbl_audit.audit_id that a user selects. I want to be able to show
SELECT tbl_observation.obs_id FROM tbl_observation, tbl_audit WHERE tbl_observation.audit_ID = the value that the user selected from the combo box above

I was thinking of whether this could be done just using the after updates or before update spots in the Properties boxes or if this had to be done with VB?
0
Comment
Question by:jtovar3
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
pteranodon72 earned 500 total points
ID: 35002156
This technique is called Cascading Comboboxes. It does require using VBA. There is a great tutorial here:
http://www.fontstuff.com/access/acctut10.htm

You will need to use code in the After Update event of the first combo box. You will also need to put code into the form's On Current event so that the correct list of choices is shown for previously entered data.

HTH,

pT72
0
 
LVL 14

Expert Comment

by:pteranodon72
ID: 35002258
<Assuming that AuditID is stored as a number field (not text field, like in the above website) >

Private Sub Combo1_AfterUpdate()
If IsNull(Combo1) = False Then
    Combo2.RowSource = "SELECT tbl_observation.obs_id FROM tbl_observation, tbl_audit WHERE tbl_observation.audit_ID = " & Combo1
Else
    Combo2.RowSource = "SELECT tbl_observation.obs_id FROM tbl_observation, tbl_audit"
End If
End Sub

Private Sub Form_OnCurrent()
If IsNull(Combo1) = False Then
    Combo2.RowSource = "SELECT tbl_observation.obs_id FROM tbl_observation, tbl_audit WHERE tbl_observation.audit_ID = " & Combo1
Else
    Combo2.RowSource = "SELECT tbl_observation.obs_id FROM tbl_observation, tbl_audit"
End If
End Sub

Open in new window


If AuditID was stored as text in the underlying table, you'd need to surround its value with apostrophes.

HTH,
pT72
0
 

Author Closing Comment

by:jtovar3
ID: 35022181
PERFECT!
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

778 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