473,692 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using and If Statement in conjunction w/ Multi-Select List Box to Filter a Report

Is there a way to add an If Statement to the following code so if data
in a field equals "x" it will launch one report and if it equals "y" it
would open another report. Anyone know how to modify this?

Private Sub cmdPreview_Clic k()
On Error GoTo Err_Handler
'Purpose: Open the report filtered to the items selected in the
list box.
'Author: Allen J Browne, 2004. http://allenbrowne.com
Dim varItem As Variant 'Selected items
Dim strWhere As String 'String to use as WhereCondition
Dim strDescrip As String 'Description of WhereCondition
Dim lngLen As Long 'Length of string
Dim strDelim As String 'Delimiter for this field type.
Dim strDoc As String 'Name of report to open.

'strDelim = """" 'Delimiter appropriate to field type.
See note 1.
strDoc = "Products by Category"

'Loop through the ItemsSelected in the list box.
With Me.lstCategory
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
'Build up the filter from the bound column (hidden).
strWhere = strWhere & strDelim & .ItemData(varIt em) &
strDelim & ","
'Build up the description from the text in the visible
column. See note 2.
strDescrip = strDescrip & """" & .Column(1, varItem) &
""", "
End If
Next
End With

'Remove trailing comma. Add field name, IN operator, and brackets.
lngLen = Len(strWhere) - 1
If lngLen > 0 Then
strWhere = "[AutoID] IN (" & Left$(strWhere, lngLen) & ")"
lngLen = Len(strDescrip) - 2
If lngLen > 0 Then
strDescrip = "Categories : " & Left$(strDescri p, lngLen)
End If
End If

'Report will not filter if open, so close it. For Access 97, see
note 3.
If CurrentProject. AllReports(strD oc).IsLoaded Then
DoCmd.Close acReport, strDoc
End If

'Omit the last argument for Access 2000 and earlier. See note 4.

DoCmd.OpenRepor t strDoc, acViewPreview, WhereCondition: =strWhere,
OpenArgs:=strDe scrip

Exit_Handler:
Exit Sub

Err_Handler:
If Err.Number <> 2501 Then 'Ignore "Report cancelled" error.
MsgBox "Error " & Err.Number & " - " & Err.Description , ,
"cmdPreview_Cli ck"
End If
Resume Exit_Handler
End Sub
_______________ _______________ __________

Jan 24 '06 #1
5 3279
How about replacing the line:

strDoc = "Products by Category"

with:

If [SomeControl] = "x" Then
strDoc = "Report1"
Else
strDoc = "Report2"
End If

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

<dk*******@yaho o.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
Is there a way to add an If Statement to the following code so if data
in a field equals "x" it will launch one report and if it equals "y" it
would open another report. Anyone know how to modify this?

Private Sub cmdPreview_Clic k()
On Error GoTo Err_Handler
'Purpose: Open the report filtered to the items selected in the
list box.
'Author: Allen J Browne, 2004. http://allenbrowne.com
Dim varItem As Variant 'Selected items
Dim strWhere As String 'String to use as WhereCondition
Dim strDescrip As String 'Description of WhereCondition
Dim lngLen As Long 'Length of string
Dim strDelim As String 'Delimiter for this field type.
Dim strDoc As String 'Name of report to open.

'strDelim = """" 'Delimiter appropriate to field type.
See note 1.
strDoc = "Products by Category"

'Loop through the ItemsSelected in the list box.
With Me.lstCategory
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
'Build up the filter from the bound column (hidden).
strWhere = strWhere & strDelim & .ItemData(varIt em) &
strDelim & ","
'Build up the description from the text in the visible
column. See note 2.
strDescrip = strDescrip & """" & .Column(1, varItem) &
""", "
End If
Next
End With

'Remove trailing comma. Add field name, IN operator, and brackets.
lngLen = Len(strWhere) - 1
If lngLen > 0 Then
strWhere = "[AutoID] IN (" & Left$(strWhere, lngLen) & ")"
lngLen = Len(strDescrip) - 2
If lngLen > 0 Then
strDescrip = "Categories : " & Left$(strDescri p, lngLen)
End If
End If

'Report will not filter if open, so close it. For Access 97, see
note 3.
If CurrentProject. AllReports(strD oc).IsLoaded Then
DoCmd.Close acReport, strDoc
End If

'Omit the last argument for Access 2000 and earlier. See note 4.

DoCmd.OpenRepor t strDoc, acViewPreview, WhereCondition: =strWhere,
OpenArgs:=strDe scrip

Exit_Handler:
Exit Sub

Err_Handler:
If Err.Number <> 2501 Then 'Ignore "Report cancelled" error.
MsgBox "Error " & Err.Number & " - " & Err.Description , ,
"cmdPreview_Cli ck"
End If
Resume Exit_Handler
End Sub
_______________ _______________ __________

