Query Criteria via Function

bschulze

Active Member
Joined
Jun 2, 2005
Messages
289
I'm trying to provide the criteria for a query via a function, only because I could not get the like(isnull(iff statement to work properly. I have a form where the users select specific filter criteria for a report, I then read the forms data and pass the value to a query.

[CODE ]
Function FilterCompanyCode() As String
If IsNull(Forms!frmReports.cboEntity) Then
FilterCompanyCode = "Is Null Or """" Or Like ""*"""
Else
FilterCompanyCode = [Forms]![frmReports].[cboEntity]
End If
End Function
[/ CODE]

The FilterCompanyCode() is in the queries criteria. When the value in the combobox is not null the query returns the desired records. When the combobox is null the query does not return any records. When I step through the function and copy the variable directly to the query, it returns the proper recordset.

Anyone have any ideas why it wont work while running the function but does work when copied into the criteria of the query. Any help is greatly appreciated.
 
Last edited:
I have a solution, will try to recap for anyone searching in the future.

Need to pass a value from a user form to a queries criteria. Desired results, if not null then user value, else all records (including Null values).

The solution I found was to break up the or statements into multiple lines.

Original Try:
IIF(IsNull([Forms]![frmA].[cboB]),Is Null Or "" Or Like "*",[Forms]![frmA].[cboB]

Solutions:
Criteria 1 = [Forms]![frmA].[cboB] Is Null
Criteria 2 = [Forms]![frmA].[cboB]


Break the statement into two criteria lines.

Alternatively, if you want to display the null values when the user has selected a criteria
[Forms]![frmA].[cboB] Or Like [Forms]![frmA].[cboB] Is Null


Hope that helps anyone interested, cant help you as to why one works and the other doesn't.
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,221,827
Messages
6,162,202
Members
451,752
Latest member
freddocp

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