Consulting

Results 1 to 10 of 10

Thread: Solved: send null value to a function

  1. #1
    VBAX Regular
    Joined
    Jul 2005
    Posts
    14
    Location

    Exclamation Solved: send null value to a function

    I have a query which receives a parameter from a form. The criteria is set to fmSetParam()

    The user selects a BI number from a combo box. If however, the user wishes to view all (no filter on BI) then
    I wish the function to return a Is Not Null to the fmSetParam. It does not work. How do I pass a Is Not Null to the function that is usable in the query?

    The statement I have so far is:

    [vba]Function fnSetParam() As String
    Dim strInput As String

    If Forms!fmSelect!txtBI = "" Then
    strInput = "Is Not Null"
    Else
    strInput= Forms!fmSelect!txtBI
    End If
    fnSetParam = strInput
    End Function[/vba]

    I have declared the strInput in the form's module. It works for any number I enter in the input box but not for a null value. In the event of a null value, I wish the query to return ALL records in the recordset.
    Last edited by Killian; 07-25-2005 at 09:17 AM. Reason: Added VBA Tags

  2. #2
    VBAX Mentor Brandtrock's Avatar
    Joined
    Jun 2004
    Location
    Titonka, IA
    Posts
    399
    Location
    Welcome to VBAX!!!

    Please use code tags when posting code. The green and white VBA icon on the reply toolbar will do this for you. It makes reading posted code much easier.

    Thanks,
    Brandtrock




  3. #3
    VBAX Regular
    Joined
    Jul 2005
    Posts
    14
    Location
    I have a query which receives a parameter from a form. The criteria is set to fmSetParam()

    The user selects a BI number from a combo box. If however, the user wishes to view all (no filter on BI) then
    I wish the function to return a Is Not Null to the fmSetParam. It does not work. How do I pass a Is Not Null to the function that is usable in the query?

    The statement I have so far is:

    [VBA]Function fnSetParam() As String
    Dim strInput As String

    If Forms!fmSelect!txtBI = "" Then
    strInput = "Is Not Null"
    Else
    strInput= Forms!fmSelect!txtBI
    End If
    fnSetParam = strInput
    End Function[/VBA]

    I have declared the strInput in the form's module. It works for any number I enter in the input box but not for a null value. In the event of a null value, I wish the query to return ALL records in the recordset.

  4. #4
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Why not use the * wildcard instead of Is Not Null?

    I think if you use "Is Not Null" Access will actually look for that string not the expression.

  5. #5
    VBAX Regular
    Joined
    Jul 2005
    Posts
    14
    Location
    "*" will not work. neither will NZ([BI]). no matter how I code it, it will accept anything - even "brocolli" - but not anything that refers to <> Null

    Thanks, Norie

  6. #6
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Can we see the actual SQL for the query?

  7. #7
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location
    Make your life easier and forget about using a parameter with the GUI for queries. Just change the source of whatever you want to the SQL statement the function below generates.

    [VBA] Function fnSetParam() As String
    Dim strSqlBase As String
    Dim strSqlWhere As String

    strSqlBase = "SELECT tblSample.Field1, tblSample.Field2," _
    & " tblSample.Field3 FROM tblSample"
    If Forms!fmSelect!txtBI = "" Then
    strSqlWhere = " WHERE Not IsNull(tblSample.Field1);"
    Else
    strSqlWhere = " WHERE tblSample.Field1 =" & Forms!fmSelect!txtBI & ";"
    End If

    fnSetParam = strSqlBase & strSqlWhere

    End Function [/VBA]

  8. #8
    VBAX Regular
    Joined
    Jul 2005
    Posts
    14
    Location
    Allright, I guess I will do that. Thanks xCav8r. You like like a brilliant guy so I will take your advice.

    Thanks to you to Norie, for your time and your interest.

    bonsiur

  9. #9
    VBAX Regular
    Joined
    Jul 2005
    Posts
    14
    Location
    How do I close this thread and mark it ANSWERED???

  10. #10
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location
    Look for the link to thread tools at the top of 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
  •