This article from Microsoft shows how to create a report that you can filter dynamically in Print Preview by selecting filter criteria from a pop-up form.
I’ve tried to implement it (see attached mdb) but when, according to the instructions in the article, I select “BC” in the Region combo box, and then click the Set Filter button. I get the following error message:
Run-time error ’13’:
Type mismatch
![]() |
Patch reliability is unclear, but widespread attacks make patching prudent. Go ahead and patch, but watch out for potential problems. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Filtering a Report from a Pop-Up Form (2000)
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Filtering a Report from a Pop-Up Form (2000)
- This topic has 21 replies, 5 voices, and was last updated 23 years, 9 months ago.
AuthorTopicWSgrovelli
AskWoody LoungerAugust 31, 2001 at 1:14 pm #359733Viewing 1 reply threadAuthorReplies-
WSMarkLiquorman
AskWoody LoungerAugust 31, 2001 at 5:17 pm #540107It would be easier to post the code here. It is too much of a bother to download databases.
My expectation is that you didn’t form your string that holds the WHERE criteria to include apostrophes around “BC”. How you form the string depends on whether it is a numeric or a text field, so depending on the situation, you can have:
If Region is numeric, this is okay: Region=123
If Region is text, then you must have: Region=’BC’An easy way is to use the BuildCriteria function, this will automatically put in the right delimiters, depending on the field type you give it.
-
WSJudyJones
AskWoody LoungerSeptember 2, 2001 at 11:33 pm #540319The way the code that the article showed to type in the OnClick event of the Set Filter command button caused extra quotes to be inserted when you moved off the lines. I am referring to the red portion of the following code:
strSQL = strSQL & “[” & Me(“Filter” & intCounter).Tag & “] ” _
& ” = ” & Chr(34) & Me(“Filter” & intCounter) & Chr(34) & “” _
And “”What I did was remove the line break so that ” And ” was all on the same line and it worked fine.
-
WSgrovelli
AskWoody LoungerSeptember 3, 2001 at 6:57 am #540335Thanks Judy.
If I look up the OpenReport Method in VBA on-line help, I see the View argument has three intrinsic constants available: acViewDesign, acViewNormal (default) and acViewPreview, I don’t see the constant A_PREVIEW used in Private Sub Form_Open(Cancel As Integer) of the article.
Do you know why they used A_PREVIEW?
Ciao -
WSJudyJones
AskWoody Lounger -
WSgrovelli
AskWoody Lounger
-
-
WSrory
AskWoody LoungerSeptember 4, 2001 at 11:19 am #540492 -
WSgrovelli
AskWoody Lounger -
WSrory
AskWoody LoungerSeptember 4, 2001 at 12:08 pm #540506Actually, they do get selected (as you should see if you select all, then move them) they just don’t appear to. I think it’s because the labels are directly underneath (and the same size as) the comboboxes, so the selection ‘highlighting’ of the 2 controls is cancelling itself out, if you see what I mean. Try moving the labels and you should be able to see the controls being selected.
Hope that helps. -
WSgrovelli
AskWoody LoungerSeptember 4, 2001 at 1:22 pm #540526Yep, I used Format, Size and Format, Align for the combo boxes and buttons on frmFilter once I created them and didn’t realize the labels(being transparent) were covering the combo boxes. Still one thing leaves me curious:
if, e.g., I move the label(Label5) associated with the Filter3 combo box away from Filter3(see attachment) and then go into Form View, the caption(Combo4: ) of the label appears on the form. If I then select Label5, select the Menu command: Format, Bring to Front and position Label5 back onto the Filter3 combo box I would expect the label caption to appear superimposed on Filter3 once in Form View. Why isn’t it so?
Ciao -
WSgrovelli
AskWoody Lounger -
WSStewart
AskWoody LoungerSeptember 6, 2001 at 5:23 am #540916I don’t have access 2000 but the 97 method would be something like this.
(straight from the help)The following example prints the value of the bound column for each selected row in a Names list box on a Contacts form. To try this example, create the list box and set its BoundColumn property as desired and its MultiSelect property to Simple or Extended. Switch to Form view, select several rows in the list box, and run the following code:
Sub BoundData()
Dim frm As Form, ctl As Control
Dim varItm As VariantSet frm = Forms!Contacts
Set ctl = frm!Names
For Each varItm In ctl.ItemsSelected
Debug.Print ctl.ItemData(varItm)
Next varItm
End Sub -
WSgrovelli
AskWoody LoungerSeptember 7, 2001 at 5:38 am #540933I used your suggestion to achieve being able to select multiple Company Names.(see attachment)
Problem is I think I need to build strSQL using an if condition to take into account the fact that the Filter1 list box can now have multiple values but I get a Syntax Error when I click the “Set Filter” button.
Is it possible to insert an if condition into the expression building the strSQL string and, if so, where’s the syntax error?
Ciao
Btw, are you the same Stewart Tanner mentioned here? -
WSStewart
AskWoody LoungerSeptember 7, 2001 at 3:09 am #541129I only have access 97 loaded on my PC so couldn’t look at your database, but have tried to provide you with an answer anyway.
Look at the following code.
Private Sub butBuildFilter_Click() Dim ctl As Control Dim varItm As Variant Dim MyCriteria As String Dim ArgCount As Integer 'initialise values MyCriteria = "" ArgCount = 0 Set ctl = lstData For Each varItm In ctl.ItemsSelected If Not ArgCount = 0 Then ' previous items have been added to the list 'Substitute your field name for the word FIELDNAME MyCriteria = MyCriteria & " or [Fieldname] = '" _ & ctl.ItemData(varItm) & "'" ArgCount = ArgCount + 1 Else 'Substitute your field name for the word FIELDNAME MyCriteria = "[Fieldname] = '" _ & ctl.ItemData(varItm) & "'" 'first item added. ArgCount = ArgCount + 1 End If Next varItm 'you now have a where clause that you can use to 'open a report, form etc, or add to a SQL 'string for whatever use. Or a string that you can 'use as a filter. MsgBox MyCriteria End Sub
Your hyperlink asking if I’m the same Stewart Tanner doesn’t go anywhere, so I can’t answer that.
Edited by Charlotte to eliminate horizontal scrolling
-
WSgrovelli
AskWoody LoungerSeptember 7, 2001 at 5:55 am #541180I used your suggestion to achieve being able to select multiple Company Names.(see attachment)
Problem is I think I need to build strSQL using an if condition to take into account the fact that the Filter1 list box can now have multiple values but I get a Syntax Error (related to the code in red in the Private Sub Set_Filter_Click() below) when I click the “Set Filter” button.
Is it possible to insert an if condition into the expression building the strSQL string and, if so, where’s the syntax error?
CiaoPrivate Sub Set_Filter_Click() Dim strSQL As String, intCounter As Integer, a Dim frm As Form, ctl As Control Dim varItm As Variant Set frm = Forms!frmFilter Set ctl = frm!Filter1 For Each varItm In ctl.ItemsSelected a = ctl.ItemData(varItm) & "," & a Next varItm For intCounter = 1 To 5 If Me("Filter" & intCounter) "" Then strSQL = strSQL & "[" & Me("Filter" & intCounter).Tag & "] " _ IF intCounter = 1 then & " IN(" & Chr(34) & a & Chr(34) & ") And " Else & " = " & Chr(34) & Me("Filter" & intCounter) & Chr(34) & " And " End If End If Next If strSQL "" Then ' Strip Last " And ". strSQL = Left(strSQL, (Len(strSQL) - 5)) ' Set the Filter property. Reports![rptCustomers].Filter = strSQL Reports![rptCustomers].FilterOn = True End If End Sub
Btw, are you the same Stewart Tanner mentioned here?
-
WSStewart
AskWoody LoungerSeptember 9, 2001 at 9:18 pm #541539ok, this should fix it.
Private Sub Set_Filter_Click() Dim strSQL As String, intCounter As Integer, a Dim frm As Form, ctl As Control Dim varItm As Variant Set frm = Forms!frmFilter Set ctl = frm!Filter1 For Each varItm In ctl.ItemsSelected a = ctl.ItemData(varItm) & "," & a Next varItm For intCounter = 1 To 5 If Me("Filter" & intCounter) "" Then strSQL = strSQL & "[" & Me("Filter" & intCounter).Tag & "] " If intCounter = 1 Then strSQL = strSQL & " IN(" & Chr(34) & a & Chr(34) & ") And " Else strSQL = strSQL & " = " & Chr(34) & Me("Filter" & intCounter) & Chr(34) _ & " And " End If End If Next If strSQL "" Then ' Strip Last " And ". strSQL = Left(strSQL, (Len(strSQL) - 5)) ' Set the Filter property. Reports![rptCustomers].Filter = strSQL Reports![rptCustomers].FilterOn = True End If End Sub
I’m not the same Stewart Tanner, although it was interesting. I’m in Victoria and am a bit to old to be a Uni student.
-
WSgrovelli
AskWoody LoungerSeptember 10, 2001 at 7:21 am #541574I’ve tried putting a break point at the line
For intCounter = 1 To 5
in Private Sub Set_Filter_Click() to step through the procedure and notice that even if I select a couple of items in the Filter1 list box, the value assumed by Filter1 equals a zero-length string because the line
If Me(“Filter” & intCounter) “” Then
gets skipped.
Why doesn’t VBA acknowledge selecting items in the Filter1 list box? -
WSrory
AskWoody LoungerSeptember 10, 2001 at 7:52 am #541577Hi,
I think your problem lies in the fact that the Value property of a multi-select listbox is set to Null. Try something like:
Private Sub Set_Filter_Click()
Dim strSQL As String, intCounter As Integer, a
Dim frm As Form, ctl As Control
Dim varItm As VariantSet frm = Forms!frmFilter
Set ctl = frm!Filter1For Each varItm In ctl.ItemsSelected
a = Chr(34) & ctl.ItemData(varItm) & Chr(34) & “,” & a
Next varItm
For intCounter = 2 To 5
If Me(“Filter” & intCounter) “” Then
strSQL = strSQL & “[” & Me(“Filter” & intCounter).Tag & “] ”
strSQL = strSQL & ” = ” & Chr(34) & Me(“Filter” & intCounter) & Chr(34) _
& ” And ”
End If
Next
If a “” Then
strSQL = strSQL & “[” & Me.Filter1.Tag & “] in (” & Left$(a, Len(a) – 1) & “)”
Else
strSQL = Left$(strSQL, Len(strSQL) – 5)
End If
With Reports![rptCustomers]
.Filter = strSQL
.FilterOn = True
End With
End Sub
Hope that helps. -
WSgrovelli
AskWoody LoungerSeptember 10, 2001 at 9:21 am #541586all your super brain cooks up.
The If a “” Then line in Private Sub Set_Filter_Click() tests for inequality with a zero-length string yet you say the Value property of a multi-select listbox is set to Null. If I don’t select any item in Filter1 list box and click on the “Set Filter” button stepping through code, when I hover the mouse over “a” in the If a “” Then line, Data Tips says “a=Empty”
Are zero-length string, Null and Empty all equivalent?
Ciao -
WSrory
AskWoody LoungerSeptember 10, 2001 at 10:54 am #541592Hi,
No they’re not equivalent. But because you only want that code to run if there is a string in a, it has the right effect. A variant variable is empty if it hasn’t been initialised (which is the case if a hasn’t been assigned a value)
In all honesty, I had forgotten that a was a variant and was thinking of it as a string variable (luckily for me it works anyway!)
-
WSStewart
AskWoody Lounger -
WSgrovelli
AskWoody Lounger
-
-
-
-
Viewing 1 reply thread -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Name of MS Word Formatting Feature
by
John Baum
2 hours, 26 minutes ago -
InControl Failure?
by
Casey H
3 hours, 9 minutes ago -
Microsoft : Free 1 year support for Windows 10 after EOL
by
Alex5723
1 hour, 33 minutes ago -
MS-DEFCON 3: Businesses must tread carefully
by
Susan Bradley
2 hours, 45 minutes ago -
McLaren Health Care says data breach impacts 743,000 patients
by
Nibbled To Death By Ducks
19 hours, 5 minutes ago -
WhatsApp banned on House staffers’ devices
by
Alex5723
14 hours, 7 minutes ago -
Is your device eligible?
by
Susan Bradley
22 hours, 4 minutes ago -
Windows 11 Insider Preview build 26200.5661 released to DEV
by
joep517
1 day, 4 hours ago -
Windows 11 Insider Preview build 26120.4452 (24H2) released to BETA
by
joep517
1 day, 4 hours ago -
Hello Windows…My Problem is Windows Hello…
by
rdleib
1 day, 5 hours ago -
New Canon Printer Wants Data Sent
by
Win7and10
1 day, 5 hours ago -
I set up passkeys for my Microsoft account
by
Lance Whitney
19 hours, 20 minutes ago -
AI is for everyone
by
Peter Deegan
1 day, 5 hours ago -
Terabyte update 2025
by
Will Fastie
23 hours, 26 minutes ago -
Migrating from Windows 10 to Windows 11
by
Susan Bradley
2 hours, 19 minutes ago -
Lost sound after the upgrade to 24H2?
by
Susan Bradley
2 days, 4 hours ago -
How to move 10GB of data in C:\ProgramData\Package Cache ?
by
Alex5723
8 hours, 5 minutes ago -
Plugged in 24-7
by
CWBillow
1 day, 14 hours ago -
Netflix, Apple, BofA websites hijacked with fake help-desk numbers
by
Nibbled To Death By Ducks
2 days, 17 hours ago -
Have Copilot there but not taking over the screen in Word
by
CWBillow
2 days, 14 hours ago -
Windows 11 blocks Chrome 137.0.7151.68, 137.0.7151.69
by
Alex5723
4 days, 8 hours ago -
Are Macs immune?
by
Susan Bradley
1 day ago -
HP Envy and the Function keys
by
CWBillow
3 days, 16 hours ago -
Microsoft : Removal of unwanted drivers from Windows Update
by
Alex5723
1 day, 9 hours ago -
MacOS 26 beta 1 dropped support for Firewire 400/800
by
Alex5723
4 days, 20 hours ago -
Unable to update to version 22h2
by
04om
2 days, 4 hours ago -
Windows 11 Insider Preview Build 26100.4482 (24H2) released to Release Preview
by
joep517
5 days, 3 hours ago -
Windows 11 Insider Preview build 27881 released to Canary
by
joep517
5 days, 3 hours ago -
Very Quarrelsome Taskbar!
by
CWBillow
4 days, 13 hours ago -
Move OneNote Notebook OFF OneDrive and make it local
by
CWBillow
5 days, 16 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.