linking txt boxes to existing combo boxes

sha7jpm

Registered User.
Local time
Today, 17:59
Joined
Aug 16, 2002
Messages
205
sorry, i am sure this is totally simple.

I have two combo boxes, by selecting data on each, the txt boxes on the form will show up a particular record.

I have built the two combo boxes, and they work fine. but I cant seem to reference the txt boxes to the combo box.

the txt boxes would have to reference to both, as the 2nd combo box relies on the data in the first.

any pointers?
 
Don't know if you are VBA-aware, but you probably need some VBA to accomplish what you want. An approach I favor is to apply a filter after you have the data you need. So if the two combo boxes are called cbo1 and cbo2, then the simple approach would be to place something like the following code in the cbo2 AfterUpdate Event Procedure:

Me.Filter = "Field1 = " & Me.cbo1 & " and Field2 = " & Me.cbo2
Me.FilterOn = True

This will restrict the records displayed by the form to those records (or "that record") with Field1 having the value in cbo1, and Field2 having the value in cbo2.

This assumes both Field1 and Field2 are numeric. If they are string-type fields the first statement would instead read

Me.Filter = "Field1 = """ & Me.cbo1 & """ and Field2 = """ & Me.cbo2 & """"

If the user can go back and change cbo1 after s/he has changed cbo2 you might want to have this code built into a subroutine called by both the cbo1 and cbo2 After Update event procedures.

Further, this code and your form will break if either cbo1 or cbo2 is blank when it is executed.

A more robust approach would be to place the following at the top of the Form's event procedures (before the first event sub):

---Code Follows---
Private Sub SetFil
' Clear filter if neither cbo is set
If IsNull(Me.cbo1) and IsNull(Me.cbo2) then
Me.FilterOn = False
Exit Sub
End If

' here if either cbo1 or cbo2 or both have data
If IsNull(Me.cbo2) then ' just use cbo1 for the filter
Me.Filter = "Field1 = " & Me.cbo1
Me.FilterOn = True
Exit Sub
End If

'Here if cbo2 has data - cbo1 not yet determined
If IsNull(Me.cbo1) then
Me.Filter = "Field2 = " & Me.cbo2
Me.FilterOn = True
Exit Sub

' here when both cbo's have data
Me.Filter = "Field1 = " & Me.cbo1 & " and Field2 = " & Me.cbo2
Me.FilterOn = True
Exit Sub

End Sub

---Code Precedes---
Then in the FORM's OnOpen event procedure, and in the AfterUpdate event procedures for cbo1 and cbo2, write

Call SetFil

HTH,
Jim
 

Users who are viewing this thread

Back
Top Bottom