Listbox values to report

brsawvel

Registered User.
Local time
Today, 17:28
Joined
Sep 19, 2007
Messages
256
Hello,

I have a listbox that displays records drawn from a query (so the records aren't always the same, although the table drawn from is). Is there a code to cause a "Create Report" button to send the records displayed in the listbox to a report for emailing or printing?
 
If your listbox is BASED on the query, then creating a report based on the same query is the same as creating a report based on the listbox, so no code would be needed.

However, it is a different story if the Box's RowSource changes due to a WHERE clause, or something like that. I only say this because I'm not sure what you meant when you said...
I have a listbox that displays records drawn from a query (so the records aren't always the same)
 
Last edited:
you're right. I don't have an actual "query" that I draw from. Sorry about being vague. I have a few buttons where, when one of them is clicked, the listbox records change because I've written an "On click" command that overrides the original rowsource.

(example):

Me.listbox.rowsource = SELECT [tbl1].[fldSite], [tbl1].[fldFloor], [tbl1].[Location] WHERE [tbl1].[fldFloor] = "2nd"

I'm only guessing, but is it possible to write a cmd that can tell the report to mirror the new listbox rowsource? Or is there a better way?
 
Have you looked at Lister's FAQ?? He wrote a For, Next Loop to run a report off Listbox Selections.

You might also want to check out using your form as an Index for the .Selected or .Selection property in Basic.

Another option too (which might make things easier - if you don't have an infinate number of rowsources that the listbox could be changed to)...

Create different queries for each rowsource possibility of the listbox. Then, instead of changing the rowsource of the box via the button, change the controlsource (to whatever query holds the proper rowsource for what you want for the box). If the number of rowsources possible is low, this would be a good option, because then you would only have one report, and just a couple lines of code when it opens...
Code:
Report OnLoad Event

  Me.RecordSource = "querythatlistboxisbasedoffof"
 
I've seen that example before, but it isn't quite what I'm looking for. It's close though. Lister has his(her?) set up to generate a report based on selections made from a listbox. Mine isn't a "multiple selection" type. I'm looking for mine to generate a report based on what appears on the listbox at that point by a command button.

I'd like to do the code you have (I will for other reports), but this one has too many possibilities.
 
I know that's what you are trying to do...for it to work for you, all you would have to do is write code to select all values in the list box, and then run the report. See what I mean??

That's the only extra step that you would need. I only say this, because I doubt something like this would work (obviously the different sides of the equality are not compatible :) )...
Code:
ActiveReport(Me).Recordsource = Forms!FormName!ListBox.RowSource
 
ahhhhh.....

is the code you show the needed addition?

brsawvel <-----New at Access, VB, and SQL
 
No it's not...the line in my last post is the only line you would need if Access Basic was a "perfect world" language. :)

The line in the post before is the line that would be executed after you selected all the items in the list box with VB code.
 
Ok, so I was able to find a code that selects all fields in a listbox -

For i = 0 To Listbox.Listcount = -1
Me.Listbox.Selected(i) = True
Next i

But, in order for that to work, the MultiSelect has to be in Extended or Simple. I have the default of the listbox MultiSelect set to None, and would like to keep it that way until I use the command button to change its settings (I have a good reason, but too much to explain).

I tried using this code -

Me.Listbox.MultiSelect (Extended)

But it comes back with an "invalid use of property" error. Do you know what I'm doing wrong?

I also tried

Me.Listbox.MultiSelect = "Extended"

Me.Listbox.MultiSelect = Extended

Me.Listbox.MultiSelect = 'Extended'

All come back with their own errors.

Edit -

I also tried

If Listbox.MultiSelect = ("0") Then Listbox.MultiSelect = ("2")
 
Last edited:
Sawvel, where did you find your little code bit?? Did you see any code like the following with it....??

Anyway it might just work, try it.
Code:
Public Function listboxTEST(ListBoxName As ListBox) As Boolean

Dim i As Long

  If ListBoxName.MultiSelect Then

    For i = 0 To ListBoxName.ListCount = -1
    ListBoxName.Selected(i) = True

    Next

  ListBoxTEST = True

End If

End Function
Then...
Code:
Private Sub CommandButton_Click

  Call listboxTEST(Forms!ListBoxForm!ListBoxName)

End Sub
 
I stole it from "the scripts" website. That part works for my application, but what I ended up doing for the issue of changing from none to multiselect is I created a hidden listbox with all the same columns. So when I click the command button, it will draw from the hidden listbox and put it into a report.
 
I tried sending this message to Lister, but it's too long....
Hello Lister,

I am currently attempting to create a copy of your example db but am having problems displaying it in a report.

First thing, though, my listbox is drawing its data from a tbl using a query statement rather than from a physical query.

I have a code behind my button that first tells the listbox to select all records (That part is working properly). Then I added your code below the first code. When I hit the button I get an "enter parameter" message for each field in my table.

Below is the code I am using:

Private Sub CreateReport_Click()

'First code to select all in listbox
For i = 0 To Listbox.Listcount = -1
Me.Listbox.Selected(i) = True
Next i

'Your code modified
On Error GoTo ErrHandler

Dim ctlSource As Control
Dim intCurrentRow, intStrLength As Integer
Dim strHolder As String
Dim vVal As Variant

Set ctlSource = Me.lstListedAMHidden

For intCurrentRow = 0 To ctlSource.ListCount - 1
If ctlSource.Selected(intCurrentRow) Then
vVal = ctlSource.Column(0, intCurrentRow)
End If
If vVal <> Empty Then
strHolder = strHolder & vVal & " Or tblAM.fldAssetID = "
vVal = Empty
End If
Next intCurrentRow

If strHolder <> ""
intStrLength = Len(strHolder) - 34
strHolder = Left(strHolder, intStrLength)
strHolder = " tblAM.fldAssetID = " & strHolder
DoCmd.OpenReport "rptListed", acViewPreview, , strHolder
Else 'If nothing was selected, run message.
MsgBox "A report cannot be generated", vbInformation, "Listbox Empty"
Me.lstListedAMHidden.SetFocus
End If
ExSub:


Do you see anything that could explain why it isn't working.
 
I would have no idea. What's the error message?? Anything??

How about this change...??
Code:
For intCurrentRow = 0 To ctlSource.ListCount - 1
If ctlSource.Selected(intCurrentRow) Then
vVal = ctlSource.Column(0, intCurrentRow)
End If
If [COLOR="Red"]vVal <> Empty [/COLOR]Then <---substitute the [COLOR="Red"]red[/COLOR] for this...[code]Len(vVal) > 0

strHolder = strHolder & vVal & " Or tblAM.fldAssetID = "
vVal = Empty
End If
Next intCurrentRow[/code]
 

Users who are viewing this thread

Back
Top Bottom