Discussion on:
View:
Show:
Does this sound like a customization trick your Access users might appreciate? What other tweaks do you implement to make their work go more smoothly?
Solution is in using wherecondition argument on DoCmd.OpenReport. Assuming I want to use a button, click on EVENT and add the follwing VBA code:
Private Sub cmdPreviewRpt_Click()
Dim strReport As String
Dim strWhere As String
strReport = "My report name here"
strWhere = "[Name of my primary Key Field here] = """ & Me![Name of my Primary Key Field] & """"
DoCmd.OpenReport strReport, acViewPreview, , strWhere
End Sub
If primary key field is numeric you change strWhere sentence to:
strWhere = "[Name of KeyField] = " & Me![Name of KeyField]
Private Sub cmdPreviewRpt_Click()
Dim strReport As String
Dim strWhere As String
strReport = "My report name here"
strWhere = "[Name of my primary Key Field here] = """ & Me![Name of my Primary Key Field] & """"
DoCmd.OpenReport strReport, acViewPreview, , strWhere
End Sub
If primary key field is numeric you change strWhere sentence to:
strWhere = "[Name of KeyField] = " & Me![Name of KeyField]
The "Discussion" forum is for matters of general discussion, not specific problems in search of a solution.
Post problems such as this to the "Question" forum, rather than the "Discussion" forum. There are those who specifically seek out problems in need of a solution, and that's where they go to look for such.
Furthermore, the "Questions" forum provides for your rating Helpful responses as such, so that others, with the same or a similar problem, might be able to find aid without having to necessarily begin a new Question.
Post problems such as this to the "Question" forum, rather than the "Discussion" forum. There are those who specifically seek out problems in need of a solution, and that's where they go to look for such.
Furthermore, the "Questions" forum provides for your rating Helpful responses as such, so that others, with the same or a similar problem, might be able to find aid without having to necessarily begin a new Question.
I teach Acess 2002 classes and I am always looking for new ways to automate Access.
May be this is what you required to print the current record you are viewing.
Private Sub PRINT_Click()
On Error GoTo Err_PRINT_Click
Dim MyForm As Form
Dim record_num As Long
stDocName = "YOUR_FORM_NAME"
Set MyForm = Screen.ActiveForm
DoCmd.SelectObject acForm, stDocName, True
record_num = MyForm.CurrentRecord
DoCmd.PrintOut acPages, record_num, record_num
Exit_PRINT_Click:
Exit Sub
Err_PRINT_Click:
MsgBox Err.Description
Resume Exit_PRINT_Click
End Sub
Private Sub PRINT_Click()
On Error GoTo Err_PRINT_Click
Dim MyForm As Form
Dim record_num As Long
stDocName = "YOUR_FORM_NAME"
Set MyForm = Screen.ActiveForm
DoCmd.SelectObject acForm, stDocName, True
record_num = MyForm.CurrentRecord
DoCmd.PrintOut acPages, record_num, record_num
Exit_PRINT_Click:
Exit Sub
Err_PRINT_Click:
MsgBox Err.Description
Resume Exit_PRINT_Click
End Sub
everybody's tip for printing current form works except when some forms have excessive detail lines in a sub form and the print out exceeds 1 page. then all forms after that are off by that page. It appears that all the various solutions rely on all pages of a form not exceeding one page
no oscar for everybody. I need a better solution for this issue M/S should have built it into a option in button set up properly.
great effort. i wish i knew VB i would help more.
no oscar for everybody. I need a better solution for this issue M/S should have built it into a option in button set up properly.
great effort. i wish i knew VB i would help more.
Thank you for this good article.
I struggled with this due to a complex form that needed a lot of updates, so the following works for me. You need to have a unique ID showing on the form.
-----
Private Sub cmdPrint_Click()
On Error GoTo cmdPrint_Click_Err
DoCmd.GoToControl "ID_mytable"
DoCmd.RunCommand acCmdFilterBySelection
DoCmd.PrintOut acSelection, 1, 1, acHigh, 1, True
cmdPrint_Click_Exit:
Exit Sub
cmdPrint_Click_Err:
MsgBox Error$
Resume cmdPrint_Click_Exit
End Sub
----
Note:
- for "ID_mytable" substitute the name of your control bound to your ID.
- this is for the click event of a button named "cmdPrint"
- using Access 2003 - probably works with earlier versions
Before running the procedure, carry out the actions manually excepting to use the page setup and print dialog so as to set the defaults for printing.
Extras to the code could include turning off the filter before exiting, storing current focus and restoring before exit.
See any 'gotchas' with this?
Mark
I struggled with this due to a complex form that needed a lot of updates, so the following works for me. You need to have a unique ID showing on the form.
-----
Private Sub cmdPrint_Click()
On Error GoTo cmdPrint_Click_Err
DoCmd.GoToControl "ID_mytable"
DoCmd.RunCommand acCmdFilterBySelection
DoCmd.PrintOut acSelection, 1, 1, acHigh, 1, True
cmdPrint_Click_Exit:
Exit Sub
cmdPrint_Click_Err:
MsgBox Error$
Resume cmdPrint_Click_Exit
End Sub
----
Note:
- for "ID_mytable" substitute the name of your control bound to your ID.
- this is for the click event of a button named "cmdPrint"
- using Access 2003 - probably works with earlier versions
Before running the procedure, carry out the actions manually excepting to use the page setup and print dialog so as to set the defaults for printing.
Extras to the code could include turning off the filter before exiting, storing current focus and restoring before exit.
See any 'gotchas' with this?
Mark
This is great. Question: Is there away to email a single report page as well?
This solution works fine for me:
Private Sub command35_Click()
'Print current record
'using Invoice.
If IsNull(Me!JobNumber) Then
MsgBox "Please select a valid record", _
vbOKOnly, "Error"
Exit Sub
End If
DoCmd.OpenReport "Invoice", , , _
"JobNumber = " & Me!JobNumber
End Sub
However if I change the form type to Data Entry I get a blank report printed (no data on it). If I change it back to not Data Entry it is fine. I want to print a record from a data entry sheet when the record is added. Can you help?
Private Sub command35_Click()
'Print current record
'using Invoice.
If IsNull(Me!JobNumber) Then
MsgBox "Please select a valid record", _
vbOKOnly, "Error"
Exit Sub
End If
DoCmd.OpenReport "Invoice", , , _
"JobNumber = " & Me!JobNumber
End Sub
However if I change the form type to Data Entry I get a blank report printed (no data on it). If I change it back to not Data Entry it is fine. I want to print a record from a data entry sheet when the record is added. Can you help?
Fine so far, but I also need to e-mail that same current record in the form of a report or form. Can you help with that please.
The article is comprehensive ,yet simple and easy to understand.
I use Access 2003, I've tried to follow your code, but I changed the unique field's name and it said error, especially on this part:
DoCmd.OpenReport "rptEmployees", , , _
"EmployeeID = " & Me!EmployeeID
I changed rptEmployees with my report name INV and EmployeeID with XITE_ID
Can you tell me what's wrong?
DoCmd.OpenReport "rptEmployees", , , _
"EmployeeID = " & Me!EmployeeID
I changed rptEmployees with my report name INV and EmployeeID with XITE_ID
Can you tell me what's wrong?
Hi,
This informtion is very usefull, and i want some more information about print.
My requrement is User can't use Print Button in Form, they would like to use File->Print Options.
If they use File->Print Option there will print all User forms details and there controls also like Buttons, TextBox etc.,
So i want Use MS Acess wone print option but don't print controls.
Please help me...it's very urgent requriment...
This informtion is very usefull, and i want some more information about print.
My requrement is User can't use Print Button in Form, they would like to use File->Print Options.
If they use File->Print Option there will print all User forms details and there controls also like Buttons, TextBox etc.,
So i want Use MS Acess wone print option but don't print controls.
Please help me...it's very urgent requriment...
Okay so I tried using this code but was unsucessful. I need to be able to serach for a record (which I can do) and just print that single record into a form without printing the whole database. Please any help would be greatly appreciated.
Private Sub cmdPrint_Click()
'Print current record
'using rpt1234.
If IsNull(Me!FNLastName) Then
MsgBox "Please select a valid record", _
vbOKOnly, "Error"
Exit Sub
End If
DoCmd.OpenReport "rpt1234", , , _
"FNLastName = " & Me!FNLastName
End Sub
------------------------------------------------------
FNLastName is how the form is selected via a search, however there may be a the same Last Name so each record has a Seq # that is autonumbered and not duplicated.
rpt1234 is which report format I want to use.
Private Sub cmdPrint_Click()
'Print current record
'using rpt1234.
If IsNull(Me!FNLastName) Then
MsgBox "Please select a valid record", _
vbOKOnly, "Error"
Exit Sub
End If
DoCmd.OpenReport "rpt1234", , , _
"FNLastName = " & Me!FNLastName
End Sub
------------------------------------------------------
FNLastName is how the form is selected via a search, however there may be a the same Last Name so each record has a Seq # that is autonumbered and not duplicated.
rpt1234 is which report format I want to use.
i used this code but i have 2 subforms and its only printing one form and with none of the data input please help going nuts
Private Sub PRINT_Click()
On Error GoTo Err_PRINT_Click
Dim MyForm As Form
Dim record_num As Long
stDocName = "Invoices"
Set MyForm = Screen.ActiveForm
DoCmd.SelectObject acForm, stDocName, True
record_num = MyForm.CurrentRecord
DoCmd.PrintOut acPages, record_num, record_num
Exit_PRINT_Click:
Exit Sub
Err_PRINT_Click:
MsgBox Err.Description
Resume Exit_PRINT_Click
Private Sub PRINT_Click()
On Error GoTo Err_PRINT_Click
Dim MyForm As Form
Dim record_num As Long
stDocName = "Invoices"
Set MyForm = Screen.ActiveForm
DoCmd.SelectObject acForm, stDocName, True
record_num = MyForm.CurrentRecord
DoCmd.PrintOut acPages, record_num, record_num
Exit_PRINT_Click:
Exit Sub
Err_PRINT_Click:
MsgBox Err.Description
Resume Exit_PRINT_Click
A lot of these print unlinked forms, but as soon as I link them, Print Preview goes blank (Headers only). Does anyone know why?
I have now found that linked forms will print if you change something on the form.
So I'm currently trying to make an invisible change on a form on load, and empty it on close.
So I'm currently trying to make an invisible change on a form on load, and empty it on close.
'* Code for button click event to print the current record showing in a form.
'* It prints the form graphically, i.e. WYSIWYG
'* All you have to do is select the printer.
'* Have not tried with Subforms.
Private Sub cmdPrintForm_Click()
On Local Error Resume Next '* This can be improved - it's just to avoid "print cancelled" messages
DoCmd.RunCommand acCmdPrintSelection '* Yep,this prints the current form - you don't have to select it(in Access 2007, anyway).
End Sub
'* It prints the form graphically, i.e. WYSIWYG
'* All you have to do is select the printer.
'* Have not tried with Subforms.
Private Sub cmdPrintForm_Click()
On Local Error Resume Next '* This can be improved - it's just to avoid "print cancelled" messages
DoCmd.RunCommand acCmdPrintSelection '* Yep,this prints the current form - you don't have to select it(in Access 2007, anyway).
End Sub
- Keyboard Shortcuts:
- Prev
- Next
- Toggle

































