I have a form with several data entry boxes (text, combo, check boxes, and option groups). The name of the data entry boxes match the field names of the corresponding table that the data will be appended to/ drawn from.
When the user selects a value from a combo box, a sql is executed that gathers the data meeting the criteria and auto populates a bunch of fields in the form. My real form has many fields in it and I will be running similar functions throughout (e.g. writing data to table), I would like to create a loop to perform the field matching instead of listing each form data entry box equaling its corresponding record source.
Using this test example, how can I alter my script to a loop instead:
Code:
Private Sub txt_ID_AfterUpdate()
Dim db As Database
Dim rs As DAO.Recordset
Dim strSQL As String
strSQL = "SELECT Table1.ID, Table1.Color, Table1.Make, Table1.Model, Table2.FName, Table2.LName FROM Table1 INNER JOIN Table2 ON Table1.ID = Table2.ID WHERE"
strSQL = strSQL & "[Table1].ID = """ & Me.txt_ID & """"
Set rs = Currentdb.OpenRecordset(strSQL, DB_OPEN_DYNASET)
Me!ID = rs!ID
Me!Color = rs!Color
Me!Make = rs!Make
Me!Model = rs!Model
Me!FName = rs!FName
Me!LName = rs!LName
rs.Close
Set rs = Nothing
End Sub
I want it to do something like this:
Code:
Private Sub txt_ID_AfterUpdate()
Dim db As Database
Dim rs As DAO.Recordset
Dim strSQL As String
strSQL = "SELECT Table1.ID, Table1.Color, Table1.Make, Table1.Model, Table2.FName, Table2.LName FROM Table1 INNER JOIN Table2 ON Table1.ID = Table2.ID WHERE"
strSQL = strSQL & "[Table1].ID = """ & Me.txt_ID & """"
Set rs = Currentdb.OpenRecordset(strSQL, DB_OPEN_DYNASET)
For each FieldName in FormA
Me!<FieldName> = rs!<FieldName>
Next FieldName
rs.Close
Set rs = Nothing
End Sub
Many thanks in advance.