Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"between" syntax error when searching

Status
Not open for further replies.

pauljkeenan

Programmer
Jun 19, 2006
41
TR
Hi Guys

Im getting unusual errors when tryint to search for records between two reference numbers. Here is the code for search

Private Sub cmdShowRefRecs_Click()

Dim strSQL1 As String, strOrder1 As String, strWhere1 As String

strSQL1 = "SELECT qrySearchVisaSub.Ref_No, qrySearchVisaSub.Name, qrySearchVisaSub.App_Date, qrySearchVisaSub.Issue_Date, qrySearchVisaSub.Visa_Type, qrySearchVisaSub.Fee, qrySearchVisaSub.Currency, qrySearchVisaSub.Del_Sanc, qrySearchVisaSub.Nationality, qrySearchVisaSub.DateOfBirth, qrySearchVisaSub.Passport_No, qrySearchVisaSub.Sticker_No " & _
"FROM qrySearchVisaSub"

strWhere1 = "WHERE"

strOrder1 = "ORDER BY qrySearchVisaSub.Ref_No;"

If Not IsNull(Me.fromRefNo) And Not IsNull(Me.toRefNo) Then
strWhere1 = strWhere1 & " (qrySearchVisaSub.Ref_No) between '*" & Me.fromRefNo & "*' and '*" & Me.toRefNo & "*' AND"
End If


'Remove the last AND from the SQL statment
strWhere1 = Mid(strWhere1, 1, Len(strWhere1) - 5)

'Pass the SQL to the RowSource of the listbox
Me!frmVisaSearchSub.Form.RecordSource = strSQL1 & " " & strWhere1 & "" & strOrder1
Me!frmVisaSearchSub.Form.Requery

End Sub

I think it must have something to do with the between statement itself, because when I moved the final "AND" out a space I got a syntax error, when I move it back in a space, I get "you cancelled the previous operation" error.

Is the syntax correct then for such a search?

Thanking you in advance
 
Yes I have, Ive tried most combinations at this stage but always the same runtime error 2001, you cancelled the previous operation.

Id like to meet the guy who writes the descriptions for error codes.

thanks for the tip though, something simple like that is probably the answer
 
What about this portion?
'Pass the SQL to the RowSource of the listbox
Me!frmVisaSearchSub.Form.RecordSource = strSQL1 & " " & strWhere1 & "" & strOrder1
Me!frmVisaSearchSub.Form.Requery

The part: ...strWhere1 & "" & strOrder1...

Maybe take out the & "" &
or either put a space in the ""
 
well this is where the debugger is pointing me to but I used the exact same statement for another search form and it worked fine.

I messed around with the quotes and whatnot but same ol same ol.
 
Three suggestions:

1) Comment out your error handling so you can see exactly where the code breaks
2) Put a Debug.Print strSQL line in right after your final contatenation, so you can see what your final SQL statement really looks like once it's all put together
3) Are the RefNo's alphabetic or numeric? If numeric, omit the single quote delimiters surrounding the field references

HTH,

Ken S.

 
p.s. if you've already done #1, disregard that and show us what line the break occurs on...

Ken S.
 
Thanks a million Ken,

the problem was the single quote delimiters, I thought I had removed them earlier when messing around with it but I guess not.

Cheers for that, youre a life-saver
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top