how can you print one record from a form

  • Thread starter Thread starter Kathy
  • Start date Start date
K

Kathy

My form (a work order)contains a button that allows the
user to print the work order in a report format. However,
when the user clicks and goes to print preview mode, the
report brings up every record in page sequence. User has
to search through each record to find the correct Work
Order number and can then print by selecting to print only
that page. Does anyone know how to format the form/print
button/report/database so that clicking on the button only
send the active record to print preview?

Thanks!
 
This example assumes you have a numeric primary key named "OrderID" on your
form, and you want to open a report named "MyReport":

Private Sub cmdPrint_Click()
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
strWhere = "[OrderID] = " & Me.[OrderID]
DoCmd.OpenReport "MyReport", acViewPreview, , strWhere
End If
End Sub
 
Thank very much for the quick reply. I am assuming this
code is to be applied as an Expression in the On Click
Control within the properties of the button. My current
expression is as follows:

Option Compare Database

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click

Dim stDocName As String

stDocName = "Work Order Printout"
DoCmd.OpenReport stDocName, acPreview

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click

End Sub

Can you reformat this code for me as per your example as I
tried but it didn't work.

Thanks, much appreciated as I have basically self taught
myself the rest and I am finding this part frustrating and
time consuming.

Cheers!
-----Original Message-----
This example assumes you have a numeric primary key named "OrderID" on your
form, and you want to open a report named "MyReport":

Private Sub cmdPrint_Click()
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
strWhere = "[OrderID] = " & Me.[OrderID]
DoCmd.OpenReport "MyReport", acViewPreview, , strWhere
End If
End Sub


--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Kathy said:
My form (a work order)contains a button that allows the
user to print the work order in a report format. However,
when the user clicks and goes to print preview mode, the
report brings up every record in page sequence. User has
to search through each record to find the correct Work
Order number and can then print by selecting to print only
that page. Does anyone know how to format the form/print
button/report/database so that clicking on the button only
send the active record to print preview?

Thanks!


.
 
Option Compare Database
Option Explicit

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click
Dim stDocName As String
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
stDocName = "Work Order Printout"
strWhere = "[YourIdField] = " & Me.[YourIdField]
DoCmd.OpenReport stDocName, acViewPreview, , strWhere
End If

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click
End Sub


