I have a form and two tables. The first table contains the form control name and field name from the second table. The second table contains several thousand records. The user selects the record they want using a combobox on the form. After selecting the record, the “After Update” event it runs the following code to populate individual controls on the form. I get error 2465 Microsoft Office Access can’t find the field ‘EmpName’ referred to in your expression on the
“If Me.Controls(rst!FieldName).Name = rstData.Fields(k).Name Then” line
Private Sub cboCaseNum_AfterUpdate()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim rstData As DAO.Recordset
Dim strSQL As String
Dim PK As Integer
Dim k As Integer
PK = Me.cboCaseNum
strSQL = “SELECT ControlName, FieldName ” & _
“FROM FormFields ” & _
“WHERE UseForForm = True ” & _
“ORDER BY FormFields.FieldName;”
Set rst = db.OpenRecordset(strSQL, dbOpenSnapshot)
strSQL = “SELECT [txtLastName] & ‘, ‘ & [txtFirstName] AS EmpName, [curDamage]+[curOtherCost] ” & _
“AS ExpPaid, curDemand, datDOB, datIncident, txtCaseNum, Company, txtJob, txtLocation, ” & _
“FROM tblInc INNER JOIN tblIncIns ON tblInc.txtCaseNum = tblIncIns.fkCaseNum ” & _
“WHERE pk_Inc= ” & PK & “;”
Set rstData = db.OpenRecordset(strSQL, dbOpenSnapshot)
Do Until rst.EOF = True
For k = 0 To rstData.Fields.Count – 1
If IsNull(rstData.Fields(k).Value) = False Then
If Me.Controls(rst!FieldName).Name = rstData.Fields(k).Name Then
Me.Controls(rst!ControlName) = rstData.Fields(k).Value
k = rstData.Fields.Count – 1
End If
End If
Next k
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
rstData.Close
Set rstData = Nothing
Set db = Nothing
Any ideas? Thanks in advance for any ideas.
Ken