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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A button to open a report on a specific record.

Status
Not open for further replies.

Simplemind

IS-IT--Management
Dec 10, 2002
3
ES
Hi,
i have a form with a related report i have placed a button in the form to open the report. whenever i click the button the report always opens on record 1. however i want the report to open on the record that is currently displayed on the form. the code for the button is as follows

Private Sub cmdPreviewPartOutReport_Click()
Dim stDocName As String
stDocName = "rptPartOutReport"
DoCmd.OpenReport stDocName, acPreview
Any suggestions as to what i have to change in this code would be greatly appreciated, Thanks
 
I had the same problem with my database. Apparently Access doesn't have any feature that would make this easier. I have a lot of forms which I need to link together, by various fields with various data types. I designed two pieces of code - one to go in the form that you are opening from, and one to go in the target form. They look like this:

In the form:
Private Sub cmdPreviewPartOutReport_Click()
Dim strTargetField As String
Dim varTargetData As Variant
Dim strTargetType As String


strTargetField = "Primary Key"
strTargetType = "int"
varTargetData = Me![Primary Key].Value

DoCmd.OpenReport "rptPartOutReport", acPreview
Reports![rptPartOutReport].OpenFormTo strTargetField, varTargetData, strTargetType

End Sub

In the report:
Public Sub OpenFormTo(strTargetField As String, varTargetData As Variant, strTargetType As String)
Dim intTargetData
Dim strTargetData

DoCmd.GoToControl strTargetField

Select Case strTargetType
Case "int"
intTargetData = varTargetData
DoCmd.FindRecord intTargetData
Case "str"
strTargetData = varTargetData
DoCmd.FindRecord strTargetData
End Select
End Sub

For your purposes, this can probably be simplified to the following

Private Sub cmdPreviewPartOutReport_Click()
Dim strTargetField as String
Dim intTargetData as Integer
strTargetField = "Primary Key"
intTargetData = Me![Primary Key].Value

DoCmd.OpenReport "rptPartOutReport", acPreview
DoCmd.FindRecord intTargetData
End Sub


The main reason I use the OpenFormTo procedure is because I don't trust variable data types to return the correct type. It also allows me to put that procedure in every form. It's fiddly but it works. The second code slice is much cleaner and should work fine. I haven't tried it on reports before, but I think it should work the same. Good luck



 
try this one

Dim stDocName As String
Dim stLinkCriteria As String
stLinkCriteria = "[RefID]=" & Me![RefID]
stDocName = "rptprintsingle"
DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria

Make a command button on your form. Put this code in the onclick event.

note: "rptprintsingle" = Name of report
refid is the primary key on the form.

there does not need to be anthing added to your report.
P.S this is a great way to print single mailing labels. If you use a dot matrix printer it's easy..if you use sheet labels then you will need that label saver code. (free)

fl


 
sorry, this line needs to be added before the findrecord
DoCmd.GoToControl strTargetField
 
Thanks alot for the help fellas, I will give them a try.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top