Note that if YourIdField is of type Text, you will need extra quotes:
strWhere = "[YourIdField] = """ & Me.[YourIdField] & """"


--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.
Kathy said:
Thank very much for the quick reply. I am assuming this
code is to be applied as an Expression in the On Click
Control within the properties of the button. My current
expression is as follows:

Option Compare Database

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click

Dim stDocName As String

stDocName = "Work Order Printout"
DoCmd.OpenReport stDocName, acPreview

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click

End Sub

Can you reformat this code for me as per your example as I
tried but it didn't work.

Thanks, much appreciated as I have basically self taught
myself the rest and I am finding this part frustrating and
time consuming.

Cheers!
-----Original Message-----
This example assumes you have a numeric primary key named "OrderID" on your
form, and you want to open a report named "MyReport":

Private Sub cmdPrint_Click()
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
strWhere = "[OrderID] = " & Me.[OrderID]
DoCmd.OpenReport "MyReport", acViewPreview, , strWhere
End If
End Sub


--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Kathy said:
My form (a work order)contains a button that allows the
user to print the work order in a report format. However,
when the user clicks and goes to print preview mode, the
report brings up every record in page sequence. User has
to search through each record to find the correct Work
Order number and can then print by selecting to print only
that page. Does anyone know how to format the form/print
button/report/database so that clicking on the button only
send the active record to print preview?

Thanks!


.
 
Thanks again, tried your suggestion and now get the
following error:

Microsoft Access can't find the field '│' referred to in
your expression.

-----Original Message-----
Option Compare Database
Option Explicit

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click
Dim stDocName As String
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
stDocName = "Work Order Printout"
strWhere = "[YourIdField] = " & Me.[YourIdField]
DoCmd.OpenReport stDocName, acViewPreview, , strWhere
End If

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click
End Sub


Note that if YourIdField is of type Text, you will need extra quotes:
strWhere = "[YourIdField] = """ & Me.[YourIdField] & """"


--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.
Kathy said:
Thank very much for the quick reply. I am assuming this
code is to be applied as an Expression in the On Click
Control within the properties of the button. My current
expression is as follows:

Option Compare Database

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click

Dim stDocName As String

stDocName = "Work Order Printout"
DoCmd.OpenReport stDocName, acPreview

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click

End Sub

Can you reformat this code for me as per your example as I
tried but it didn't work.

Thanks, much appreciated as I have basically self taught
myself the rest and I am finding this part frustrating and
time consuming.

Cheers!
-----Original Message-----
This example assumes you have a numeric primary key named "OrderID" on your
form, and you want to open a report named "MyReport":

Private Sub cmdPrint_Click()
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
strWhere = "[OrderID] = " & Me.[OrderID]
DoCmd.OpenReport "MyReport", acViewPreview, , strWhere
End If
End Sub


--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

My form (a work order)contains a button that allows the
user to print the work order in a report format. However,
when the user clicks and goes to print preview mode, the
report brings up every record in page sequence. User has
to search through each record to find the correct Work
Order number and can then print by selecting to print only
that page. Does anyone know how to format the form/print
button/report/database so that clicking on the button only
send the active record to print preview?

Thanks!


.


.
 
What is the name of the primary key field of the table the form gets its
records from? Use that name in place of "YourIdField".

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Kathy said:
Thanks again, tried your suggestion and now get the
following error:

Microsoft Access can't find the field '│' referred to in
your expression.

-----Original Message-----
Option Compare Database
Option Explicit

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click
Dim stDocName As String
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
stDocName = "Work Order Printout"
strWhere = "[YourIdField] = " & Me.[YourIdField]
DoCmd.OpenReport stDocName, acViewPreview, , strWhere
End If

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click
End Sub


Note that if YourIdField is of type Text, you will need extra quotes:
strWhere = "[YourIdField] = """ & Me.[YourIdField] & """"


--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.
Kathy said:
Thank very much for the quick reply. I am assuming this
code is to be applied as an Expression in the On Click
Control within the properties of the button. My current
expression is as follows:

Option Compare Database

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click

Dim stDocName As String

stDocName = "Work Order Printout"
DoCmd.OpenReport stDocName, acPreview

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click

End Sub

Can you reformat this code for me as per your example as I
tried but it didn't work.

Thanks, much appreciated as I have basically self taught
myself the rest and I am finding this part frustrating and
time consuming.

Cheers!
-----Original Message-----
This example assumes you have a numeric primary key
named "OrderID" on your
form, and you want to open a report named "MyReport":

Private Sub cmdPrint_Click()
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
strWhere = "[OrderID] = " & Me.[OrderID]
DoCmd.OpenReport "MyReport", acViewPreview, ,
strWhere
End If
End Sub


--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

My form (a work order)contains a button that allows the
user to print the work order in a report format.
However,
when the user clicks and goes to print preview mode, the
report brings up every record in page sequence. User
has
to search through each record to find the correct Work
Order number and can then print by selecting to print
only
that page. Does anyone know how to format the
form/print
button/report/database so that clicking on the button
only
send the active record to print preview?

Thanks!


.


.
 
Thank you so much Allen. Success! Now my users are
extremely happy.

Cheers!
Kathy
-----Original Message-----
What is the name of the primary key field of the table the form gets its
records from? Use that name in place of "YourIdField".

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Kathy said:
Thanks again, tried your suggestion and now get the
following error:

Microsoft Access can't find the field '│' referred to in
your expression.

-----Original Message-----
Option Compare Database
Option Explicit

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click
Dim stDocName As String
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
stDocName = "Work Order Printout"
strWhere = "[YourIdField] = " & Me. [YourIdField]
DoCmd.OpenReport stDocName, acViewPreview, , strWhere
End If

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click
End Sub


Note that if YourIdField is of type Text, you will need extra quotes:
strWhere = "[YourIdField] = """ & Me.[YourIdField] & """"


--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
Thank very much for the quick reply. I am assuming this
code is to be applied as an Expression in the On Click
Control within the properties of the button. My current
expression is as follows:

Option Compare Database

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click

Dim stDocName As String

stDocName = "Work Order Printout"
DoCmd.OpenReport stDocName, acPreview

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click

End Sub

Can you reformat this code for me as per your example as I
tried but it didn't work.

Thanks, much appreciated as I have basically self taught
myself the rest and I am finding this part
frustrating
and
time consuming.

Cheers!
-----Original Message-----
This example assumes you have a numeric primary key
named "OrderID" on your
form, and you want to open a report named "MyReport":

Private Sub cmdPrint_Click()
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
strWhere = "[OrderID] = " & Me.[OrderID]
DoCmd.OpenReport "MyReport", acViewPreview, ,
strWhere
End If
End Sub


--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

My form (a work order)contains a button that
allows
the
user to print the work order in a report format.
However,
when the user clicks and goes to print preview
mode,
the
report brings up every record in page sequence. User
has
to search through each record to find the correct Work
Order number and can then print by selecting to print
only
that page. Does anyone know how to format the
form/print
button/report/database so that clicking on the button
only
send the active record to print preview?

Thanks!


.



.


.
 
Sorry Allen, still one problem. When I copied and pasted
your code, I got an error on the Do.cmd line. I fixed it
by eliminating a space. We have success clicking on the
form button and seeing the active document in print
preview view. However, when we send the document to the
printer, we get the following message:

"The command or action "OpenReport" isn't available now."
*You may be in a read only...
*The type of object the action applies to isn't currently
selected or isn't active for view
Use only the commands or macro actions that are currently
available for this database.

Note:
There is no one else using the system so we are not in
read only mode. I do have a Macro for Opening the report
in Print Preview mode. In the control properties of the
button, I have selected "Expression". If I delete the
Macro, the Work Order button function does not work at all.

K

-----Original Message-----
What is the name of the primary key field of the table the form gets its
records from? Use that name in place of "YourIdField".

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Kathy said:
Thanks again, tried your suggestion and now get the
following error:

Microsoft Access can't find the field '│' referred to in
your expression.

-----Original Message-----
Option Compare Database
Option Explicit

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click
Dim stDocName As String
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
stDocName = "Work Order Printout"
strWhere = "[YourIdField] = " & Me. [YourIdField]
DoCmd.OpenReport stDocName, acViewPreview, , strWhere
End If

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click
End Sub


Note that if YourIdField is of type Text, you will need extra quotes:
strWhere = "[YourIdField] = """ & Me.[YourIdField] & """"


--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
Thank very much for the quick reply. I am assuming this
code is to be applied as an Expression in the On Click
Control within the properties of the button. My current
expression is as follows:

Option Compare Database

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click

Dim stDocName As String

stDocName = "Work Order Printout"
DoCmd.OpenReport stDocName, acPreview

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click

End Sub

Can you reformat this code for me as per your example as I
tried but it didn't work.

Thanks, much appreciated as I have basically self taught
myself the rest and I am finding this part
frustrating
and
time consuming.

Cheers!
-----Original Message-----
This example assumes you have a numeric primary key
named "OrderID" on your
form, and you want to open a report named "MyReport":

Private Sub cmdPrint_Click()
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
strWhere = "[OrderID] = " & Me.[OrderID]
DoCmd.OpenReport "MyReport", acViewPreview, ,
strWhere
End If
End Sub


--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

My form (a work order)contains a button that
allows
the
user to print the work order in a report format.
However,
when the user clicks and goes to print preview
mode,
the
report brings up every record in page sequence. User
has
to search through each record to find the correct Work
Order number and can then print by selecting to print
only
that page. Does anyone know how to format the
form/print
button/report/database so that clicking on the button
only
send the active record to print preview?

Thanks!


.



.


.
 
What space did you remove?
How does the line read now - the one that gives the problem.

The only difference you need to get it to print instead of preview, is to
replace
acViewPreview
with
acViewNormal

Is the preview already open when you attempt to print it?

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Kathy said:
Sorry Allen, still one problem. When I copied and pasted
your code, I got an error on the Do.cmd line. I fixed it
by eliminating a space. We have success clicking on the
form button and seeing the active document in print
preview view. However, when we send the document to the
printer, we get the following message:

"The command or action "OpenReport" isn't available now."
*You may be in a read only...
*The type of object the action applies to isn't currently
selected or isn't active for view
Use only the commands or macro actions that are currently
available for this database.

Note:
There is no one else using the system so we are not in
read only mode. I do have a Macro for Opening the report
in Print Preview mode. In the control properties of the
button, I have selected "Expression". If I delete the
Macro, the Work Order button function does not work at all.

K

-----Original Message-----
What is the name of the primary key field of the table the form gets its
records from? Use that name in place of "YourIdField".

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Kathy said:
Thanks again, tried your suggestion and now get the
following error:

Microsoft Access can't find the field '│' referred to in
your expression.


-----Original Message-----
Option Compare Database
Option Explicit

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click
Dim stDocName As String
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
stDocName = "Work Order Printout"
strWhere = "[YourIdField] = " & Me. [YourIdField]
DoCmd.OpenReport stDocName, acViewPreview, ,
strWhere
End If

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click
End Sub


Note that if YourIdField is of type Text, you will need
extra quotes:
strWhere = "[YourIdField] = """ & Me.[YourIdField]
& """"


--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
Thank very much for the quick reply. I am assuming this
code is to be applied as an Expression in the On Click
Control within the properties of the button. My current
expression is as follows:

Option Compare Database

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click

Dim stDocName As String

stDocName = "Work Order Printout"
DoCmd.OpenReport stDocName, acPreview

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click

End Sub

Can you reformat this code for me as per your example
as I
tried but it didn't work.

Thanks, much appreciated as I have basically self taught
myself the rest and I am finding this part frustrating
and
time consuming.

Cheers!
-----Original Message-----
This example assumes you have a numeric primary key
named "OrderID" on your
form, and you want to open a report named "MyReport":

Private Sub cmdPrint_Click()
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
strWhere = "[OrderID] = " & Me.[OrderID]
DoCmd.OpenReport "MyReport", acViewPreview, ,
strWhere
End If
End Sub


--
Allen Browne - Microsoft MVP. Perth, Western
Australia.
Tips for Access users -
http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot
org.

My form (a work order)contains a button that allows
the
user to print the work order in a report format.
However,
when the user clicks and goes to print preview mode,
the
report brings up every record in page sequence. User
has
to search through each record to find the correct
Work
Order number and can then print by selecting to print
only
that page. Does anyone know how to format the
form/print
button/report/database so that clicking on the button
only
send the active record to print preview?

Thanks!


.



.


.
 
I had to remove a space in the Do.cmd line before strWhere
Yes, the document is in Print Preview mode when we go to
print.

Existing Code:

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click
Dim stDocName As String
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
stDocName = "Work Order Printout"
strWhere = "[ID] = " & Me.[ID]
DoCmd.OpenReport stDocName, acViewPreview, ,
strWhere
End If

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click
End Sub



Private Sub Work_Order___BeforeUpdate(Cancel As Integer)

End Sub
-----Original Message-----
What space did you remove?
How does the line read now - the one that gives the problem.

The only difference you need to get it to print instead of preview, is to
replace
acViewPreview
with
acViewNormal

Is the preview already open when you attempt to print it?

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Sorry Allen, still one problem. When I copied and pasted
your code, I got an error on the Do.cmd line. I fixed it
by eliminating a space. We have success clicking on the
form button and seeing the active document in print
preview view. However, when we send the document to the
printer, we get the following message:

"The command or action "OpenReport" isn't available now."
*You may be in a read only...
*The type of object the action applies to isn't currently
selected or isn't active for view
Use only the commands or macro actions that are currently
available for this database.

Note:
There is no one else using the system so we are not in
read only mode. I do have a Macro for Opening the report
in Print Preview mode. In the control properties of the
button, I have selected "Expression". If I delete the
Macro, the Work Order button function does not work at all.

K

-----Original Message-----
What is the name of the primary key field of the table the form gets its
records from? Use that name in place of "YourIdField".

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

Thanks again, tried your suggestion and now get the
following error:

Microsoft Access can't find the field '│' referred
to
in
your expression.


-----Original Message-----
Option Compare Database
Option Explicit

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click
Dim stDocName As String
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
stDocName = "Work Order Printout"
strWhere = "[YourIdField] = " & Me. [YourIdField]
DoCmd.OpenReport stDocName, acViewPreview, ,
strWhere
End If

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click
End Sub


Note that if YourIdField is of type Text, you will need
extra quotes:
strWhere = "[YourIdField] = """ & Me. [YourIdField]
& """"


--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
Thank very much for the quick reply. I am
assuming
this
code is to be applied as an Expression in the On Click
Control within the properties of the button. My current
expression is as follows:

Option Compare Database

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click

Dim stDocName As String

stDocName = "Work Order Printout"
DoCmd.OpenReport stDocName, acPreview

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click

End Sub

Can you reformat this code for me as per your example
as I
tried but it didn't work.

Thanks, much appreciated as I have basically self taught
myself the rest and I am finding this part frustrating
and
time consuming.

Cheers!
-----Original Message-----
This example assumes you have a numeric primary key
named "OrderID" on your
form, and you want to open a report named "MyReport":

Private Sub cmdPrint_Click()
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
strWhere = "[OrderID] = " & Me.[OrderID]
DoCmd.OpenReport "MyReport", acViewPreview, ,
strWhere
End If
End Sub


--
Allen Browne - Microsoft MVP. Perth, Western
Australia.
Tips for Access users -
http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot
org.

My form (a work order)contains a button that allows
the
user to print the work order in a report format.
However,
when the user clicks and goes to print preview mode,
the
report brings up every record in page sequence. User
has
to search through each record to find the correct
Work
Order number and can then print by selecting to print
only
that page. Does anyone know how to format the
form/print
button/report/database so that clicking on the button
only
send the active record to print preview?

Thanks!


.



.



.


.
 
The code looks okay assuming your primary key field really is called "ID",
and it is a number (not a Text field).

If you already have the report open in preview mode, you should be able to
use the toolbar icon to print it instead of running this code again.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Kathy said:
I had to remove a space in the Do.cmd line before strWhere
Yes, the document is in Print Preview mode when we go to
print.

Existing Code:

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click
Dim stDocName As String
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
stDocName = "Work Order Printout"
strWhere = "[ID] = " & Me.[ID]
DoCmd.OpenReport stDocName, acViewPreview, ,
strWhere
End If

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click
End Sub



Private Sub Work_Order___BeforeUpdate(Cancel As Integer)

End Sub
-----Original Message-----
What space did you remove?
How does the line read now - the one that gives the problem.

The only difference you need to get it to print instead of preview, is to
replace
acViewPreview
with
acViewNormal

Is the preview already open when you attempt to print it?

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Sorry Allen, still one problem. When I copied and pasted
your code, I got an error on the Do.cmd line. I fixed it
by eliminating a space. We have success clicking on the
form button and seeing the active document in print
preview view. However, when we send the document to the
printer, we get the following message:

"The command or action "OpenReport" isn't available now."
*You may be in a read only...
*The type of object the action applies to isn't currently
selected or isn't active for view
Use only the commands or macro actions that are currently
available for this database.

Note:
There is no one else using the system so we are not in
read only mode. I do have a Macro for Opening the report
in Print Preview mode. In the control properties of the
button, I have selected "Expression". If I delete the
Macro, the Work Order button function does not work at all.

K


-----Original Message-----
What is the name of the primary key field of the table
the form gets its
records from? Use that name in place of "YourIdField".

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

Thanks again, tried your suggestion and now get the
following error:

Microsoft Access can't find the field '│' referred to
in
your expression.


-----Original Message-----
Option Compare Database
Option Explicit

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click
Dim stDocName As String
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
stDocName = "Work Order Printout"
strWhere = "[YourIdField] = " & Me.
[YourIdField]
DoCmd.OpenReport stDocName, acViewPreview, ,
strWhere
End If

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click
End Sub


Note that if YourIdField is of type Text, you will need
extra quotes:
strWhere = "[YourIdField] = """ & Me. [YourIdField]
& """"


--
Allen Browne - Microsoft MVP. Perth, Western
Australia.
Tips for Access users -
http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot
org.
Thank very much for the quick reply. I am assuming
this
code is to be applied as an Expression in the On
Click
Control within the properties of the button. My
current
expression is as follows:

Option Compare Database

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click

Dim stDocName As String

stDocName = "Work Order Printout"
DoCmd.OpenReport stDocName, acPreview

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click

End Sub

Can you reformat this code for me as per your example
as I
tried but it didn't work.

Thanks, much appreciated as I have basically self
taught
myself the rest and I am finding this part
frustrating
and
time consuming.

Cheers!
-----Original Message-----
This example assumes you have a numeric primary key
named "OrderID" on your
form, and you want to open a report
named "MyReport":

Private Sub cmdPrint_Click()
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
strWhere = "[OrderID] = " & Me.[OrderID]
DoCmd.OpenReport "MyReport",
acViewPreview, ,
strWhere
End If
End Sub


--
Allen Browne - Microsoft MVP. Perth, Western
Australia.
Tips for Access users -
http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot
org.

My form (a work order)contains a button that
allows
the
user to print the work order in a report format.
However,
when the user clicks and goes to print preview
mode,
the
report brings up every record in page sequence.
User
has
to search through each record to find the correct
Work
Order number and can then print by selecting to
print
only
that page. Does anyone know how to format the
form/print
button/report/database so that clicking on the
button
only
send the active record to print preview?

Thanks!


.



.



.


.
 
That is what I thought. Clicking on the button from the
form works properly. I get the work order report in print
preview mode. However, it will not print. The problem
seems to be with the Macro. It says the macro action
failed and allows you to Halt the action or close the
Action Failed window. I have tried deleting the Macro
since the Click Control is using the expression. When I
do so I can't even get to print preview mode. I am
assuming this is because the button requires a macro.
Anyhow, I am stumped. I am so close but so far. Any
suggestions?

-----Original Message-----
The code looks okay assuming your primary key field really is called "ID",
and it is a number (not a Text field).

If you already have the report open in preview mode, you should be able to
use the toolbar icon to print it instead of running this code again.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

I had to remove a space in the Do.cmd line before strWhere
Yes, the document is in Print Preview mode when we go to
print.

Existing Code:

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click
Dim stDocName As String
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
stDocName = "Work Order Printout"
strWhere = "[ID] = " & Me.[ID]
DoCmd.OpenReport stDocName, acViewPreview, ,
strWhere
End If

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click
End Sub



Private Sub Work_Order___BeforeUpdate(Cancel As Integer)

End Sub
-----Original Message-----
What space did you remove?
How does the line read now - the one that gives the problem.

The only difference you need to get it to print instead of preview, is to
replace
acViewPreview
with
acViewNormal

Is the preview already open when you attempt to print it?

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

Sorry Allen, still one problem. When I copied and pasted
your code, I got an error on the Do.cmd line. I
fixed
it
by eliminating a space. We have success clicking on the
form button and seeing the active document in print
preview view. However, when we send the document to the
printer, we get the following message:

"The command or action "OpenReport" isn't available now."
*You may be in a read only...
*The type of object the action applies to isn't currently
selected or isn't active for view
Use only the commands or macro actions that are currently
available for this database.

Note:
There is no one else using the system so we are not in
read only mode. I do have a Macro for Opening the report
in Print Preview mode. In the control properties of the
button, I have selected "Expression". If I delete the
Macro, the Work Order button function does not work
at
all.
K


-----Original Message-----
What is the name of the primary key field of the table
the form gets its
records from? Use that name in place of "YourIdField".

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

Thanks again, tried your suggestion and now get the
following error:

Microsoft Access can't find the field '│'
referred
to
in
your expression.


-----Original Message-----
Option Compare Database
Option Explicit

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click
Dim stDocName As String
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
stDocName = "Work Order Printout"
strWhere = "[YourIdField] = " & Me.
[YourIdField]
DoCmd.OpenReport stDocName, acViewPreview, ,
strWhere
End If

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click
End Sub


Note that if YourIdField is of type Text, you
will
need
extra quotes:
strWhere = "[YourIdField] = """ & Me. [YourIdField]
& """"


--
Allen Browne - Microsoft MVP. Perth, Western
Australia.
Tips for Access users -
http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot
org.
Thank very much for the quick reply. I am assuming
this
code is to be applied as an Expression in the On
Click
Control within the properties of the button. My
current
expression is as follows:

Option Compare Database

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click

Dim stDocName As String

stDocName = "Work Order Printout"
DoCmd.OpenReport stDocName, acPreview

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click

End Sub

Can you reformat this code for me as per your example
as I
tried but it didn't work.

Thanks, much appreciated as I have basically self
taught
myself the rest and I am finding this part
frustrating
and
time consuming.

Cheers!
-----Original Message-----
This example assumes you have a numeric
primary
key
named "OrderID" on your
form, and you want to open a report
named "MyReport":

Private Sub cmdPrint_Click()
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
strWhere = "[OrderID] = " & Me. [OrderID]
DoCmd.OpenReport "MyReport",
acViewPreview, ,
strWhere
End If
End Sub


--
Allen Browne - Microsoft MVP. Perth, Western
Australia.
Tips for Access users -
http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at
mvps
dot
org.

My form (a work order)contains a button that
allows
the
user to print the work order in a report format.
However,
when the user clicks and goes to print preview
mode,
the
report brings up every record in page sequence.
User
has
to search through each record to find the correct
Work
Order number and can then print by selecting to
print
only
that page. Does anyone know how to format the
form/print
button/report/database so that clicking on the
button
only
send the active record to print preview?

Thanks!


.



.



.



.


.
 
Kathy, I don't understand how the "macro" comes into this.

To use this code, you must have the On Click property of your button set to:
[Event Procedure]
That is different from using a macro, so why is Access talking about macros?

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Kathy said:
That is what I thought. Clicking on the button from the
form works properly. I get the work order report in print
preview mode. However, it will not print. The problem
seems to be with the Macro. It says the macro action
failed and allows you to Halt the action or close the
Action Failed window. I have tried deleting the Macro
since the Click Control is using the expression. When I
do so I can't even get to print preview mode. I am
assuming this is because the button requires a macro.
Anyhow, I am stumped. I am so close but so far. Any
suggestions?

-----Original Message-----
The code looks okay assuming your primary key field really is called "ID",
and it is a number (not a Text field).

If you already have the report open in preview mode, you should be able to
use the toolbar icon to print it instead of running this code again.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

I had to remove a space in the Do.cmd line before strWhere
Yes, the document is in Print Preview mode when we go to
print.

Existing Code:

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click
Dim stDocName As String
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
stDocName = "Work Order Printout"
strWhere = "[ID] = " & Me.[ID]
DoCmd.OpenReport stDocName, acViewPreview, ,
strWhere
End If

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click
End Sub



Private Sub Work_Order___BeforeUpdate(Cancel As Integer)

End Sub

-----Original Message-----
What space did you remove?
How does the line read now - the one that gives the
problem.

The only difference you need to get it to print instead
of preview, is to
replace
acViewPreview
with
acViewNormal

Is the preview already open when you attempt to print it?

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

message
Sorry Allen, still one problem. When I copied and
pasted
your code, I got an error on the Do.cmd line. I fixed
it
by eliminating a space. We have success clicking on the
form button and seeing the active document in print
preview view. However, when we send the document to the
printer, we get the following message:

"The command or action "OpenReport" isn't available
now."
*You may be in a read only...
*The type of object the action applies to isn't
currently
selected or isn't active for view
Use only the commands or macro actions that are
currently
available for this database.

Note:
There is no one else using the system so we are not in
read only mode. I do have a Macro for Opening the
report
in Print Preview mode. In the control properties of the
button, I have selected "Expression". If I delete the
Macro, the Work Order button function does not work at
all.

K


-----Original Message-----
What is the name of the primary key field of the table
the form gets its
records from? Use that name in place of "YourIdField".

--
Allen Browne - Microsoft MVP. Perth, Western
Australia.
Tips for Access users -
http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot
org.

Thanks again, tried your suggestion and now get the
following error:

Microsoft Access can't find the field '│' referred
to
in
your expression.


-----Original Message-----
Option Compare Database
Option Explicit

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click
Dim stDocName As String
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
stDocName = "Work Order Printout"
strWhere = "[YourIdField] = " & Me.
[YourIdField]
DoCmd.OpenReport stDocName, acViewPreview, ,
strWhere
End If

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click
End Sub


Note that if YourIdField is of type Text, you will
need
extra quotes:
strWhere = "[YourIdField] = """ & Me.
[YourIdField]
& """"


--
Allen Browne - Microsoft MVP. Perth, Western
Australia.
Tips for Access users -
http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot
org.
Thank very much for the quick reply. I am
assuming
this
code is to be applied as an Expression in the On
Click
Control within the properties of the button. My
current
expression is as follows:

Option Compare Database

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click

Dim stDocName As String

stDocName = "Work Order Printout"
DoCmd.OpenReport stDocName, acPreview

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click

End Sub

Can you reformat this code for me as per your
example
as I
tried but it didn't work.

Thanks, much appreciated as I have basically self
taught
myself the rest and I am finding this part
frustrating
and
time consuming.

Cheers!
-----Original Message-----
This example assumes you have a numeric primary
key
named "OrderID" on your
form, and you want to open a report
named "MyReport":

Private Sub cmdPrint_Click()
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
strWhere = "[OrderID] = " & Me. [OrderID]
DoCmd.OpenReport "MyReport",
acViewPreview, ,
strWhere
End If
End Sub


--
Allen Browne - Microsoft MVP. Perth, Western
Australia.
Tips for Access users -
http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps
dot
org.

My form (a work order)contains a button that
allows
the
user to print the work order in a report
format.
However,
when the user clicks and goes to print preview
mode,
the
report brings up every record in page sequence.
User
has
to search through each record to find the
correct
Work
Order number and can then print by selecting to
print
only
that page. Does anyone know how to format the
form/print
button/report/database so that clicking on the
button
only
send the active record to print preview?

Thanks!


.



.



.



.


.
 
Since my last email, I have deleted the macro. I now am
back where I started. I have a button on the form for
printing/print previewing the work order. The code now
reads as follows:

Private Sub PRINT_WORK_ORDER_Click()
On Error GoTo Err_PRINT_WORK_ORDER_Click

Dim stDocName As String
Dim strCriteria As String

stDocName = "Work Order Printout"
strCriteria = "[ID]=" & Me![ID] & "'"
DoCmd.OpenReport stDocName, acViewPreview, strCriteria

Exit_PRINT_WORK_ORDER_Click:
Exit Sub

Err_PRINT_WORK_ORDER_Click:
MsgBox Err.Description
Resume Exit_PRINT_WORK_ORDER_Click

End Sub

This code is still enabling me to click on the button and
get to print preview mode. However, print preview mode is
still in page sequence and not displaying only the active
record. The active record is the record that I want to
print. With this code, I do have the ability to print but
I have to search through all records to find my active
record and then select it in the printing format. I tried
entering a new button, and entering the code you
recommended. This resulted in not being able to even get
to print preview mode. I guess we have to start again,
sorry! I appreciate your help.
-----Original Message-----
Option Compare Database
Option Explicit

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click
Dim stDocName As String
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
stDocName = "Work Order Printout"
strWhere = "[YourIdField] = " & Me.[YourIdField]
DoCmd.OpenReport stDocName, acViewPreview, , strWhere
End If

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click
End Sub


Note that if YourIdField is of type Text, you will need extra quotes:
strWhere = "[YourIdField] = """ & Me.[YourIdField] & """"


--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.
Kathy said:
Thank very much for the quick reply. I am assuming this
code is to be applied as an Expression in the On Click
Control within the properties of the button. My current
expression is as follows:

Option Compare Database

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click

Dim stDocName As String

stDocName = "Work Order Printout"
DoCmd.OpenReport stDocName, acPreview

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click

End Sub

Can you reformat this code for me as per your example as I
tried but it didn't work.

Thanks, much appreciated as I have basically self taught
myself the rest and I am finding this part frustrating and
time consuming.

Cheers!
-----Original Message-----
This example assumes you have a numeric primary key named "OrderID" on your
form, and you want to open a report named "MyReport":

Private Sub cmdPrint_Click()
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
strWhere = "[OrderID] = " & Me.[OrderID]
DoCmd.OpenReport "MyReport", acViewPreview, , strWhere
End If
End Sub


--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

My form (a work order)contains a button that allows the
user to print the work order in a report format. However,
when the user clicks and goes to print preview mode, the
report brings up every record in page sequence. User has
to search through each record to find the correct Work
Order number and can then print by selecting to print only
that page. Does anyone know how to format the form/print
button/report/database so that clicking on the button only
send the active record to print preview?

Thanks!


.


.
 
You are missing a comma:

DoCmd.OpenReport stDocName, acViewPreview, , strCriteria

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Since my last email, I have deleted the macro. I now am
back where I started. I have a button on the form for
printing/print previewing the work order. The code now
reads as follows:

Private Sub PRINT_WORK_ORDER_Click()
On Error GoTo Err_PRINT_WORK_ORDER_Click

Dim stDocName As String
Dim strCriteria As String

stDocName = "Work Order Printout"
strCriteria = "[ID]=" & Me![ID] & "'"
DoCmd.OpenReport stDocName, acViewPreview, strCriteria

Exit_PRINT_WORK_ORDER_Click:
Exit Sub

Err_PRINT_WORK_ORDER_Click:
MsgBox Err.Description
Resume Exit_PRINT_WORK_ORDER_Click

End Sub

This code is still enabling me to click on the button and
get to print preview mode. However, print preview mode is
still in page sequence and not displaying only the active
record. The active record is the record that I want to
print. With this code, I do have the ability to print but
I have to search through all records to find my active
record and then select it in the printing format. I tried
entering a new button, and entering the code you
recommended. This resulted in not being able to even get
to print preview mode. I guess we have to start again,
sorry! I appreciate your help.
-----Original Message-----
Option Compare Database
Option Explicit

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click
Dim stDocName As String
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
stDocName = "Work Order Printout"
strWhere = "[YourIdField] = " & Me.[YourIdField]
DoCmd.OpenReport stDocName, acViewPreview, , strWhere
End If

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click
End Sub


Note that if YourIdField is of type Text, you will need extra quotes:
strWhere = "[YourIdField] = """ & Me.[YourIdField] & """"
Kathy said:
Thank very much for the quick reply. I am assuming this
code is to be applied as an Expression in the On Click
Control within the properties of the button. My current
expression is as follows:

Option Compare Database

Private Sub Print_Preview_Work_Order_Click()
On Error GoTo Err_Print_Preview_Work_Order_Click

Dim stDocName As String

stDocName = "Work Order Printout"
DoCmd.OpenReport stDocName, acPreview

Exit_Print_Preview_Work_Order_Click:
Exit Sub

Err_Print_Preview_Work_Order_Click:
MsgBox Err.Description
Resume Exit_Print_Preview_Work_Order_Click

End Sub

Can you reformat this code for me as per your example as I
tried but it didn't work.

Thanks, much appreciated as I have basically self taught
myself the rest and I am finding this part frustrating and
time consuming.

Cheers!
-----Original Message-----
This example assumes you have a numeric primary key
named "OrderID" on your
form, and you want to open a report named "MyReport":

Private Sub cmdPrint_Click()
Dim strWhere As String

If Me.Dirty Then 'save any edits
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to view."
Else
strWhere = "[OrderID] = " & Me.[OrderID]
DoCmd.OpenReport "MyReport", acViewPreview, ,
strWhere
End If
End Sub


--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

My form (a work order)contains a button that allows the
user to print the work order in a report format.
However,
when the user clicks and goes to print preview mode, the
report brings up every record in page sequence. User
has
to search through each record to find the correct Work
Order number and can then print by selecting to print
only
that page. Does anyone know how to format the
form/print
button/report/database so that clicking on the button
only
send the active record to print preview?

Thanks!
 
Back
Top