if i put
Set RS = DB.OpenRecordset(sqlQuery)
below
sqlQuery = "SELECT * FROM flights WHERE from ='" & cityfrom.Text & "' and to = '" & cityto.Text & "'"
it runs throuh but gets no matchs, which it should because i choose criteria that matched the db.
VB runs through your code sequentially. Trying to execute OpenRecordset(sqlQuery) before you've told VB what sqlQuery is that line doesn't work. This is a fundamental the way VB works..something you should be familiar with.
it runs throuh but gets no matchs, which it should because i choose criteria that matched the db.
First lets look at your code:
Code:
Private Sub commnext_Click()
Unload Me
Load flights
flights.Show
Dim sqlQuery As String
' search the database with the SQL string
' looking for the person with the name entered
' in the textbox
sqlQuery = "SELECT * FROM Flights WHERE from ='" & cityfrom.Text & "' and to = '" & cityto.Text & "'"
Set RS = DB.OpenRecordset(sqlQuery)
' this on error resume next will catch errors
' if there are no matches in the database
On Error Resume Next
' try and show the second field of the search
' result (0 being the first, and 1 being the second)
MsgBox RS.Fields(0).Value
' if there is nothing in the recordset, it means
' the search returned nothing. So let the user know
If Err.Number = 3021 Then
MsgBox "no match"
End If
End Sub
The first line in that sub is
Unload Me. If you've unloaded this, which is where cityfromand citytoresides, VB will not know the values of cityfrom.Text and cityto.text because they've been unloaded. Because they've been unloaded, then you've referenced the comboboxes again they get reloaded into memory and they're values will be their initial values, which are "Select A City" and "Select A City" for both comboboxes.
Some things for strongly suggested reading:
Variables in VB
Control Flow