Generate Dynamic Report from Listboxes?

magster06

Registered User.
Local time
Today, 12:51
Joined
Sep 22, 2012
Messages
235
Hello All,

Is it possible to generate a report from 2 listboxes?

I have attached a pic of what I am trying to accomplish. I am wanting to have a report open with only the criteria that a user selects from the listboxes.

listbox 1: User selects a case number(s)

listbox 2: User selects the fields they would like in the report

So, if the user selects:

Case number: 13-001

Then selects fields:

Allegation
Incident date
Focus_Last name

his report would be:

Code:
Case Number      Allegation     Incident Date     Focus_Last Name
13-001              DUI              06/01/2013        Doe
 

Attachments

  • Listbox Report.png
    Listbox Report.png
    21.2 KB · Views: 107
Do you want it as a Report? Or would you settle for a Query? Because a Report needs to be defined, a Query as you could see can be generated dynamically..
 
Do you want it as a Report?

Yes, I am trying to generate a dynamic report, so the users can pick what information to include in the report.

I am trying to adapt another members code (from the code bank) , Lister, to fit my needs.

Here is his code:

Code:
Private Sub cmdOpenReport_Click()
'Lister 07 Aug 2008, Waipawa New Zealand
    'Loops through the values in the list box to produce a
'string criteria to filter a report.
'The loop is the important bit. You could mod' the code
'to add values to a table or combo box etc
'I have changed the code in this example so its simpler to use.
'I have scraped the "Or" statment and replaced it with a "in("value","value","value")" statment, which is a lot cleaner.
'So you have a choice of either now.
On Error GoTo ErrHandler
    'Set variables
    Dim ctlSource As Control
    Dim intCurrentRow, intStrLength As Integer
    Dim strHolder As String
    Dim vVal As Variant
    
    Set ctlSource = Me.List2 'set control source to look at the list box
    
    For intCurrentRow = 0 To ctlSource.ListCount - 1 'Loop until the end of all the items in the list box
        If ctlSource.Selected(intCurrentRow) Then 'If item selected in list box, step into the if statment
            vVal = ctlSource.Column(0, intCurrentRow) 'set vVal to the bound value in the list box for this selected item
        End If
        If vVal <> Empty Then 'If vVal is not "Empty" (it has a value) step into this if statment.
            strHolder = strHolder & vVal & ","
            'Add vVal and it's needed extra string (",") to it's self.
            vVal = Empty 'reset vVal to Empty
        End If
    Next intCurrentRow 'Loop to next itme in the list
    
    'If only one item is selected your string will look something like
    '"5,"
    'If you have more items selected it will look like this
    '"5,2,"
    'Now we need to chop off the unnecessary end of the string (",")
   If strHolder <> "" Then 'Check to make sure an item was selected.
        intStrLength = Len(strHolder) - 1 'We count the full length of the string
        strHolder = Left(strHolder, intStrLength) 'And remove the unnecessary bit.
       strHolder = " QCase.CaseID in (" & strHolder & ")" 'We add the required bit of the string to the front of the string
               DoCmd.OpenReport "Case Report", acViewPreview, , strHolder 'And use it in the criteria of the open report method
    Else 'If nothing was selected, run message.
        MsgBox "You should select a Utensil from the list", vbInformation, "No Item Selected"
        Me.List2.SetFocus
    End If
ExSub:
Exit Sub
ErrHandler:
    MsgBox "Error Number: " & Err.Number & " - " & Err.Description, vbInformation, "Opps: ERROR!"
    GoTo ExSub
End Sub

But I cannot get it to work. It keeps throwing error 3075 Syntax error (missing operator) in Query expression



Hmm, now that I am taking a closer look at Listers code, it is drawing the information for his report from the information already populated in his listbox.

I need the information (records) from the listbox for the fields listed.

Listers code is still useful for looping through the listbox. I think I need to create a sql statement to pull the records.
 
Last edited:
You may get some tips from Martin Green's site.
see
http://www.fontstuff.com/access/acctut19.htm

and other tutorials on the site.

The other approach is to have a series of canned reports; and the user selects the canned report but may include some parameters to limit DateFrom, DateTo,etc.
 
You are welcome, glad to help.
There is a lot of good info at Martin's site -- he does a nice job of putting SQL and vba together.
 

Users who are viewing this thread

Back
Top Bottom