Print a Form from a Form with the current record of the source form

TonyVelasco

New member
Local time
Today, 08:23
Joined
Jan 9, 2004
Messages
8
Used to be a programmer many years ago, from 1988-1992. Moved on to I.T., mainly network infrastructure and server and desktop design, engineering, and administration. Security, network backbone, server management, and application help desk are my current strengths.

Now I also am being tasked with programming again, but I am more than a little rusty!!! I have forgotten most of what I once knew.

I am using Access 97 from a programming standpoint for the first time. I have a form for data entry, editing and display. It needs to print a license with some of the form data on it.

For various reasons I chose to use a form for the license rather than a report (limitations in Access 97's report capabilities to handle custom page sizes and watermarks, namely).

As such, I have a button on the main form that print's the license form. The code for that button is below. The problem is that the form I am calling defaults to the first record of the table, rathr than the current record of the main form. I need to have it print the record that is on the main form, and only that record.

Any suggestions?

Private Sub PrintLicense_Click()
On Error GoTo Err_PrintLicense_Click

Dim stDocName As String
Dim MyForm As Form

stDocName = "LicenseForm"
Set MyForm = Screen.ActiveForm
DoCmd.SelectObject acForm, stDocName, True
DoCmd.PrintOut acSelection
DoCmd.SelectObject acForm, MyForm.Name, False

Exit_PrintLicense_Click:
Exit Sub

Err_PrintLicense_Click:
MsgBox Err.Description
Resume Exit_PrintLicense_Click

End Sub
 
Last edited:
I wouldn't have thought that use a form instead of a report would be any where ideal for printing purposes.

Have you thought of upgrading to 2000 or above?

However to select a record try something similair to this:

Code:
Dim StrLinkCriteria As String

Strlinkcriteria = forms!yourformname!yourPKField

docmd.selectobject acform, stdocname,strlinkcriteria, true

Play around with the code, but personally wouldn't advice using forms to print, but I haven't seen what you are trying to achieve.

Good Luck

Andy
 
Assuming that I have read this right, you are wanting to print a copy of the “Form” that you are looking at on screen.

Again, assuming that this is what you want, you can create a button on screen with the button wizard. Select Record Operations -> Print Record -> etc. The set the buttons visible property to On Screen Only.

But if you have a button here is the code…

Code:
Private Sub Command17_Click()
On Error GoTo Err_Command17_Click


    DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
    DoCmd.PrintOut acSelection

Exit_Command17_Click:
    Exit Sub

Err_Command17_Click:
    MsgBox Err.Description
    Resume Exit_Command17_Click
    
End Sub

Just change the Command17_Click() Button code to what ever yours is.

And all this will let you print the record and the form that you are looking at on screen.
Hope this is some help. :)
 
Lister,

You should avoid using the DoCmd.DoMenuItem commands. They are not convertible beyond Access 97. No it does not make sense that the Access 97 wizards still create the obsolete DoCmd.DoMenuItem commands. This sub does the same thing yet uses the DoCmd.RunCommand...
Code:
Private Sub bPrint_Click()
On Error GoTo Err_bPrint_Click
    
    DoCmd.RunCommand acCmdSelectRecord
    DoCmd.PrintOut acSelection

Exit_bPrint_Click:
    Exit Sub
    
Err_bPrint_Click:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_bPrint_Click
    
End Sub
HTH
 
Thank you for the ideas.

The second form, that is being printed, is a license that does not have all of the same information on it, and is not formatted like, the source form.

It also has a background watermark, and is a half page license, so 8.5 inches wide by 5.5 long, a custom size, rather than 8.5 by 11.

I started with a report, but couldn't get it to handle the custom size of the form properly and place a background image reliably. It would always think the form was letter size, and try to "center" a background graphic in very weird and unpredictable ways. It would also print the graphic twice, once on the top 5.5 inch form with the license, and once on an otherwise blank second form.

So, I found that a form sized to 8.5 wide by 5.5 in length worked perfectly, and reliably with the half page licenses.

So, I do need to print the second form, rather than the first. So the trick is transfering the record to the second form so it is the active record for that form before it prints.

Since I want the form to immediately print, rather than come up in some preview window or pop up on screen, is there a way to get the form to default to the record of the source form, and then use the lines:

DoCmd.RunCommand acCmdSelectRecord
DoCmd.PrintOut acSelection


Or will I be forced to use something like a OpenForm command, and if so is there a way to get that to not display on screen, but just immediately print?
 
Spacepro, there is no option to do the following:

Dim StrLinkCriteria As String

Strlinkcriteria = forms!yourformname!yourPKField

docmd.selectobject acform, stdocname,strlinkcriteria, true


as the docmd.selectobject method does not allow an argument such as strlinkcriteria to specify a record. It only has these valid arguments: objecttype, objectname, indatabasewindow

so that won't work.

I am looking at an option like this instead, since the SelectObject method seems to limited for what I am trying to accomplish:

code that will open the form with the correct record from the source form, but hide the form so it doesn't display, print the form out, then close the form. That would accomplish the same effect.

I think a DoCmd.OpenForm is the way to go, but am having a hard time with the syntax. I then can probably use the command I already have in the code snippet to print the form, and then I just need a command that closes the form.

