My Assistant
![]() ![]() |
|
|
Mar 15 2012, 09:43 AM
Post
#1
|
|
|
UtterAccess Veteran Posts: 461 From: Austin, TX |
I have a form (frmEnvDocs) (with no record source associated) that has a List Box (named EnvDocs) with row source shown below.
SELECT DISTINCT tblTypeEnvProcess.TypeEnvProcess FROM tblTypeEnvProcess UNION select "All" from tblTypeEnvProcess; It also has a command button on it. The button opens a date-prompter form named frmNumberEnvDocsApprvdDatePrompter. The date prompter form has two unbound fields on it - one for Start Date and one for End Date. This form also has a command button that the user clicks once they have selected the start and end dates. The OnClick event procedure for this command button executes the VBA Code shown below. ************** Private Sub cmdOpenQuery_Click() On Error GoTo Err_cmdOpenQuery_Click Dim MyDB As DAO.Database Dim qdef As DAO.QueryDef Dim i As Integer Dim strSQL As String Dim strWhere As String Dim strIN As String Dim flgSelectAll As Boolean Dim varItem As Variant Set MyDB = CurrentDb() strSQL = "SELECT * FROM qryNumberofEnvDocsApprvd" 'Build the IN string by looping through the listbox 'EnvDocs is the name of the unbounded List Box with row source: 'SELECT DISTINCT tblTypeEnvProcess.TypeEnvProcess FROM tblTypeEnvProcess UNION select "All" from tblTypeEnvProcess; For i = 0 To EnvDocs.ListCount - 1 If EnvDocs.Selected(i) Then If EnvDocs.Column(0, i) = "All" Then flgSelectAll = True End If strIN = strIN & "'" & EnvDocs.Column(0, i) & "'," End If Next i 'Create the WHERE string, and strip off the last comma of the IN string strWhere = " WHERE [TypeEnvProcess] in (" & Left(strIN, Len(strIN) - 1) & ")" 'If "All" was selected in the listbox, don't add the WHERE condition If Not flgSelectAll Then strSQL = strSQL & strWhere End If MyDB.QueryDefs.Delete "qryNumberofEnvDocsApprvd2" Set qdef = MyDB.CreateQueryDef("qryNumberofEnvDocsApprvd2", strSQL) 'Open the query, built using the IN clause to set the criteria DoCmd.OpenQuery "qryNumberofEnvDocsApprvd2", acViewNormal 'Clear listbox selection after running query For Each varItem In Me.EnvDocs.ItemsSelected Me.EnvDocs.Selected(varItem) = False Next varItem Exit_cmdOpenQuery_Click: Exit Sub Err_cmdOpenQuery_Click: If Err.Number = 5 Then MsgBox "You must make a selection(s) from the list", , "Selection Required !" Resume Exit_cmdOpenQuery_Click Else 'Write out the error and exit the sub MsgBox Err.Description Resume Exit_cmdOpenQuery_Click End If End Sub ************ The query qryNumberofEnvDocsApprvd is made up of the primary table (Projects) and five other related tables (tblRoutes, tblCounties, tblDistricts, tblEnvDocs, and tblTypeEnvProcess). The query selects all approved environmental documents (DateApproved field in table tblEnvDoc) within a particular time period (the Date Prompter mentioned above). The time period is the criteria shown below on the DateApproved field in the query. Between [Forms]![frmNumberEnvDocsApprvdRptDatePrompter]![txtStartDate] And [Forms]![frmNumberEnvDocsApprvdRptDatePrompter]![txtEndDate] If I run the query directly (without using any forms), it asks me for the start and end dates and then runs the query just fine. However, when I use the forms with the VBA code above, I get a compile error (variable not defined) and it opens the VBA code in question and highlights the term EnvDocs in the following line of code: For i = 0 To EnvDocs.ListCount - 1 I suspect I have something in the code out of order but can't see the forest for the trees. Can anyone offer any help. Thanks in advance for your time. |
|
|
|
Mar 15 2012, 10:07 AM
Post
#2
|
|
|
UtterAccess VIP Posts: 8,138 From: CT |
if it is a multiselect listbox you need to use items selected like this to test the count of items selected
yourlistbox.ItemsSelected.Count > 0 |
|
|
|
Mar 15 2012, 10:13 AM
Post
#3
|
|
|
UtterAccess Veteran Posts: 461 From: Austin, TX |
Where would I insert that in the code? Thanks agian.
|
|
|
|
Mar 15 2012, 10:28 AM
Post
#4
|
|
|
UtterAccess VIP Posts: 8,138 From: CT |
it isnt an INSERT into your existing code it s a REPLACEMENT example.
you have these lines... CODE For i = 0 To EnvDocs.ListCount - 1 If EnvDocs.Selected(i) Then i am saying it should be something like this. CODE for i = 0 to EnvDocs.ItemsSelected.Count
If EnvDocs.itemsSelected(i) Then |
|
|
|
Mar 15 2012, 10:31 AM
Post
#5
|
|
|
UtterAccess Ruler Posts: 1,090 |
try retyping this line:
For i = 0 To EnvDocs.ListCount - 1 but after the word "To " type Me., the intellisense will prompt you for the correct object name. |
|
|
|
Mar 15 2012, 10:39 AM
Post
#6
|
|
|
UtterAccess Veteran Posts: 461 From: Austin, TX |
To Bob G. I tried your replacement code but got the same compile error message.
|
|
|
|
Mar 15 2012, 10:44 AM
Post
#7
|
|
|
UtterAccess Veteran Posts: 461 From: Austin, TX |
To arnelgp. Did you mean like this:
For i = 0 To Me.EnvDocs.ListCount - 1 If so, I get the compile error message "Method or Data Member Not Found" and it highlights the term ".EnvDocs" in the line of code. I may have type it incorrectly. |
|
|
|
Mar 15 2012, 11:03 AM
Post
#8
|
|
|
UtterAccess VIP Posts: 8,138 From: CT |
then EnvDocs is not the name of the listbox
|
|
|
|
Mar 15 2012, 12:50 PM
Post
#9
|
|
|
UtterAccess Veteran Posts: 461 From: Austin, TX |
I have attached a sample database for y'all to look at. If you run the query without a selected range of approval dates, then it runs fine. However, when you run the query with the range of approval dates, you get a compile error message. I borrowed the VBA code from the internet at http://www.databasedev.co.UK/query_using_listbox.html and modified it to fit our situation. However, in their VBA code string strSQL = "SELECT * FROM ___________", they were using a table where I am using a query. Any other help is greatly appreciated. Thanks in advance.
Attached File(s)
|
|
|
|
Mar 15 2012, 04:49 PM
Post
#10
|
|
|
UtterAccess VIP Posts: 8,138 From: CT |
the answer is that you have to use the FORM qualifier as you are calling the listbox that resides on one form from the onlick event of the another form
|
|
|
|
Mar 16 2012, 03:55 AM
Post
#11
|
|
|
UtterAccess Ruler Posts: 1,090 |
|
|
|
|
Mar 19 2012, 08:52 AM
Post
#12
|
|
|
UtterAccess Veteran Posts: 461 From: Austin, TX |
Many thanks to arnelgp and Bob G for spending their valuable time to help me out. The fix arnelgp provided works great, and now I have a new twist to the challenge. I would like to have two other command buttons on the form frmEnvDocs that would perform a print/preview of a report with and without a selected range of approval dates. The report in question is rptApprvdEnvDoc.
I have attached a revised copy of the sample database for your reference. I sincerely appreciate any help with this added feature. Thanks in advance.
Attached File(s)
|
|
|
|
Apr 6 2012, 03:10 PM
Post
#13
|
|
|
UtterAccess Veteran Posts: 461 From: Austin, TX |
No one interested enough to answer? Thanks in advance for your valuable time.
|
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 23rd May 2013 - 01:27 AM |