Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print a Report based solely on the Current Record in a Form 1

Status
Not open for further replies.

adamdunko

Technical User
Apr 18, 2003
10
US
Hello, I am not a programmer so keep that in mind. What I am trying to do is include a button on my form, whereas when pressed, it will print a report that includes only data from the current record on the form.

I feel like there is a way to do this with a query and macros, but I really can't figure it out. Please help with any suggestions.
 
1) put a button on your form. Follow the wizard to Preview your report.
2) In the design mode of the form, view the Properties window, and click the button. In the OnClick event of the button, click the build button (the little button to the right of the event with the three dots on it). It will open up to the code the wizard wrote for you. It will look something like this:
Code:
Private Sub Command6_Click()
On Error GoTo Err_Command6_Click

    Dim stDocName As String

    stDocName = "ActualsByBoard"
    DoCmd.OpenReport stDocName, acPreview

Exit_Command6_Click:
    Exit Sub

Err_Command6_Click:
    MsgBox Err.Description
    Resume Exit_Command6_Click
    
End Sub

This will open your report.

3) Since you want to only see the current record, you'll need to add to the code above. You'll be adding what is called a "where" string to tell it to open the report to only one record. Assuming the ID field on your form is called "ID", and the ID field in the table or query that your report is based on is called "ReportID", the code will look like this:

Code:
DoCmd.OpenReport stDocName, acPreview,,"[ReportID] = " & me.ID

So that's how you tell it to open the report for only certain criteria.

(if you didn't know, the keyword "me" refers to the form or report object you're sitting on at the moment).

If you simply type a comma after the line of code that the wizard wrote for you (DoCmd.OpenReport stDocName, acPreview) you will see it prompting you for more info. type a second comma, and it will want the WHERE string, which is what i wrote above. If some place you type the word me. (me and dot/period) you will see it pop up with a list of things you can choose, some of which are the fields you have in the form.

Hope this helps.

g



Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
That worked well! Since I wanted just to print the report, I followed that wizard instead and added the:

,,"[ReportID] = " & me.ID

to that line and it printed. Incidentally, I had to add a unique identifying autonumber field to the table and to the form (made it invisible). But it worked! Thank you so much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top