Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filling fields on form based on value of ComboBox 1

Status
Not open for further replies.

BSSME

MIS
Dec 8, 2005
16
US
I’m trying to get certain fields within my form to populate from a table based on another control’s value. For example, when a user selects a county from a drop-down the fields for district number and job type should automatically fill with information that pertains to that county on the form.

I have tried the dlookup in the text boxes but I keep getting errors. I was originally putting this in the Control Source but then I realized that is not correct. So I am at a loss as to what to do. Any ideas?

Combo Box County Selected
District number field populate from the same table based on County
 
You need to write dynamic query.
You can try the following code.....
Event is Country_AfterUpdate
I assume District_Number to be a combo box too.

Private Sub Country_AfterUpdate()
IF isnull(me.Country)=True or LEN(TRIM(Me.Country)) = 0 Then
me.District_Number.RowSource = "SELECT Distinct District_Number From <<Table Name>> ORDER BY District_Number"
ELSE
me.District_Number.RowSource = "SELECT Distinct District_Number From <<Table Name>> WHERE Country = '" & me.Country & "' ORDER BY District_Number"
EndIf
me.District_Number.Requery
End Sub

Hope this helps you....
In case there is only one District_Number then the code will be different.
 
That helps tremendously! This is what I have so far from the code you gave.

****Begin Code****
Private Sub County_AfterUpdate()
If IsNull(Me.County) = True Or Len(Trim(Me.County)) = 0 Then
Me.DistrictNum.RowSource = "SELECT Distinct TaxCode From Counties ORDER BY TaxCode"
Else
Me.DistrictNum.RowSource = "SELECT Distinct TaxCode From Counties WHERE Abbrev = '" & Me.County & "' ORDER BY TaxCode"
End If
Me.DistrictNum.Requery
End Sub
****End Code****
What if I'm using a text box instead? I'm guessing you just reassign the value by adding variables and running the query from code to populate the variable for the text box?

Your help gave me a wonderful start and I'm sure I can figure out the rest on my own with a little nudge.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top