1

I am trying to figure out why the following code wont return anything from [Card Lookup 1], 2 or 3. I know some very basic SQL and virtually no VBA. I appreciate any assistance.

Private Sub btnSearch_Click()
Dim SQL As String
    SQL = "SELECT [Master List].[First Name], [Master List].[Last Name], [Master List].[Card Lookup 1], [Master List].[Card Lookup 2], [Master List].[Card Lookup 3], [Master List].ID " _
    & "FROM [Master List]" _
    & "WHERE [First Name] LIKE '*" & Me.txtkeywords & "*' " _
    & "OR [Last Name] LIKE '*" & Me.txtkeywords & "*' " _
    & "OR [Card Lookup 1] LIKE '*" & Me.txtkeywords & "*' " _
    & "OR [Card Lookup 2] LIKE '*" & Me.txtkeywords & "*' " _
    & "OR [Card Lookup 3] LIKE '*" & Me.txtkeywords & "*' " _
    & "OR ID LIKE '*" & Me.txtkeywords & "*' " _
    & "ORDER BY [Master List].[Last Name] "
    
    Me.subUserSearch.Form.RecordSource = SQL
    Me.subUserSearch.Form.Requery

End Sub
[First Name] = Short Text
[Last Name] = Short Text
[Card Lookup 1] = Number
[Card Lookup 2] = Number
[Card Lookup 3] = Number
[ID] = AutoNumber

Expected: User inputs name or number associated with a card Returns match based on name or number entered. In the original code it returns every field fine except Card Lookup 1, 2, or 3 Upon changing the wildcard symbol, it fails to return any data.

Card Lookup 1,2 and,3 are assigned IDcard Numbers.

3
  • Please provide sample data of Card Lookups for minimal reproducible example and value used in Me.txtkeywords. Commented May 17, 2021 at 17:56
  • Are the "problem" fields numeric? stackoverflow.com/questions/1108171/… & "OR [Card Lookup 1] & '' LIKE '*" & Me.txtkeywords & "*' " _ Commented May 17, 2021 at 18:04
  • @TimWilliams Yes they are numeric. Commented May 17, 2021 at 18:16

2 Answers 2

1

There is a space missing:

& "FROM [Master List]" _
& "WHERE [First Name] LIKE '*" & Me.txtkeywords & "*' " _

... results in:

FROM [Master List]WHERE [First Name] LIKE '*foo*'

Insert space here:

& "FROM [Master List] " _

Do you know the debug console in VBA editor? For debugging the first step is always looking at the results:

debug.print SQL

Also, you might want to add error handling so you can see the actual error message causing your empty results in the debug console:

Private Sub xxx()
on error goto fErr

' query action here

fExit:
' do your cleanups here
exit sub

fErr:
debug.print err.description
resume fExit
End Sub
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for seeing the space. I have no idea about the debug console. First time really using Access...
@JamesFleming if you liked this answer, please consider accepting it. Thanks!
1

As noted here: How to use LIKE condition in SQL with numeric field?

You can't use Like with a numeric field without casting the value to a string (eg. by concatenating its value with '')

Private Sub btnSearch_Click()
Dim SQL As String
    SQL = "SELECT [Master List].[First Name], [Master List].[Last Name], [Master List].[Card Lookup 1], [Master List].[Card Lookup 2], [Master List].[Card Lookup 3], [Master List].ID " _
    & "FROM [Master List] " _
    & "WHERE [First Name] LIKE '*" & Me.txtkeywords & "*' " _
    & "OR [Last Name] LIKE '*" & Me.txtkeywords & "*' " _
    & "OR [Card Lookup 1] & '' LIKE '*" & Me.txtkeywords & "*' " _
    & "OR [Card Lookup 2] & '' LIKE '*" & Me.txtkeywords & "*' " _
    & "OR [Card Lookup 3] & '' LIKE '*" & Me.txtkeywords & "*' " _
    & "OR ID LIKE '*" & Me.txtkeywords & "*' " _
    & "ORDER BY [Master List].[Last Name] "
    
    Me.subUserSearch.Form.RecordSource = SQL
    Me.subUserSearch.Form.Requery

End Sub

6 Comments

Thank you. I understand how that is supposed to work. After applying the changes. I enter "371". It correctly returns values in the ID field that contain "371" but still does not return anything from the Card Lookup fields.
I'm not an access user, so maybe there's something specific to your "lookup" fields which is an issue here? Are they just "regular" numeric fields?
In design view they are listed as number with a field size of long integer.
Are you using keywords match the "id" of your lookup, or the "value" ?
I dont think I fully understand the question. However, I think it should be the value.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.