PDA

View Full Version : Report with Parameter Query



Imdabaum
03-16-2010, 10:08 AM
Okay, I've seen some examples of where people automate parameters in a query
IE:
'First we'll set some variables:

Dim qdf As Querydef
Dim rst As Recordset

'then we'll open up the query:

Set qdf = CurrentDB.OpenQueryDef(qryName)

'Now we'll assign values to the query using the parameters option:

qdf.Parameters(0) = qryStartDate
qdf.Parameters(1) = qryEndDate

'Now we'll convert the querydef to a recordset and run it

Set rst = qdf.OpenRecordset

'Run some code on the recordset

'Close all objects
rst.Close
qdf.Close
Set rst = Nothing
Set qdf = Nothing

What if I want to automate a report that uses a parameter query? Is there a way to do that? The report doesn't seem to have the parameter properties.

Background: I have one query that opens from a custom ribbon. The users enter the name of the person for which they want the report. Yesterday the boss told me I needed to have an individual report for all vendors that looks the same and can be emailed automatically. Rather than copy and paste 24 reports and 24 queries. I was hoping that I could just associate the report with the person it's being emailed to and iterate through the names for the report.

Anyone ever done something like this?

OBP
03-17-2010, 03:53 AM
Yes, I have posted a few databases that iterate through a "Selected" list of Recipients. If you want to be able to select only some of the recipients then your table needs an "email" field, however if you want to send it to everybody in the table that field is not required.
This is an example of the VBA that uses an "emailing form to enter the Subject and a few lines of the email Body and sends them to a Selected List. It also Sends an Attachment, which you probably don't need.
Dim subject As String, Body As String, EmailAddress As String, objOutlook As New Outlook.Application
Dim count As Integer, rs As Object, counter As Integer, recount As Integer, objMessage As MailItem, strAttach As String
subject = Me.Text0
strAttach = Me.Attachment
Set rs = CurrentDb.OpenRecordset("Contestants emails")
rs.MoveLast
recount = rs.RecordCount
rs.MoveFirst
For count = 1 To recount
EmailAddress = rs![email Address]
Body = "Dear " & rs.Fullname & Chr$(13) & Chr$(13) & [Text1] & Chr$(13) & [Text2] & Chr$(13) & [Text3] & Chr$(13) & [Text4]
Set objMessage = objOutlook.CreateItem(olMailItem)
With objMessage
.To = EmailAddress
.subject = subject
.Body = Body
.Attachments.Add strAttach
.Send
End With
Set objOutlook = Nothing
Set objMessage = Nothing
With rs
.Edit
!email = 0
.Update
.Bookmark = .LastModified
End With
If rs.EOF Then Exit For
rs.MoveNext
Next count
rs.Close
Set rs = Nothing

Imdabaum
03-17-2010, 07:51 AM
I proposed a different solution to the employer and he accepted it so I've got one report that takes a parameter and is opened from the ribbon toolbar and a duplicate for emailing purposes without parameters, but which is filtered as I iterate through the employee records. I don't know why I didn't think of it before I posted, but sometimes that's the way my mind works. Plus I had just built 24 duplicate reports after trying to convince him there was an easier way... creating another report just seemed overkill... But this solution allowed me to delete the other 24 reports and their associated queries so yay for trimming it down i guess.

Thanks OBP for the tip.