Is null or other values

Gespierde velopompe

New Member
Joined
May 20, 2010
Messages
45
Hi All,

Small question. I use below code to pick up the beginning area and the end area for which I need results.

Code:
>=[Forms]![F_Homepage]![sector1] And <=[Forms]![F_Homepage]![sector2]

Now, in some instances, the input filed might also be empty, what do I need to add then? If I just add "Is null" to the criteria then I only get blank results.

Thanks,
Frederik.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi Alan,

thanks for the reply, something I learned again. But it is not what I was looking for.

In my code "sector1" and "sector2" are textboxes that can be filled in with a value. Meaning that if sector1 = 04 and sector2 = 07 then the querry should look up values between 04 and 07. But if the textbox is left blank then at the moment I don't get any values returned from my querry. In the contrary, if the textboxes are left blank, then the querry should return all values for "sector".

regards
 
Upvote 0
If you have a minimum and maximum range of codes (such as 00-99 or even 00000-99999) then NZ can substitute these values for null textboxes.

>=NZ([Forms]![F_Homepage]![sector1],'00')
And
<=NZ([Forms]![F_Homepage]![sector2],'99')


Whether it's paranoia or actually a good idea I'd add exit code to the textboxes:
Code:
If Len(Me.Sector1.Value & "")=0 Then
    Me.Sector1.Value = Null
End if
I have found sometimes controls have zero-length strings in them rather than nulls, if a user deletes a value in the control. Or you could write the conditional test more stringently to handle both zero length strings and nulls.
 
Last edited:
Upvote 0
Try Adding an Or statement with Like [Forms]![F_Homepage]![sector1] & "*" or Like [Forms]![F_Homepage]![sector2] &"*"
 
Upvote 0
Just another thought. To avoid endless if not/or/and/else constructions in the expression of the criteria, just go a step further and dynamically built up the SQL statement you use for the form (or wherever you use it).

Add a button to the form to trigger creating the new SQL statement, here is an example for your case.

Code:
Private Sub cmd_CreateSQL_Click()
Dim sSQL As String
Dim sWhereClause As String
Dim iFrom As Integer
Dim iTo As Integer
'The SQL statement without any arguments/criteria
sSQL = "Select * from Table1"
'Built up the whereclause
With Me
    
        'Test if 1st criteria is present
        If Len(.Sector1.Value) > 0 Then iFrom = 1
        'test if 2nd criteria is present
        If Len(.Sector2.Value) > 0 Then iTo = 2
        
        Select Case iFrom - iTo
        
            Case 0 'No criteria
                    sWhereClause = vbNullString
            
            Case 1 'Only first criteria
                    sWhereClause = "Where Sector >='" & .Sector1.Value & "'"
            
            Case -1 'Both Criteria
                    sWhereClause = "Where Sector >='" & .Sector1.Value & "' And Sector <='" & .Sector2.Value & "'"
            
            Case -2 'Only second criteria
                    sWhereClause = "Where Sector <='" & .Sector2.Value & "'"
            
        End Select
        
End With
'Create the SQL string by adding the whereclause
sSQL = sSQL & " " & sWhereClause
'From here you can use the SQL as the recordsource for the form
Me.RecordSource = sSQL

End Sub

At first sight it may look like a little overkill, but you'll find it a lot easier to use.
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,742
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top