Results 1 to 9 of 9
  1. #1
    stildawn is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    185

    Query criteria based on Form TextBox value

    HI All



    I have a form called Add/Edit AFR, with a textbox called Text309.

    I have a Query which is basically a mirror of my main table, but in field "FWO Number" I have this expression in the criteria:

    IIf(IsNull([Forms]![Add/Edit AFR]![Text309]),"*","Like "*"" & [Forms]![Add/Edit AFR]![Text309] & "*")

    However this doesn't seem to be working at all.

    I used to have just: "Like "*"" & [Forms]![Add/Edit AFR]![Text309] & "*"

    This one worked, however I noticed that it if Text309 was blank, it would the query would exclude all the records that didnt have an FWO Number (empty fields). This is not ideal if the Text309 is empty it should show every single record in the table.

    So I tried the IIF expression above but now no matter what I put in Text309 nothing comes up at all.

    If should be checking if Text309 is blank, if blank it should be putting "*" into the criteria which would bring up ever record (or so I'm told), if its not blank then it should be performing the Like *Text309.Value* criteria.

    Any ideas?

    Thanks

  2. #2
    stildawn is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    185
    Thinking about this, if this IIF statement above cant work, is there a way via VBA in the actual FORM to simply populate the criteria field on the Query if Text309 is entered?

    So something like this in a command button on the Add/Edit Form: (I'm making this code up by the way)

    If Me.Text309 Is Null Then
    Else
    Query[SearchQ]![FWO Number]![Criteria] = "Like "*"" & [Forms]![Add/Edit AFR]![Text309] & "*"
    End If

    So the command button would populate the criteria on the query only if Text309 had value.

    ???

    Thanks

  3. #3
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Try:
    IIf(IsNull([Forms]![Add/Edit AFR]![Text309]),"","Like "*"" & [Forms]![Add/Edit AFR]![Text309] & "*")
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  4. #4
    stildawn is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    185
    I tried that.

    When empty I get no results, and when I enter something I get an expression error. I entered "11" which is the starting number for all FWO numbers??

    Is there a way to set query field criteria via VBA like my made up example? As that would work better I think.

  5. #5
    stildawn is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    185
    Gah this is annoying. First I experimented with criteria to get a all results shown criteria. This worked for that: Is Null Or Like "*"

    This included everything in my table which is great. So then I tried putting that into the IIF statement, I tried all these versions:

    IIf(IsNull([Forms]![Add/Edit AFR]![Text309]),Is Null Or Like "*",Like "*" & [Forms]![Add/Edit AFR]![Text309] & "*")
    IIf(IsNull([Forms]![Add/Edit AFR]![Text309]),"Is Null Or Like "*"","Like "*" & [Forms]![Add/Edit AFR]![Text309] & "*"")
    IIf(IsNull([Forms]![Add/Edit AFR]![Text309]),"Is Null Or Like " & ""*"","Like " & ""*"" & [Forms]![Add/Edit AFR]![Text309] & ""*"")

    None of them worked, if Text309 was blank it came up with expression error, if it had "11" in it it just didnt show anything (even though every single FWO number has 11 in it).

    I know that: Is Null Or Like "*"

    Works by itself in the criteria, and that: Like "*" & [Forms]![Add/Edit AFR]![Text309] & "*"

    Works by itself, but I just can't put the two together in a IF statement???

  6. #6
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Have you tried:
    IIf(([Forms]![Add/Edit AFR]![Text309]) Is Null,"","Like "*"" & [Forms]![Add/Edit AFR]![Text309] & "*")
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  7. #7
    stildawn is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    185
    Just tried that one now.

    Blank results in no records shown
    "11" results in expression error (the invalid or to complex error)

  8. #8
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Maybe:
    IIf(([Forms]![Add/Edit AFR]![Text309]) Is Null,"","Like "*" & [Forms]![Add/Edit AFR]![Text309] & "*")
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  9. #9
    stildawn is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    185
    That one came up with a error when entering the criteria

    "expression is an invalid string" it wont let me save it like that.

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

Similar Threads

  1. Replies: 3
    Last Post: 07-25-2013, 05:19 PM
  2. Replies: 5
    Last Post: 11-15-2012, 03:33 PM
  3. Replies: 1
    Last Post: 10-20-2012, 12:53 PM
  4. Replies: 1
    Last Post: 12-04-2011, 06:33 PM
  5. Replies: 1
    Last Post: 10-28-2011, 02:46 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