Print the contents of an Access text box Control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'd like to know how I can send the value of the content (control Source?) of
a
form's text box control to the Printer (w/o using .Net functions).
Assume:
There is a Form in my database named: "frmTestForm"
A Text Box Control on this form named: "txtTextBox1"

I just want to send the txtTextBox1 content to my default printer. Is there
an easy way to do this?
 
Thanks so much. This is a simple solution that can be customized
in a bunch of different ways. I am going to share the code that I wrote
in response to Alex's suggestion.
 
Public Sub PrintTheControl(pControl As Control)
'this Sub Calls a Report (rptPrintControl)
'that simply prints the values Provided in a control on a Form
'I pass the Control into the routine and determine the form
'using CodeContextObject

Dim strFormName As String
Dim strControlName As String
Dim strOpenArgs As String

Const RPT As String = "rptPrintControl" 'name of Report that prints
a control

strFormName = CodeContextObject.Name 'tells me the calling Form's name

strControlName = pControl.Name 'name of control I want to
Print
strOpenArgs = strFormName & ";" & strControlName

'open the report and prints out the control
DoCmd.OpenReport RPT, _
WindowMode:=acWindowNormal, _
OpenArgs:=strOpenArgs

End Sub

In the Report's Open Event I have the following code.
The name of the control in the Report that holds the content
for printing is "txtBox2Print"
Set the Control's properties to "Can Grow = Yes"

I use 2 global variables to hold the Calling Form's Name (
gstrCallingFormName )
and the name of the control containing the text (gstrControlName2Print )
THese are not required but I use them elsewhere for another purpose

Private Sub Report_Open(Cancel As Integer)
'this is rptPrintControl's Open Report event

Dim astrOpenArgs() As String
Dim strControlSource As String

'get the calling form name and control name
astrOpenArgs = Split(Me.OpenArgs, scSEMI)

'split up the string into 2 array elements to easily assign for 2 items
this
'may be overkill but if there were more, it scales nicely

gstrCallingFormName = astrOpenArgs(0)
gstrControlName2Print = astrOpenArgs(1)

'Build string value of the ContolSource property for the the report's
'txtBox2Print control

strControlSource = "=FORMS!" & gstrCallingFormName & _
"!" & gstrControlName2Print

'the above evaluated to: =FORMS!frmTest!txtTestControlWithText
'and is modeled according to Alex's suggestion

'assign the ControlSource property which casues the text to be copied to
'the rptPrintControl's txtBox2Print text box control
Me!txtBox2Print.ControlSource = strControlSource



End Sub

Great stuff - thanks for a simple idea
 
Here is a simplified version of the solution Ken came up with.

I created a simple report with a single text box on it (with CanGrow and
CanShrink set to true). I then set the controlsource to:

=Forms!frmTestForm!txtTextBox1

In the OnClick event for my button I simply open and print the report using
DoCmd:

DoCmd.OpenReport "rptPrintTextBox1", acViewNormal

Which sends it to the default printer.

Hope the helps others!

Carl Lorentson
Renaissance Information Systems
Jericho, VT
 
Yes Indeed this solution is simple but it is hard coded. I wanted to have a
generalized solution to the problem so I could use it with other similar
controls.
This is a quick solution to a one off issue that works.

The solution I posted was more generalized so that you could put only ONE
copy of the report that printed the text box and then pass to it parameters
(I chose to use the OpenArgs) so that the report would then act on the
parameters. This way you anywhere in your application you need to print out a
text box you can do it without modifying the report. Presuming that you have
the routines marked as PUBLIC in one of your modules.

thanks for the suggestion.
 
This way you anywhere in your application you need to print out a
text box you can do it without modifying the report.

How about...


open "prn:" for output as #outfile

print #outfile, txtMyTextbox.Value

close #outfile



HTH


Tim F
 
Good point, Ken. Yours is definately a more general solution. Below is
coding I tried to do so that I didn't even need to have a report defined. It
creates a report, adds the text control, sets the control source, prints it,
then closes without saving.

It worked fine up to the point of closing the report. For some reason after
the call to PrintOut, Access can't find the report to issue the Close. This
leaves the newly created report open and the user needs to deal with it - not
acceptable of course. If anybody knows how to get around that, please let me
know.

Here is the code:

Dim rpt As Report
Dim ctrl As Control

' create the report
Set rpt = CreateReport

' create the control
Set ctrl = CreateReportControl(rpt.Name, acTextBox, acDetail)

' set its properties
ctrl.Height = 1428.6 'twips, 1"
ctrl.Width = 9285.7 'twips, 6.5"
ctrl.CanGrow = True
ctrl.CanShrink = True
ctrl.ControlSource = "= forms!" & Me.Name & "!txtTextToPrint"
rpt.UseDefaultPrinter = True

' print it
DoCmd.PrintOut

' close without saving - this generate and error that
' Access can't find the object or it is closed.
DoCmd.Close acReport, rpt.Name, acSaveNo
Set rpt = Nothing

Carl
 
Hi Tim,

I gave your approach a shot using this code:

Private Sub cmdPrintText_Click()
Dim outfile As Integer
Dim Printerpath As String

outfile = FreeFile
Printerpath = Application.Printer.DeviceName
Open Printerpath For Output As #outfile
Print #outfile, Me.txtTextToPrint.Value
Close #outfile
End Sub

But all I got was a "path not found". Now, I don't have a printer directly
connected to my system, it is off another computer on the network. This
might work just fine with a directly conncected printer.

Carl
 
But all I got was a "path not found". Now, I don't have a printer
directly connected to my system, it is off another computer on the
network. This might work just fine with a directly conncected
printer.

Okay, I give up. I am sure that in the dim and distant past I remember
printing directly to the Report object... something along the lines of

Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
Me.Print "Hello World"

End Sub


Presumably you could pass the name of the control in a OpenArgs or a
global variable or a custom Report Property.

All the best


Tim F
 
While I was researching this, I ran across the Print method (but it may be a
DoCmd method). It prints the string from wherever the insertion point is on
the report and can only be called from the Print event on the report. This
would probably work and eliminate the need to have the text bos.

Carl
 
Back
Top