472,348 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,348 software developers and data experts.

Values from 4 Listboxes to a Query/Report

Hi,

I have the following code that runs my report generator. The user
selects a table from a combo box, then whatever fields they want from a
list box. (This part all works and the report runs fine). There is then
a combo box they can select a field from (eg CompanyID etc) and then
the list box below that contains the values (eg Microsoft, Novell etc).
These are all multi-select list boxes. Now I can get the code to work
if the user selects a table, fields, and values up to the first value
list box, (but they can't leave this blank and just select a table and
fields), so I need to add some kind of 'If Me.cboFieldName1 <Null
Then' execute whatnot if not then please skip everything and just run
the report.

But there are then 3 more combos and list boxes, So I need to do the
following:

if cboFieldName1 is not null then
run this code
if not skip the next three and run report

if cboFieldName2 is not null then
run this code
if not skip the next two and run report

if cboFieldName3 is not null then
run this code
if not skip the next one and run report

if cboFieldName4 is not null then
run this code
if not skip to running the report

But i'm not sure exactly what to write to get it to work. So some help
there would be great.

So at the moment the code below makes the user use all 4 refining list
boxes. But then spits out the following error when run report is
clicked - Syntax error (missing operator) in query expression 'Year in
('2006') WHERE Membership_Type in ('Facilities') WHERE
subMembership_Type in (Eastern Stand Coporate Suite')'.

it has read the values selected (from 3 not all 4)

So in summary:

Works selecting a table and fields and values (1 listbox only - have to
remove the creating the WITH IN string sections of code for list boxes
2 - 4 for this to work) In addition the user must not leave the one
working list box empty.
So I need a If statement to allow for an empty list box/s
situation.

The form itself works for 4 list boxes until the run report is clicked
and the missing syntax error appears.
The error is somewhere in the below code, but i'm hitting a
brickwall.

All help is greatly appreciated.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Private Sub cmdRunReport_Click()
On Error GoTo Err_cmdRunReport_Click
Dim MyDB As DAO.Database
Dim qdf As QueryDef
Dim rst As DAO.Recordset, rst2 As DAO.Recordset
Dim i As Integer, j As Integer, k As Integer, strSQL As String
Dim strFieldList As String, strIN As String, strWhereIN1 As String,
strWhereIN2 As String, strWhereIN3 As String, strWhereIN4 As String
Dim strWhere1 As String, strWhere2 As String, strWhere3 As String,
strWhere4 As String
Dim flgAll As Boolean

Set MyDB = CurrentDb()
Set rst = MyDB.OpenRecordset("tablefields")

strSQL = "SELECT "
j = 0
k = 0
rst.MoveFirst
'create the IN string by looping thru the listbox
For i = 0 To lstFieldNames.ListCount - 1
If lstFieldNames.Selected(i) Then
strIN = strIN & "[" & lstFieldNames.Column(0, i) & "] as
Field" & k & ","
rst.Edit
rst!indexx = k
rst.Update
rst.MoveNext
k = k + 1
Else
rst.Edit
rst!indexx = Null
rst.Update
rst.MoveNext
End If
j = j + 1
Next i
For i = k To lstFieldNames.ListCount - 1
strIN = strIN & "null as Field" & i & ","
Next i
' stripoff the last comma of the IN string
strFieldList = Left(strIN, Len(strIN) - 1)

strSQL = strSQL & strFieldList & " FROM " & Me!cboTable
'MsgBox strSQL
' creates the -WhereIN- string by looping thru the listbox
For i = 0 To lstFieldValues1.ListCount - 1
If lstFieldValues1.Selected(i) Then
If lstFieldValues1.Column(0, i) = "All" Then
flgAll = True
End If

' checks data type of field for delimiting
If cboFieldName1.Column(1) >= 1 And
cboFieldName1.Column(1) <= 7 Then
' if the field holds numeric data
strWhereIN1 = strWhereIN1 & " " &
lstFieldValues1.Column(0, i) & ","
ElseIf cboFieldName1.Column(1) = 8 Then
' if the field is a date
strWhereIN1 = strWhereIN1 & "#" &
lstFieldValues1.Column(0, i) & "#,"
ElseIf cboFieldName1.Column(1) = 10 Then
'if the field is text
strWhereIN1 = strWhereIN1 & "'" &
lstFieldValues1.Column(0, i) & "',"
End If
End If
Next i
' creates the -WHERE- string, stripping off the last comma of
the -IN- string
strWhere1 = " WHERE " & Me!cboFieldName1 & " in (" &
Left(strWhereIN1, Len(strWhereIN1) - 1) & ")"
'MsgBox strWhere1
' if "All" was selected then don't add the -WHERE- condition
If Not flgAll Then
strSQL = strSQL & strWhere1
End If
'MsgBox strSQLS

' creates the -WhereIN- string by looping thru the listbox
For i = 0 To lstFieldValues2.ListCount - 1
If lstFieldValues2.Selected(i) Then
If lstFieldValues2.Column(0, i) = "All" Then
flgAll = True
End If

' checks data type of field for delimiting
If cboFieldName2.Column(1) >= 1 And cboFieldName2.Column(1)
<= 7 Then
' if the field holds numeric data
strWhereIN2 = strWhereIN2 & " " &
lstFieldValues2.Column(0, i) & ","
ElseIf cboFieldName2.Column(1) = 8 Then
' if the field is a date
strWhereIN2 = strWhereIN2 & "#" &
lstFieldValues2.Column(0, i) & "#,"
ElseIf cboFieldName2.Column(1) = 10 Then
'if the field is text
strWhereIN2 = strWhereIN2 & "'" &
lstFieldValues2.Column(0, i) & "',"
End If
End If
Next i
' creates the -WHERE- string, stripping off the last comma of the
-IN- string
strWhere2 = " WHERE " & Me!cboFieldName2 & " in (" &
Left(strWhereIN2, Len(strWhereIN2) - 1) & ")"
'MsgBox strWhere2
' if "All" was selected then don't add the -WHERE- condition
If Not flgAll Then
strSQL = strSQL & strWhere2
End If
'MsgBox strSQL

' creates the -WhereIN- string by looping thru the listbox
For i = 0 To lstFieldValues3.ListCount - 1
If lstFieldValues3.Selected(i) Then
If lstFieldValues3.Column(0, i) = "All" Then
flgAll = True
End If

' checks data type of field for delimiting
If cboFieldName3.Column(1) >= 1 And cboFieldName3.Column(1)
<= 7 Then
' if the field holds numeric data
strWhereIN3 = strWhereIN3 & " " &
lstFieldValues3.Column(0, i) & ","
ElseIf cboFieldName3.Column(1) = 8 Then
' if the field is a date
strWhereIN3 = strWhereIN3 & "#" &
lstFieldValues3.Column(0, i) & "#,"
ElseIf cboFieldName3.Column(1) = 10 Then
'if the field is text
strWhereIN3 = strWhereIN3 & "'" &
lstFieldValues3.Column(0, i) & "',"
End If
End If
Next i
' creates the -WHERE- string, stripping off the last comma of the
-IN- string
strWhere1 = " WHERE " & Me!cboFieldName3 & " in (" &
Left(strWhereIN3, Len(strWhereIN3) - 1) & ")"
'MsgBox strWhere3
' if "All" was selected then don't add the -WHERE- condition
If Not flgAll Then
strSQL = strSQL & strWhere3
End If
'MsgBox strSQL

' creates the -WhereIN- string by looping thru the listbox
For i = 0 To lstFieldValues4.ListCount - 1
If lstFieldValues4.Selected(i) Then
If lstFieldValues4.Column(0, i) = "All" Then
flgAll = True
End If

' checks data type of field for delimiting
If cboFieldName4.Column(1) >= 1 And cboFieldName4.Column(1)
<= 7 Then
' if the field holds numeric data
strWhereIN4 = strWhereIN4 & " " &
lstFieldValues4.Column(0, i) & ","
ElseIf cboFieldName4.Column(1) = 8 Then
' if the field is a date
strWhereIN4 = strWhereIN4 & "#" &
lstFieldValues4.Column(0, i) & "#,"
ElseIf cboFieldName4.Column(1) = 10 Then
'if the field is text
strWhereIN4 = strWhereIN4 & "'" &
lstFieldValues4.Column(0, i) & "',"
End If
End If
Next i
' creates the -WHERE- string, stripping off the last comma of the
-IN- string
strWhere4 = " WHERE " & Me!cboFieldName4 & " in (" &
Left(strWhereIN4, Len(strWhereIN4) - 1) & ")"
'MsgBox strWhere4
' if "All" was selected then don't add the -WHERE- condition
If Not flgAll Then
strSQL = strSQL & strWhere4
End If
'MsgBox strSQL
MyDB.QueryDefs.Delete "qryLocalAuthority"
Set qdf = MyDB.CreateQueryDef("qryLocalAuthority", strSQL)

DoCmd.OpenReport "qryLocalAuthority", acPreview
Exit_cmdRunReport_Click:
Exit Sub

Err_cmdRunReport_Click:
If Err.Number = 3265 Then '*** if the error is the query is
missing
Resume Next '*** then skip the delete line and
resume on the next line
ElseIf Err.Number = 5 Then
MsgBox "You must make a selection"
Resume Exit_cmdRunReport_Click
Else
MsgBox Err.Description '*** write out the error and exit
the sub
Resume Exit_cmdRunReport_Click
End If

End Sub

Jan 18 '07 #1
1 2382
I would probably write a function to return a simple filter. Check the
ItemsSelected.Count property of the listboxes and build the filter
accordingly with IN. Then when you get all the parts assembled into
the function, return the string and pass it in the Open event of the
report.
There are examples of using listboxes to filter reports at Accessweb.
www.mvps.org/access

Jan 18 '07 #2

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

Similar topics

4
by: Greg Iocco | last post by:
Simple problem, but I've checked the newsgroups and couldn't find an answer. On the main swithboard is a command button to open a report. The...
2
by: Justin Koivisto | last post by:
Firstly, I want to thank all that have helped me out with getting grips on Access each time I've had questions. This has got to be one of the most...
2
by: DC Gringo | last post by:
I have two listboxes, the first of which is an autopostback=true that allows multiple row selection. When I select multiple values (by holding down...
4
by: schmidtmic | last post by:
I have a table that contains 20 checkbox fields. Each checkbox corrisponds to an Score amount. For Example: Checkbox1 is worth 10 points...
1
by: colleen1980 | last post by:
There is a form where it ask the two dates and then run a query then report. Private Sub Command36_Click() DoCmd.OpenQuery "qryResultsReport"...
2
by: salad | last post by:
This is a tip on how to speed up listboxes DRAMATICALLY. Persons that would benefit are those that are constantly updating the rowsource of a...
4
by: Phille | last post by:
Hi I have a form with a few textboxes and some listboxes with multiple values allowed. I would like to use an append query to insert everything...
4
by: gazsharpe101 | last post by:
Hi, I have got 4 separate listboxes on a report and dependant on the content of the report, I wish to display a message box to inform the user of...
10
by: Trevor2007 | last post by:
I have a query that setting date from and date to from form values works: >=!! And <=!! but now I am trying to add another peramiter to pass...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

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.