Just beginning to experiment with the JOIN clause so I can display data from across multiple tables in a gridview. However, I can't for the life of me work out what my error is here. I've tried switching around the order of tables called, columns called and the such to no success. This, I think, is the closest to working I can get the statement. It's basically a straight copy of a supposedly working statement, yet still doesn't work.
Any help is appreciated. (I'm using VB.Net to code, Access '13 as the database (which might be part of the problem; I've read it's very finicky about syntax) and developing with VS 2013.)
@tim: as requested, here's the full sub.
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
If cbSearchType.Text = "Guest Surname" Then
Dim searchSQL As String = "SELECT bookings.bookingStartDate, bookings.bookingEndDate, guests.guestFirstName, guests.guestSurname, locations.locationName FROM bookings JOIN guests ON guests.guestID = bookings.guestID JOIN locations ON locations.locationID = bookings.locationID WHERE guests.guestSurname = @guestSurname"
Dim searchCommand = New OleDbCommand(searchSQL, globalVariables.objConnection)
Dim searchAdapter As New OleDbDataAdapter(searchSQL, globalVariables.objConnection)
Dim searchDataTable As New DataTable
searchCommand.Parameters.AddWithValue("guestSurname", tbSearchTextBox.Text)
searchAdapter.SelectCommand = searchCommand
searchAdapter.Fill(searchDataTable)
globalVariables.objConnection.Open()
searchAdapter.Fill(searchDataTable)
gvSearchResults.DataSource = searchDataTable
globalVariables.objConnection.Close()
ElseIf cbSearchType.Text = "Location" Then
ElseIf cbSearchType.Text = "Booking Start Date" Then
ElseIf cbSearchType.Text = "Booking End Date" Then
End If
End Sub
End Class
"...WHERE guests.guestSurnmae = '" + @guestSurname + "'" - i.e., ifguestSurname` is a char or varchar field, you'll need to quote it with single quotes'. The other thing to check is that you have spaces between keywords (FROM,WHERE, etc). Can you post the actual VB.NET code you're using?"@guestSurname"instead of"guestSurname"in yourParameters.AddWithValue. Not 100% sure Access supports named parameters either. Try stepping through the debugger and seeing what the text of theOleDbCommandis after you add the parameter - that should show you if it's correct or not.