2

This is the main code showing that every time I search data type mismatch in criteria expression and method refresh of object IADODC failed this was showing

Private Sub gosearch_Click()
    adogrid.RecordSource = "Select *from StudentsInformation where IDNo='" + Text1.Text + "' or Name='" + Text1.Text + "'"
    adogrid.Refresh
    If adogrid.Recordset.EOF Then
        MsgBox "Record Not Found, Enter any other IDNo or Name", vbCritical, "Message"
    Else
        adogrid.Caption = adogrid.RecordSource
    End If
End Sub
Étienne Laneville
  • 4,697
  • 5
  • 13
  • 29
Kay _
  • 21
  • 1

1 Answers1

1

Looks like you are trying to lookup people by name or some numeric ID. If IDNo is defined as a numeric field in your database, you will always get an error when searching by name. You should first check if the content of Text1 is numeric and use a different query:

If IsNumeric(Text1.Text) Then
    adogrid.RecordSource = "Select * from StudentsInformation where IDNo=" + Text1.Text
Else
    adogrid.RecordSource = "Select * from StudentsInformation where Name='" + Text1.Text + "'"
End If

Concatenating a SQL statement like this is not recommended (see Why reason in How and Why to Use Parameterized Queries). Look at the following question regarding converting this code to use parameterized queries: VBA, ADO.Connection and query parameters.

Étienne Laneville
  • 4,697
  • 5
  • 13
  • 29