Please help with some syntax or a code sample I can try, thank you!
 
Tony,

You can launch the second form by:

Code:
docmd.OpenForm "YourForm",,,"[YourKey] = " & Me.YourKey,,acHidden

Then use the form's OnOpen event to print it using the
previous code. Then do a DoCmd.Close

I don't print forms, so I don't even know if you can print
a hidden one.

Wayne
 
Forms are not designed to be printed, Reports are. There is no reason why you cannot use one and avoid the reams of code you're trying to write
 
Thanks to Wayne, message for Rich

Rich,

I agree, a report was what I first designed, and used. I only switched to printing a form when the reports handling of the background graphics and custom page size screwed up constantly. It may just be crappy handling by Access97, and we are going to upgrade soon, but until then I have to have this working this week, so I am going to use a form.

I would prefer to use a report, but since Access 97 is doing such a lousy job of interpreting the custom size of the report and can't seem to position the background graphic properly, I have no choice but to use the form. It sucks, but so be it.

Wayne, I was digging through the Access help and had it figured out, but thanks for the sample, as it is what I had coded. I have found out that it won't print out a hidden form, so I am trying to work around that. Since it won't print a hidden form, I cleared the On Open even procedure from the license form, and just went back to having the print and close in the event procedure for the button click.

Thanks for your help! I am going to see if I can open the license minimized or off screen, but still print it. Any ideas on that?
 
Where did you set the Custom Page size, does your printer support them. I've never had a problem with 97 Reports changing paper sizes, only when Windows decides I no longer have a printer installed, kinda strange because I have three printers installed:mad:
 
Well, I was surprised Access 97 has such limited control of form size within the application. I had to rely on using my HP print driver to setup a half of a 8.5 X 11 page (8.5 x 5.5). I called it HalfPage.

Then I matched my report to that size, and everything was fine, except that the background graphic would not center on the page. GRR. I messed with this for hours, and hours, and hours, and it would always miscalculate the center of the page. I even edited the graphic extensively with a transparent layer, making it bigger on the top and one side to try and "bump" it to the center, but then Access97 would just move it in some other weird way.

Finally, I just copied and pasted the report to a form, and setup the form for 8.5" wide by 5.5" long. I setup the background graphic, set the page setup for the form to use my specific printer driver with the custom page size set as default, then test printed. It worked, but I needed to adjust it just a bit to account for margins, the unprintable area at the edge of each page, which was bumping the form down a bit more with each page printed in succession. Easy enough to pull the bottom edge up a bit of the form, then no problem after that.

It was easy to have a button sending the current record on a form to a report, but not as obvious how to do that with a form. I first setup a button on the data entry and editing form using the wizard that opened the license form. The wizard made this easy, but the form would of course default to the first record in the table or query it was setup for. It also would print all records, rather than just the record I wanted, or even just the first one, when setup with the wizard.

I found in the help files how to narrow it to just the current record, but found no way to set the current record to what I needed using the SelectObject method that the wizard created.

So in the end, I switched to a OpenForm method that opens the form and passes a variable with the unique key of the record that was in the current form. The form wouldn't print if hidden, so instead I minimize it right after opening it, and then automatically print it and close it, and boom, your right back to the main data entry/editing form. Works quickly, and reliably.

I will post the code later for others to see.

I will have some questions about queries and joins next week, so be prepared for more goofy questions from me! :rolleyes:

Man, I am SOOOO rusty with both databases and programming, it's gonna take a bit to get back into it. Everything was very structured and linear back when I did a lot of programming, not object oriented with all kinds of mixed script languages, code objects, and rather non-linear programming techniques.

I wrote alot of code in Modula, Modula II, Fortran, Pascal, Turbo Pascal, Assembly, and Cobol on VAX/VMS systems, Digital Ultrix systems, and some early pc's, and also did a little C programming, but got out of programming about the time Windows 3.1 started making inroads, and all the new object oriented languages and versions of languages really bloomed, and before there were much in the way of scripting languages.

So I am a bit befuddled in Access with things like precedence, like if I attach a code snippet to a OnClick event, and another to an on open event in a form, how is precedence managed, and how and when does control pass from one event to the next?

I am sure I will get comfortable with it soon enough, as I already feel a little more comfortable after my few days of digging around through the help files, but I need to get a good database concepts refresher, and probably a book or tutorial on some programming fundamentals. This should be fun!
 
Tony,

Well, you did fine on that! It looks like you'll be a good asset to
the community. Hope to see you again soon.

Wayne - (VAX Veteran)
 
Here is the code I settled on

In case anyone else every wants to do this, here is the code I settled on to open one form from another using a button, then printing the form using the same record as the original source form.

Private Sub PrintLicense_Click()
On Error GoTo Err_PrintLicense_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "LicenseForm"

stLinkCriteria = "[BusinessID]=" & Me![BusinessID]
DoCmd.openform stDocName, acNormal, , stLinkCriteria
DoCmd.Minimize
DoCmd.PrintOut acSelection
DoCmd.Close acForm, stDocName, acSaveNo

Exit_PrintLicense_Click:
Exit Sub

Err_PrintLicense_Click:
MsgBox Err.Description
Resume Exit_PrintLicense_Click

End Sub
 

Users who are viewing this thread

Back
Top Bottom