I have a form (FRMsurveyReports) with several combo boxes (e.g. Class, ClassUnique, SurveyID, etc.) which use an SQL query based on a table (TBLSurveyResponses) for their record source. The available options filter based on any criteria selected in the combo boxes above them. If nothing is selected, all options should be visible.
For example, consider the following combo boxes below.
ClassUnique
SurveyID
Using code #1 below, if I leave the ClassUnique selection blank, SurveyID will only return records where the ClassUnique value is not null (because the wildcard character doesn't find null values). However, I can't seem to find a good variation on the code to include the nulls. Code #2 below is my latest attempt, but simply returns an error saying, “This expression is typed incorrectly, or is too complex to be evaluated.”
Code #1 -
Like nz([Forms]![FRMsurveyReports].[ClassUnique],"*")
Code #2 -
IIf([Forms]![FRMsurveyReports].[ClassUnique] Is Null,([TBLSurveyResponses].[ClassUnique]) Like "*" Or ([TBLSurveyResponses].[ClassUnique]) Is Null,([TBLSurveyResponses].[ClassUnique]) Like [Forms]![FRMsurveyReports].[ClassUnique])
Any ideas? Thanks for your help!
It's not clear to me where you are putting this fragment of code you listed.
I'm still for dynamically building your filters or RecordSources based on the entries. I generally do that by calling one common filtering subroutine, but you could build the code into each object's AfterUpdate event to selectively rebuild only the RecordSources that are affected by the object that just changed.
In the AfterUpdate event of all of your combo boxes, either a call to the filtering subroutine or the filtering code. In my example, I'll use a common subroutine. -
Private Sub cboObject1_AfterUpdate()
-
Call SetRecordSources
-
end sub
-
-
Private Sub cboObject2_AfterUpdate()
-
Call SetRecordSources
-
end sub
-
-
Private Sub cboObject3_AfterUpdate()
-
Call SetRecordSources
-
end sub
-
-
Private Sub SetRecordSource()
-
dim strSQL as string
-
-
' Select records for combo box object cboObject2
-
strSQL = "Select X,Y,Z from tablename where 1 = 1 "
-
if not IsNull(cboObject1) then strsql = strSql & " AND ClassUnique Like '" & Forms]![FRMsurveyReports].[ClassUnique] & "*' "
-
' then add more ANDs for any other combo box objects that affect this filter before you set the new RecordSource
-
' You probably also have to add some grouping
-
cboObject2.RecordSource = strSql
-
dboObject2.requery
-
-
' Now do cboObject3, 4, 5 ... in similar fashion and so on
-
-
end sub
Jim
6 10779 jimatqsi 1,271
Recognized Expert Top Contributor
If a given combo box is blank just leave the corresponding field out of your where clause. There's no point in testing for everything, if everything is okay then just don't make the test.
It's not really clear what SurveyID is. Is that a saved query? If so, then this won't work, and I would suggest changing your code to dynamically build a string query to plug into the form's recordsource.
Or, if it is a saved query, you could take the tests out of that and dynamically build a filter for the form. Depends on the volume of data that might possibly be returned whether that might be okay or not.
Jim
Jim,
ClassUnique and SurveyID are two of the columns in my table, and while I use about ten to filter, I listed only those to simplify things.
My purpose in testing for everything is twofold:
1) Filter subsequent combo boxes based on selections above them
2) Filter records based on all selected combo boxes
There is one SurveyID for each record (this is a unique field). Conversely, each record can be assigned to one of many ClassUnique values, or none at all. I need to be able to have a user select ClassUnique value "A" from the combo box, and allow only the SurveyID selections in the corresponding combo box that have "A" as the ClassUnique. This isn't a problem. I run into a snag though when I leave the ClassUnique combo box selection empty. When I do not make a selection, the SurveyID field will only show me records that have something/anything in the ClassUnique field (it leaves out all the records with null values).
Example:
ClassUnique_______SurveyID
1234______________99999999
AAAA______________11112222
AAAA______________22223333
__________________44445555
BBBB______________66667777
__________________88889999
AAAA______________12121212
1234______________55555555
In the table above, with my working code (shown below), I get good results if I select AAAA, BBBB, or 1234. However, if I leave ClassUnique blank, I only have six SurveyID's to choose from (the ones without a ClassUnique do not appear).
Partially working code: -
Like nz([Forms]![FRMsurveyReports].[ClassUnique],"*")
I understand why this code doesn't work (the "*" will not find Null values), I just haven't been able to find a good fix yet.
jimatqsi 1,271
Recognized Expert Top Contributor
It's not clear to me where you are putting this fragment of code you listed.
I'm still for dynamically building your filters or RecordSources based on the entries. I generally do that by calling one common filtering subroutine, but you could build the code into each object's AfterUpdate event to selectively rebuild only the RecordSources that are affected by the object that just changed.
In the AfterUpdate event of all of your combo boxes, either a call to the filtering subroutine or the filtering code. In my example, I'll use a common subroutine. -
Private Sub cboObject1_AfterUpdate()
-
Call SetRecordSources
-
end sub
-
-
Private Sub cboObject2_AfterUpdate()
-
Call SetRecordSources
-
end sub
-
-
Private Sub cboObject3_AfterUpdate()
-
Call SetRecordSources
-
end sub
-
-
Private Sub SetRecordSource()
-
dim strSQL as string
-
-
' Select records for combo box object cboObject2
-
strSQL = "Select X,Y,Z from tablename where 1 = 1 "
-
if not IsNull(cboObject1) then strsql = strSql & " AND ClassUnique Like '" & Forms]![FRMsurveyReports].[ClassUnique] & "*' "
-
' then add more ANDs for any other combo box objects that affect this filter before you set the new RecordSource
-
' You probably also have to add some grouping
-
cboObject2.RecordSource = strSql
-
dboObject2.requery
-
-
' Now do cboObject3, 4, 5 ... in similar fashion and so on
-
-
end sub
Jim
Jim,
Thanks for the idea. I hadn't considered dynamically changing the record source -- I'm going to give it a shot. I apologize for not being clearer on where the code extract was coming from before. It was part of the Row Source coding in the property sheet section of the combo box. Here is the full code in SQL view: -
SELECT DISTINCT TBLSurveyResponses.SurveyID
-
FROM TBLSurveyResponses
-
WHERE (((TBLSurveyResponses.SurveyID) Is Not Null) AND ((TBLSurveyResponses.QuestionsModDTG) Like [Forms]![FRMsurveyReports].[QuestionsModDTG]) AND ((TBLSurveyResponses.ClassType) Like nz([Forms]![FRMsurveyReports].[ClassType],"*")) AND ((TBLSurveyResponses.Class) Like nz([Forms]![FRMsurveyReports].[Class],"*")) AND ((TBLSurveyResponses.ClassUnique) Like nz([Forms]![FRMsurveyReports].[ClassUnique],"*")) AND ((TBLSurveyResponses.SurveyDate) Like nz([Forms]![FRMsurveyReports].[SurveyDate],"*")) AND ((TBLSurveyResponses.ReplyRequested) Like nz([Forms]![FRMsurveyReports].[ReplyRequested],"*")))
-
ORDER BY TBLSurveyResponses.SurveyID;
Again, thanks for your idea to handle it all in the after update event. I think that will work much better... I'll let you know.
All right, building on Jim's idea, I now have functioning code (see below). Hopefully this might help someone else with a similar problem. If anyone can see a better way to set this up, please advise. Thanks.
Hopefully I'm not missing any of the pertinent code there...
jimatqsi 1,271
Recognized Expert Top Contributor
Very nice job. :) Glad you got it worked out.
Jim
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Steven Stewart |
last post by:
Hi there,
I have a query (QueryB) that is based on the results of another query
(QueryA).
When I open QueryB itself, it displays the exact records that I want. I
have a report based on...
|
by: Matthew |
last post by:
Hey,
I have built a form that has certain combo and text boxes on it, which
a user specifies his criteria and then clicks on a search button to
run a query based on that criteria. To build to...
|
by: MLH |
last post by:
For example: Nz(,0) returns "300" if the value in
field is 300 (currency data type) and "0" if the value is
zero or null. I get strings in the query output - they are all left
aligned and I...
|
by: Matthias Klöpper |
last post by:
Hi there,
I'm currently trying to call some API-Functions via Reflection.Emit since I
need to bind to different dlls based on user input. The dynamic creation of
the required PInvoke-Methods works...
|
by: Jack Addington |
last post by:
I have a 3rd party object that fires an itemchanged event when someone edits
some data on a form. This event has a custom eventArgs that has a field
called ActionCode. In the code of the event,...
|
by: amith.srinivas |
last post by:
Hi all,
From a word macro in VBA, I am trying to create a report in
access DB. The report is based on a query with a parameter. I am using
Set rpt = Application.CreateReport
rpt.RecordSource =...
|
by: bz |
last post by:
Hi,
I have a datagrid with two columns Region and City
Both have combo-boxes. City column is unbound
What I want is, when use select a region, to fill the combo for City
column with appropriate...
|
by: barcaroller |
last post by:
I am trying to adopt a model for calling functions and checking their return
values. I'm following Scott Meyer's recommendation of not over-using
exceptions because of their potential overhead.
...
|
by: =?Utf-8?B?QmVu?= |
last post by:
Hi all,
I'm trying to understand the concept of returning functions from the
enclosing functions. This idea is new to me and I don't understand when and
why I would need to use it.
Can someone...
|
by: Del |
last post by:
Hello and thanks for any and all assistance!
I have a database that is used by several users on several different
machines.
The backend database is housed on a file server.
Each user has a...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |