Results 1 to 13 of 13
  1. #1
    Gary is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Posts
    14

    Use an iif statement to deal with null values in criteria boxes

    Hey All,



    I did some research on this but never found a satisfactory answer.

    Why is it that in the query builder when I enter the following into the criteria box it doesn't deal with the null in the combo

    iif( isnull(forms!formname!combobox), is not null ,forms!formname!combobox)

    shouldnt this return any feilds that are not null???

    Thanks in advance,

    Gary

  2. #2
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    A query is looking at a field in a table. So if you want to find records where a certain field is null then you have to reference the field not the combo box.


    WHERE field1 is not Null

    You can use a form control to supply values to a field used in the criteria in a query

    WHERE field2=forms!formname!combobox

    Also this function is not valid, since "is not null" does not represent a valid value or expression that can be returned by the IIF() function.

    iif( isnull(forms!formname!combobox), is not null ,forms!formname!combobox)

  3. #3
    Gary is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Posts
    14

    so then..

    all I want is a simple way to display all records that are not null if a combo box value is null. if the combo box is not null i want to display all records with the value in the combo box.

    I have even tried using the following

    iif( isnull(forms!formname!combobox), not forms!formname!combobox ,forms!formname!combobox)

    I just need something to put in the spot for if the is null statement is true

    is there any function that I can use to do this?

    -Gary

  4. #4
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    This might work for you. You'll have to supply your specific names :

    IIf(IsNull(forms!formname!combobox), True, table1.field1 = forms!formname!combobox);

  5. #5
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    I am not an expert as jzwp11 but I would have done some thing like this:
    Use CreateQueryDef to Dynamicaly create my Query

    Here is the Code:

    Private Sub Command5_Click()
    On Error GoTo Err_Command5_Click
    Dim strSQl As Object
    Dim strDropQry As String
    Dim strCriteria As String
    If IsNull(Me.Combo0) Then
    strCriteria = "MemberName Like '*'"
    Else
    strCriteria = "MemberName='" & Me.Combo0 & "'"
    End If

    strDropQry = "Drop table MyQuery"
    On Error Resume Next
    CurrentDb.Execute strDropQry, dbFailOnError

    Set strSQl = CurrentDb.CreateQueryDef("MyQuery", "Select * From tblmember Where " & strCriteria)

    DoCmd.OpenQuery "MyQuery"

    Exit_Command5_Click:
    Exit Sub

    Err_Command5_Click:
    MsgBox Err.Description
    Resume Exit_Command5_Click

    End Sub

    I have used strCriteria to determine the criteria of the query when the ComboBox 0 is Null I have used criteria Like '*'

    When the combo box has a value that is taken then as the criteria to create the query.

    I have used a drop table sql to drop the Existing MyQuery this assures that Myquery is always according to the latest Criteria

    Hope this helps

  6. #6
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    maximus,

    I am not an expert by any means. In fact, I was thinking along the same route as you since I would probably prefer the code solution. But before posting a similar code, I went ahead and did some internet searching. I actually found the solution on another site. I did check it in a test database, and it appears to work.

  7. #7
    Gary is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Posts
    14
    hi jzwp11,

    I tried to use the solution you posted:

    IIf(IsNull(forms!formname!combobox), True, table1.field1 = forms!formname!combobox);


    in the criteria for the query builder and when the query was run the variable box popped up asking what table1.field1 was supposed to be.

    when I removed table1.field1 I got a error saying the query was too complicated to be solved.

    I will try maximus' solution next.

    -Gary

  8. #8
    Gary is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Posts
    14
    JUST GOT IT!

    instead of using the IIF I did the following

    Like NZ([forms]![Form1]![comboBox],"*")

    Works Beautifully!!

    Thanks for all your help guys!

    -Gary

  9. #9
    Gary is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Posts
    14
    How do I mark this as solved??

  10. #10
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    table1.field1 represents whatever table/field that you are using to compare the combo box value to. You would have to substitute your own table/field name here.

    I could not find how to mark a thread as solved. It might only be an option for the original poster.

  11. #11
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    you can mark the thread solved by clicking on thread tools on the top right hand side of you screen.

    To learn how to do it follow this link.

    https://www.accessforums.net/forum-s...lved-1828.html

    I am glad that the problem is solved and in a much simpler manner.

  12. #12
    Gary is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Posts
    14
    thanks Maximus, I guess I just wasn't looking hard enough!

    the only problem with the solution is that it doesn't deal with null values in the table I am querying. now that I think of it I can probably do something like:

    LIKE NZ([forms]![Form1]![comboBox],"*" or IsNull)

    If that works I will post to let you know.

  13. #13
    smartie is offline Novice
    Windows XP Access 2003
    Join Date
    Dec 2011
    Posts
    1

    null combo box > display all

    Hello, I believe my issues are related to your threads.

    I have a form with 1 unbound combo box. The row source is pulling it from the tblOffices, which shows a list of all of our offices within the company.

    I have 1 command button, the event procedure (on click) opens a report (do cmd) "rptEvaluations". The rptEvaluations is based on the qryEmpEvaluations. The query has the following fields tblEmployee.office, tblEmployee.name, tblEmployee.EvalType, tblEmployee.EvalDate

    So basically I want the user to select an office and click the button to report on every employee for that specific office space.

    The criteria I have entered into the qryEmpEvaluations.Detachment is: "[Forms]![frmReports]![cboOffice]"
    This code works, it brings up the report and only displays the employees for the office specified.

    I would like it to display all offices and all employees if the combo box is left null.

    thanks,
    dave

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

Similar Threads

  1. Replies: 1
    Last Post: 03-27-2010, 06:13 AM
  2. Null Values in query
    By LesleaOH in forum Queries
    Replies: 0
    Last Post: 10-19-2009, 04:45 PM
  3. nested if statement with two criteria
    By kendra in forum Queries
    Replies: 5
    Last Post: 06-16-2009, 04:07 PM
  4. Fill in Null values
    By Petefured in forum Queries
    Replies: 1
    Last Post: 10-06-2008, 12:54 PM
  5. HELP with NULL DateTime VALUES
    By lfolger in forum Programming
    Replies: 3
    Last Post: 03-28-2008, 02:33 PM

Tags for this Thread

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