Results 1 to 4 of 4
  1. #1
    offie is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2013
    Posts
    176

    Assigning different criteria depending on if textbox (in search form) is null or not

    Let's say I have textbox [text] and field [Table].[1]



    When [text] is null I want the criteria to Like "*" or is null
    When [text has a value I want it to be Like "*" & [Forms]![FormName]![text] & "*"

    I came up with
    IIF( [Forms]![FormName]![text] = Null, [Table].[1] = Like "*" or is null, [Table].[1] = Like "*" & [Forms]![FormName]![text] & "*")

    I put this into the criteria (WHERE) and it didn't work, I got no records from the query.
    Eventually I want to do the same operation on many fields, though trying it out on just one it doesn't work.

    Would I have to put it in as a field (in SELECT), but then doesn't that syntax have to be different?

    I feel super silly right now but I'm stumped,

    Thanks!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    If you want to use a parameterized query to filter records, review these tutorials:

    http://datapigtechnologies.com/flash...tomfilter.html

    http://datapigtechnologies.com/flash...earchform.html

    http://datapigtechnologies.com/flash...mtoreport.html

    However, I don't prefer parameterized query objects. I usually use WHERE CONDITION argument of DoCmd.OpenReport - in VBA code something like:
    DoCmd.OpenReport "report name", , , "[fieldname]=" & Me.textboxname

    Use delimiters in the criteria if text or date:
    "[fieldname]='" & Me.textboxname "'"
    "[fieldname]=#" & Me.textboxname "#"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Loc is offline Novice
    Windows Vista Access 2007
    Join Date
    May 2013
    Posts
    27
    Funny I was looking for the same thing and I think this answered my question because I need to do this type of search using 4 tables. I also found the information easy to follow and very well put together. Not long and drawn out like some other ones I have found.

    Thanks you for the help.

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    FYI:

    IIF( [Forms]![FormName]![text] = Null,
    You can't compare a value to NULL that way.
    NULL is not equal to anything, not even another NULL!

    NULL = NULL -> False
    NULL <> NULL -> False

    You have to use
    Code:
    IIF( IsNull([Forms]![FormName]![text]),...

    A lot of people get stuck on this...........

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 05-03-2013, 09:03 AM
  2. Replies: 7
    Last Post: 11-22-2012, 08:03 AM
  3. Instant Search with textbox in Form
    By MrBeardo in forum Queries
    Replies: 1
    Last Post: 03-28-2012, 02:08 PM
  4. Replies: 1
    Last Post: 02-29-2012, 04:46 PM
  5. Replies: 1
    Last Post: 02-23-2012, 02:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums