I am trying to do a search using an ID to find the information that I need. The database is Access and the ID is an autonumber value(primary key). I am using Visual Studio 2022(visual basic) to to the search an it works if I do the search using the name but if I use the ID it give me an error. Can you help me solving this problem?
Error message:
System.Data.OleDb.OleDbException: data type mismatch in criteria expression
This is my code:
Dim consulta As String
Dim oda As New OleDbDataAdapter
Dim ods As DataSet
Dim registro As Byte
If ComboBox2.Text <> " " Then
consulta = "select *from QAtracker where ID='" & TextBox55.Text & " '"
oda = New OleDbDataAdapter(consulta, Conexion1)
ods = New DataSet oda.Fill(ods, "QAtracker")
registro = ods.Tables("QAtracker").Rows.Count
If registro <> 0 Then
DataGridView1.DataSource = ods
DataGridView1.DataMember = "QAtracker"
TextBox55.Text = ods.Tables("QAtracker").Rows(0).Item("ID")
TextBox5.Text = ods.Tables("QAtracker").Rows(0).Item("Login")
TextBox6.Text = ods.Tables("QAtracker").Rows(0).Item("Logout")
else
MsgBox("Codigo no existe")
Conexion1.Close()
End If
End If
Any ideas?
Thanks for your help
I tried doing to search with another Value and it worked. I would love to know what am I doing wrong.
Since the ID is a number and not a string, you should remove the quotation marks.
Please note that your code is not secure against SQL injection. While the val() function works well when searching for numbers, it always outputs a number. However, exercise caution when dealing with strings, as you need to escape special characters to enhance security.