Hello guys!

Can you help me this type of error?

Run-time error '8574':

The object 'Employee ID:' was not found

This is the code:

Private Sub cmdPreview_Click()

Dim rsq As New Recordset

rsq.Open "SELECT * FROM qRecord where EmpID = " & Val(txtEmpID.Text) & "", con, adOpenStatic, adLockReadOnly, addUnknown
    
If rsq.EOF = False Then
    MsgBox "No records to view.", vbInformation, "No Records"
Else

    With drTrans.Sections(2)
        .Controls(Label1.Caption) = rsq.Fields("EmpID")
        .Controls(Label2.Caption) = rsq.Fields("LastName") & "," & rsq.Fields("FirstName")
        .Controls(Label3.Caption) = rsq.Fields("Position")
        
    End With
    
    Set drTrans.DataSource = rsq
    
    drTrans.Show
End If
End Sub

Dani AI

Generated

Likely cause and quick summary — the runtime error indicates the code is looking for a control named "Employee ID:" instead of the control's design-time name. In 's snippet the call into the report's Controls collection uses a label's Caption as the key, so the report tries to find a control literally named "Employee ID:". In addition the EOF test is inverted and the report should be bound (DataSource set) before attempting to populate its controls. Echoing , bind the report first; echoing , avoid SELECT * and explicitly request the columns the report expects.

Suggested corrected pattern (adapt control names to the ones used in the report):

Dim rsq As New ADODB.Recordset
rsq.Open "SELECT EmpID, LastName, FirstName, Position FROM qRecord WHERE EmpID = " & Val(txtEmpID.Text), con, adOpenStatic, adLockReadOnly

If rsq.EOF Then
    MsgBox "No records to view.", vbInformation, "No Records"
    rsq.Close: Set rsq = Nothing
    Exit Sub
End If

Set drTrans.DataSource = rsq

With drTrans.Sections(2)
    .Controls("LabelEmpID").Caption = CStr(rsq.Fields("EmpID").Value)
    .Controls("LabelName").Caption = rsq.Fields("LastName").Value & ", " & rsq.Fields("FirstName").Value
    .Controls("LabelPosition").Caption = rsq.Fields("Position").Value
End With

drTrans.Show

Troubleshooting checklist: confirm the DataReport control names in the designer and use those names (not their Caption) when indexing .Controls; verify the SQL returns the named fields (use explicit column list); use Debug.Print or loop rsq.Fields to inspect field names at runtime if mismatches appear; if the report was created with a DataEnvironment, ensure DataMember/DataSource expectations match the runtime recordset. Finally, close and release the recordset after use and prefer parameterized/Command-based queries rather than concatenating user input into SQL.

Recommended Answers

All 2 Replies

well I can't see anything referring to an object Employee ID but I am thinking you probably need to move line 18 above line 11. not much point in trying to use drTrans until you have assigned it a value.

Assuming this is a DataReport, this type of error usually occurs when there is a mismatch between the DataEnvironment that was initially used to create the report, and the RecordSource that is used to populate the report at runtime. You should verify that you're using the right column names.

As a general rule, it's a good idea to explicitly name the columns in your SQL statement rather than use the wildcard '*' to return the columns you need. That way you can verify that the column names you're expecting match what the DataEnvironment originally specified.

Hope that helps! Good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.