Jan 24 '06 #2
Thank you, but since I am working with a multi-select list box with
unbound controls, the field that I need refer to isn't a part of the
form, but a part of the rowsource of the unbound multi-select list box.
I have tried to work this out, but can't seem to get it. Here is what
I have so far:

If Me.lstCategory. RowSource.Autos .AutoState = "TX" Then
strDoc = "PrintTXAutoIDC ardFromMultiSel ect"
Else
strDoc = "PrintMiscAutoI DCardFromMutlti Select"
End If

lstCategory being the name of the unbound multi-select list box and
Autos.AutoState being a part of the row source of the multi-select list
box that I need to refer to. I know my problem is with the 1st line
and in the way I am trying to refer to the rowsource.

HELP!!!!

Thanks.
DK

Jan 25 '06 #3
So you want to know whether "TX" is one of the items in the multi-select
list box?

You will need to write a loop through the ItemsSelected collection of the
list box, examining each one to see if you have a match.

You will need to code this yourself, recognising that becuase it is a
multi-select, it is possible that multiple conditions could be matched.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

<dk*******@yaho o.com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
Thank you, but since I am working with a multi-select list box with
unbound controls, the field that I need refer to isn't a part of the
form, but a part of the rowsource of the unbound multi-select list box.
I have tried to work this out, but can't seem to get it. Here is what
I have so far:

If Me.lstCategory. RowSource.Autos .AutoState = "TX" Then
strDoc = "PrintTXAutoIDC ardFromMultiSel ect"
Else
strDoc = "PrintMiscAutoI DCardFromMutlti Select"
End If

lstCategory being the name of the unbound multi-select list box and
Autos.AutoState being a part of the row source of the multi-select list
box that I need to refer to. I know my problem is with the 1st line
and in the way I am trying to refer to the rowsource.

HELP!!!!

Thanks.
DK

Jan 25 '06 #4
No help, but thanks anyway.

Jan 25 '06 #5
No help, but thanks anyway.

Jan 25 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
9639
by: Russell Mangel | last post by:
Is it possible to use the using statement with XmlTextReader? I tryed to use it, but it gives me the error message: Cannot implicitly convert type 'System.Xml.XmlTextReader' to 'System.IDisposable' Is there something I am doing wrong? // This no worky using(XmlTextReader xtr = new XmlTextReader("C:\\myfile.xml")) {
5
2700
by: Bill Priess | last post by:
Hey gang, Ok, I'm stumped on this one... I am using the using statement to wrap a SqlDataAdapter that I am using to fill a DataTable. Now, what I need to know is, just how much block-scope applies to objects created in the using scope. For example: <code> static DataTable getTable()
1
315
by: Kepler | last post by:
I have a situation where I need to use a Using statement that creates some records in a database. After that completes, if it completes, I need to do some file creation. Any code I'm putting after the Using statement gives a compiler warning of "Unreachable Code Detected", and it never runs. Why is this happening? I really don't want to put the file manipulation code in the using statement because it's a transaction. I also need for...
8
3703
by: J-T | last post by:
I have a class like below I have a couple of questions about that: 1) I like to use "Using statement" when creating an object of this class,so I had to implement IDisposable.Am I doing this right here? 2) Do I have to be worried about those objects I have created in GetPackageNames() ?? for instance making them null afterward or something? 3) This class is in my business layer As you can see I'm using Microsoft.SQLServer.DTSPkg80...
1
3506
by: pascal_pare | last post by:
Hi, Is there a way to declare a using statement with the CodeDom? Something like: using (obj = new Object()) { obj.DoSomething; }
18
2362
by: Trevor | last post by:
I have seen the "using" statement used in strange ways in some C# code. Example: using (StreamReader sr = new StreamReader("TestFile.txt")) { // do some stuff... } What effect does the using statement give you in this example?
5
5604
by: Andreas Müller | last post by:
Hi, I was wondering, if there is something similar in VB.NET like the using statement in C#. What it does is to automatically call Dispose on the object decrared with in the statement when the block exits. using (MyBoj) { }// MyObj.Dispose is called
2
5014
by: Robert Bravery | last post by:
Hi all, Being new to C# and .net I often don't know how to use things. I have created an app that imports excel data, it works well, with methods to open excel, extract the data and close excel. Now a colege of mine suggested useinf the usingstatement, so that the office object is dispossed/destroyued correctly, saying that it prevents memoryleaks. Problem is I am having problems trying to achieve this, I keep getting the error: type...
0
8538
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9083
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8797
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8800
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5818
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4323
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4557
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2972
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 we have to send another system
3
1957
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.