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

Printing current record as a report 3

Status
Not open for further replies.

SBelyea

Technical User
May 29, 2008
46
US
This is driving me absolutely nuts!!!

Within my database, you can open a form based off of tbl_streets that has a bunch of different data. tbl_streets has a PK named SID (an AutoNumber). I want to be able to print everything I can see in this form view (data from tbl_streets, tbl_maintenance, tbl_distance) as a report. My current (not working) method has been to create a report based off of all three tables, and then attempt to print a report based on the SID of the current record being viewed. I've only had dismal failures trying this. Here is my current code:

Code:
Private Sub Image13_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
On Error GoTo Err_Image13_MouseUp
'   Special Effect (Flat)
    Image13.SpecialEffect = 0
'   Open Form
    Dim strDocName As String
            
    strDocName = "rpt_streets"
        
    DoCmd.OpenReport strDocName, aViewNormal, "qry_rpt_streets"

Exit_Image13_MouseUp:
    Exit Sub

Err_Image13_MouseUp:
    MsgBox Err.Description
    Resume Exit_Image13_MouseUp
    
End Sub

My report is called rpt_streets.
 
Can't you just create the report from the form, instead of from the tables? It seems I remember the Report wizard giving the option to build from a form..

--

"If to err is human, then I must be some kind of human!" -Me
 
Thanks for the heads up - I tried that option, however, and it doesn't return what I need. I'm using a report built from the ground up because I need to show multiple attached records for each database entry - with my form saved as a report, it still only shows single attached records for each entry.

Is there a simple way to pass my SID from my form to my report so that I can print my specified record?
 
At least on a form, not 100% sure with a report, you can change the layout to "Tabular" which shows more than one record.

--

"If to err is human, then I must be some kind of human!" -Me
 
Thanks for the heads up dhookom! I tried to use this code, but I get prompted for my SID value each time I try to run it. Here's my code:

Code:
Dim strDocName As String
Dim strWhere As String
    strDocName = "rptSomeReport"
    strWhere = "[SID]=" & me!SID
    DoCmd.OpenReport strDocName, acPreview, , strWhere

There is an SID value on my form, as well as an SID field on my report. The SID field is an autonumber and a PK. Do I need to change the syntax in the code to make this work?
 
Try changing [blue]me![/blue] to the actual path of the object, something like:

[blue]Forms!frmMyForm[/blue]!SID

Or

[blue]Reports!rptSomeReport[/blue]!SID

--

"If to err is human, then I must be some kind of human!" -Me
 
And if it is a text/string value, and not numeric, then you may need to add in apostrophes, something like this:

Code:
strWhere = "[SID]= [highlight]'[/highlight]" & me!SID [highlight]& "'"[/highlight]

--

"If to err is human, then I must be some kind of human!" -Me
 
'Course, I meant "single quotes", but it's all the same on the keyboard. [wink]

--

"If to err is human, then I must be some kind of human!" -Me
 
SBelyea stated SID is an autonumber primary key. That suggests the value is numeric.

I would confirm there is actually a field named SID in the record source of the form and the report.

Duane
Hook'D on Access
MS Access MVP
 
SBelyea stated SID is an autonumber primary key. That suggests the value is numeric.
[blush]
Must've missed that part - yeah, on the very first line. [blush]

--

"If to err is human, then I must be some kind of human!" -Me
 
kjv1611:

I tried your changes in the code (explicitly naming my form and report) to no avail. When I changed it to

Code:
 strWhere = "[SID]=" & Forms!frm_street_name!SID

I am still prompted for an SID value, and even after I put one in the report shows up blank.

For the code

Code:
strWhere = "[SID]=" & Reports!rpt_streets!SID

I receive an error stating that "rpt_streets" does not exist (although it most certainly does it exist.]

dhookom:

The report has the SID field from tbl_streets on it, and the form was constructed based on tbl_streets. I do have these fields not visible, but they do exist on the form. Would this make a difference?


Thanks for all of you help you two have already provided - I have no idea why this won't work!
 
If it is not open, it does not exist in the reports collection. Is SID a field in the report? Is your report based on a query that asks for an SID?

strWhere = "[SID]=" & Forms!frm_street_name!SID

Would normally be correct.

 
Here is the record source for my report:

Code:
SELECT [tbl_street_name].[SID] AS tbl_street_name_SID, [tbl_street_name].[street_name], [tbl_street_name].[DID] AS tbl_street_name_DID, [tbl_distance].[DID] AS tbl_distance_DID, [tbl_distance].[from], [tbl_distance].[to], [tbl_distance].[length], [tbl_distance].[SID] AS tbl_distance_SID, [tbl_maintenance].[MID], [tbl_maintenance].[type_of_maintenance], [tbl_maintenance].[date_planned], [tbl_maintenance].[date_completed], [tbl_maintenance].[maintenance_evaluation], [tbl_maintenance].[pavement_evaluation], [tbl_maintenance].[DID] AS tbl_maintenance_DID, [tbl_maintenance].[pavement_notes], [tbl_maintenance].[maintenance_notes], [tbl_distance].[width], [tbl_maintenance].[paved_width] FROM tbl_street_name INNER JOIN (tbl_distance INNER JOIN tbl_maintenance ON [tbl_distance].[DID]=[tbl_maintenance].[DID]) ON [tbl_street_name].[SID]=[tbl_distance].[SID];

Could it be (perhaps) that since my [tbl_street_name].[SID] is stored as tbl_street_name.SID?
 
Looks like you should then be referencing either

[blue]tbl_street_name_SID[/blue]
or
[blue]tbl_distance_SID[/blue]

--

"If to err is human, then I must be some kind of human!" -Me
 
You have an alias on the name. Try:


strWhere = "tbl_street_name_SID=" & Forms!frm_street_name!SID


 
You guys are absolutely amazing!!!!!

Changing the line of code to
Code:
strWhere = "tbl_street_name_SID=" & Forms!frm_street_name!SID

worked!

Remou, kjv1611 and dhookom, thanks for helping me figure this out! I appreciate all of the help you provided! Shiny stars all around to this crowd!
 
Or if your code is Form-Level, and will never be anything but, then you can just do:
Code:
strWhere = "tbl_street_name_SID=" [highlight]& SID[/highlight]

--

"If to err is human, then I must be some kind of human!" -Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top