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!

Printing Current Records From a Form

Status
Not open for further replies.

Gabbie

Technical User
Dec 19, 2001
8
US
I have a data form which I generated straight from a table. I have been trying to create a button on my form which would print (or print preview) a report of only the current record. I have been researching old posts and have tried to use some of the codes that were given earlier but I can't get any of them to work for me. The following code will open the report but it always goes back to the first record:

Sub
Dim stDocName As String
Dim LinkFilter As String

LinkFilter = "DEMProjectID=" & Me![DEMProjectID]
stDocName = "printrecords"
DoCmd.OpenReport stDocName, acViewPreview, LinkFilter
End Sub

Alternately I have also tried this other code which will open one record but with no data in it-it just shows the labels:
Sub
Dim stDocName As String

DoCmd.OpenReport "printrecords", acViewPreview, , "DEMProjectID =" & Forms![DEM Project Tracking Database]!DEMProjectID
End Sub

There is probably a simple explanation like a syntax error but I can't seem to straighten it out. I have wasted days fooling around with this and any suggestions would be greatly appreciated.
 

From what you have said you are applying a filter to your report so only the record from your form will print. The filter on property of a report is turned off by default. My preference is to turn it on in code. Try setting the filter on property of the report properties to yes and try that first. If it works, please let me know because I’ve never done this except in code. If it does not work, in the reports on open event, insert this line.

Me.filteron = true

Robert Berman

 
I am assuming that DEMProjectID is the Primary Key in your table and that it is the name of the field on your form.

I am going to call your report button cmdPrint (you can just replace the word cmdPrint with the real name of your button)

Private Sub cmdPrint_Click()
Dim intProjectId as integer

intProjectId = me.DEMProjectID 'This captures the primary
'key for the record

docmd.OpenReport "PrintRecords",acViewPreview ,,DEMProjectId = intProjectId

End Sub

This should open it up in Preview. If you want it to just print to the printer you can change the acViewPreview to acViewNormal.

Let me know if you still have problems.
 
Thanks so much for the replys,
Unfortunately I have tried all of the above and in all cases I now get a print preview of a blank record where it has the labels showing but no data. This makes me think that my problem isn't necessarily with my code but in my table or form or report somewhere. Sigh.
 
Hi Gabbie,

This is what I use, and it works well - sending an individual record to Print Preview. I've not installed error trapping -- meaning that if, in this usage -- an Employee does not have a CPP on file, it still goes into Print Preview, rather than return a message saying that there are no records. BTW, this code goes with the CLICK property of a command button on the main form.

*************** Beginning of Code ***************

Private Sub Command25_Click()
On Error GoTo Err_Command25_Click

' Command button allows viewing and printing of an individual CPP.

Dim stDocName As String

stDocName = "r_Employee_And_CPP" '(This is the name of the report).

' Open the report, keying on Employee ID number.
DoCmd.OpenReport _
"r_Employee_And_CPP", acPreview, "", "[Forms]![f_Employee_CPP]![EmployeeID]=[q_Employees_And_CPP]![EmployeeID]"

Exit_Command25_Click:
Exit Sub

Err_Command25_Click:
MsgBox Err.Description
Resume Exit_Command25_Click

End Sub

*************** End of Code ***************
 
Here is one that I use:

DoCmd.OpenReport "ReportName", acViewPreview,, "[FieldName1] = " & Forms![FormName]![FieldName1]

Try that.
 
Gabbie, I missed telling you some info in my last reply:

You need to make sure you have a auto number field set up called something like "ID". That way a specific number would be tied to a specific record, thus retrieving the record you need to preview or print:

DoCmd.OpenReport "rpt_Report1", acViewPreview,, "[ID]=" & Forms![frm_Form1]![ID]

Sorry about the last reply:)
 
After using a combination of suggested codes posted to this question, I finally got the button to work yesterday. Hurrah!!! This forum is great-Thanks everybody!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top