4
Reply

Use Variable in query

David Carter

David Carter

Aug 24 2010 2:48 AM
3.9k

I have an OleDbAdapter attached to a datagridview.
I wish to take the value in a textbox to use in a query to fill the datagridview.
Eg. The following works: David Carter in this instance is typed into the Query as a constant.
OleDbDataAdapter1.SelectCommand.CommandText =
"Select * From InfringementRecords Where([INFStudent] = 'David Carter')"
But I don't want David Carter to be fixed in the query, I want to use a variable as entered into a textbox
Dim SelectName as string
SelectName = Textbox1.text
OleDbDataAdapter1.SelectCommand.CommandText =
"Select * From InfringementRecords Where([INFStudent] = " & SelectName & ")"
That gives me an error:
Syntax error (missing operator) in query expression '([INFStudent] = David Carter)'.
Does anyone know how to use a variable in a query?
Visual Studio 2008.
Regards,
David
 

Answers (4)