VB.Net/Access 2007 DB Query Problem
I am brand spanking new to runtime db connections. I have a simple query problem that I can't seem to input the correct search criteria into Google find help.
Facts:
Using OleDB
runtime dev
Issue: I have a text box I want to use as the condition in which to query my Customer table and produce results in a DataGridView in VB. Everytime I execute my code I get the following: "Syntax error in string in query expression 'LastName >= (textbox contents)"
Here is my small sub executing this query:
Code:
Public Sub SearchMenu()
Dim SearchBox As String
SearchBox = frmMain.txtName.Text
Connection.Open()
dvdDataSet.Clear()
accCommand.CommandText = "SELECT * FROM Customer WHERE LastName >=' " & SearchBox
accCommand.Connection = Connection
dvdDataAdapter.SelectCommand = accCommand
dvdDataAdapter.Fill(dvdDataSet, "Customers")
frmMain.dvgFrmMain.DataSource = dvdDataSet.Tables("Customers")
End Sub
Can anyone throw me a bone on this? My Gaddis book is just completely useless on database use.
Thanks a lot.
Re: VB.Net/Access 2007 DB Query Problem
I have gotten the error reduced down to "No value given for one or more required parameters". I understand the statement but I still don't understand what I am missing in the statement. I have added everything needed. I have changed the code minimally but it still just isn't accepting it.
New code, minimal changes:
Code:
Public Sub SearchMenu()
Dim SearchBox As String
SearchBox = frmMain.txtName.Text
Connection.Open()
dvdDataSet.Clear()
accCommand.CommandText = "SELECT * FROM Customer WHERE LastName =" & SearchBox
accCommand.Connection = Connection
dvdDataAdapter.SelectCommand = accCommand
dvdDataAdapter.Fill(dvdDataSet, "Customers")
frmMain.dvgFrmMain.DataSource = dvdDataSet.Tables("Customers")
End Sub
Re: VB.Net/Access 2007 DB Query Problem
hahah Solved it myself!
Code:
accCommand.CommandText = "SELECT * FROM Customer Where LastName >= '" & SearchBox & "' ORDER BY LastName"
Re: VB.Net/Access 2007 DB Query Problem
That's correct now. :)
For an explanation and examples of delimiters to use around values within SQL statements, see the article How do I use values (numbers, strings, dates) in SQL statements? from our Database Development FAQs/Tutorials (at the top of this forum)
As you now have it sorted out, could you please do us a little favour, and mark the thread as Resolved?
(this saves time reading for those of us who like to answer questions, and also helps those who search to find answers)
You can do it by clicking on "Thread tools" just above the first post in this thread, then "Mark thread resolved". (like various other features of this site, you need JavaScript enabled in your browser for this to work).