Results 1 to 13 of 13

Thread: [RESOLVED] wildcard in where part of Select

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Resolved [RESOLVED] wildcard in where part of Select

    I am building a Select Statement based on some listbox selections. When * is selected in the listbox I want it to be a wildcard. When I build it like this it doesn't work:

    "Select a from TableA where b = " & Listbox1.Text

    It doesn't work when * is the selection nor when '*' is the selection.

    Obviously I don't know how to set up the wildcard in the where part of the select statement.

    The english eqivalent of the query I want is "return all records from TableA where b is equal to anything.

    It would be tedious to just truncate the whole where part for a wildcard I think, because the real problem is more complex with several listboxes for some anded wheres. The problem presented here was watered down substantially for clarity and isolation of the specific issue on hand.

    How can I accomplish this?
    Last edited by Muddy; Mar 15th, 2007 at 10:28 AM. Reason: resolved

  2. #2
    Lively Member rasana's Avatar
    Join Date
    Jan 2007
    Location
    Mumbai, (India)
    Posts
    94

    Re: wildcard in where part of Select

    Select a from TableA where b = " & Listbox1.Text

    store selected text of listbox in variable
    i.e.
    Code:
    seltext=Listbox1.text
    now write query like

    Code:
    "select * from TableA where b='" & seltext & "'"

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: wildcard in where part of Select

    Quote Originally Posted by rasana
    Select a from TableA where b = " & Listbox1.Text

    store selected text of listbox in variable
    i.e.
    Code:
    seltext=Listbox1.text
    now write query like

    Code:
    "select * from TableA where b='" & seltext & "'"
    Thanks rasana, but that doesnt seem to work with a wildcard ( * ) ... though Im not even sure a wildcard is possible in the where part of a select ... is it?

  4. #4
    Lively Member rasana's Avatar
    Join Date
    Jan 2007
    Location
    Mumbai, (India)
    Posts
    94

    Re: wildcard in where part of Select

    yes we can use wilcard with select statement....will you please tell me what error it's giving?

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: wildcard in where part of Select

    Sorry Im being so unclear rasana ... here is the statement I want:

    Select a from TableA where b = *

    this doesnt throw an error, but returns no records as there is no field b with a * in it.

    However I want * to be a wildcard and not a search character.

    I want the above statement to return field a from all records of TableA.
    I dont want to write it like "Select a from TableA" for reasons already stated in previous posts.

    Thanks for the replies (and patience) !!

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: wildcard in where part of Select

    What kind of database is this?

    * is something you would use with Access, but % is what you would use with SQL Server.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: wildcard in where part of Select

    Quote Originally Posted by Hack
    What kind of database is this?

    * is something you would use with Access, but % is what you would use with SQL Server.
    its an Access database ... Ive tried both % and * but neither work in the context in which I am trying to use them.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: wildcard in where part of Select

    Here is the actual code (simplified a bit for clarity) ... you can see why I am trying to avoid truncating the where for "don't care" fields?


    Code:
    strSelect = "select distinct   recMain.Reg,  from  recMain inner join tfilter on tfilter.Reg = recMain.Reg " & _
                    " where tfilter.f1 = '" & List1.Text & _
                    "' and tfilter.f2 = '" & List2.Text & _
                    "' and tfilter.f3 = '" & List3.Text & _
                    "' and tfilter.f4 = '" & List4.Text & "'"

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: wildcard in where part of Select

    To set up a wildcard, use like
    Code:
    SELECT * FROM tableA WHERE b LIKE '*" & List1.Text & "' "

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: wildcard in where part of Select

    Thats works (using % for the wildcard)!!! Thanks!

  11. #11
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [RESOLVED] wildcard in where part of Select

    Really?

    That is good to know. I wasn't aware that Access supported the % sign.

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] wildcard in where part of Select

    It depends on the options, and the connection you are using.

    * is SQL-89 (what Access usually uses), and % is SQL-92 (most DBMS's, including Access if the option is set).

    I could be wrong here, but I think if you are connecting via ADO it always uses SQL-92 (as you are using Jet, rather than Access).

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: [RESOLVED] wildcard in where part of Select

    I was connecting to an mdb using ADO 2.8 ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width