I need help to alter a VBA code to add print preview

S

Steve Taber

I have the below code to select a report from a list box and then open the
specific report. In it's current status it just prints the report, and I want
to have it open as a preview. I have tried to insert the acView Preview
command, but I can't get it to work. Any ideas?

Private Sub Command5_Click()
Dim strWhere As String, varItem As Variant
' Request to edit items selected in the list box
' If no items selected, then nothing to do
If Me!List21.ItemsSelected.Count = 0 Then Exit Sub
' Loop through the items selected collection
For Each varItem In Me!List21.ItemsSelected
' Grab the CompanyID column for each selected item
strWhere = strWhere & Me!List21.Column(0, varItem) & ","
Next varItem
' Throw away the extra comma on the "IN" string
strWhere = Left$(strWhere, Len(strWhere) - 1)
' Open the companies form filtered on the selected companies
strWhere = "[Number] IN (" & strWhere & ")"


Debug.Print strWhere

DoCmd.OpenReport ReportName:="rptIncidentReport", WhereCondition:=strWhere
DoCmd.Close acForm, Me.Name


End Sub
 
B

Brian Bastl

Hi Steve,
try:
DoCmd.OpenReport ReportName:="rptIncidentReport", acViewPreview,,strWhere

HTH,
Brian
 
S

Steve Taber

Brian,
I'm now getting a compile error: expected: named parameter.
Any ideas?
Thanks,

Brian Bastl said:
Hi Steve,
try:
DoCmd.OpenReport ReportName:="rptIncidentReport", acViewPreview,,strWhere

HTH,
Brian


Steve Taber said:
I have the below code to select a report from a list box and then open the
specific report. In it's current status it just prints the report, and I want
to have it open as a preview. I have tried to insert the acView Preview
command, but I can't get it to work. Any ideas?

Private Sub Command5_Click()
Dim strWhere As String, varItem As Variant
' Request to edit items selected in the list box
' If no items selected, then nothing to do
If Me!List21.ItemsSelected.Count = 0 Then Exit Sub
' Loop through the items selected collection
For Each varItem In Me!List21.ItemsSelected
' Grab the CompanyID column for each selected item
strWhere = strWhere & Me!List21.Column(0, varItem) & ","
Next varItem
' Throw away the extra comma on the "IN" string
strWhere = Left$(strWhere, Len(strWhere) - 1)
' Open the companies form filtered on the selected companies
strWhere = "[Number] IN (" & strWhere & ")"


Debug.Print strWhere

DoCmd.OpenReport ReportName:="rptIncidentReport", WhereCondition:=strWhere
DoCmd.Close acForm, Me.Name


End Sub
 
B

Brian Bastl

Honestly not sure since all we did was add "acViewPreview". Perhaps change
DoCmd....... strWhere back to WhereCondition:=strWhere. Meant to leave it in
anyhow.

Brian


Steve Taber said:
Brian,
I'm now getting a compile error: expected: named parameter.
Any ideas?
Thanks,

Brian Bastl said:
Hi Steve,
try:
DoCmd.OpenReport ReportName:="rptIncidentReport", acViewPreview,,strWhere

HTH,
Brian


Steve Taber said:
I have the below code to select a report from a list box and then open the
specific report. In it's current status it just prints the report, and
I
want
to have it open as a preview. I have tried to insert the acView Preview
command, but I can't get it to work. Any ideas?

Private Sub Command5_Click()
Dim strWhere As String, varItem As Variant
' Request to edit items selected in the list box
' If no items selected, then nothing to do
If Me!List21.ItemsSelected.Count = 0 Then Exit Sub
' Loop through the items selected collection
For Each varItem In Me!List21.ItemsSelected
' Grab the CompanyID column for each selected item
strWhere = strWhere & Me!List21.Column(0, varItem) & ","
Next varItem
' Throw away the extra comma on the "IN" string
strWhere = Left$(strWhere, Len(strWhere) - 1)
' Open the companies form filtered on the selected companies
strWhere = "[Number] IN (" & strWhere & ")"


Debug.Print strWhere

DoCmd.OpenReport ReportName:="rptIncidentReport", WhereCondition:=strWhere
DoCmd.Close acForm, Me.Name


End Sub
 
S

Steve Taber

Brian,
I tried removing ReportName:= and it worked. Thanks for your help!
Steve

Brian Bastl said:
Honestly not sure since all we did was add "acViewPreview". Perhaps change
DoCmd....... strWhere back to WhereCondition:=strWhere. Meant to leave it in
anyhow.

Brian


Steve Taber said:
Brian,
I'm now getting a compile error: expected: named parameter.
Any ideas?
Thanks,

Brian Bastl said:
Hi Steve,
try:
DoCmd.OpenReport ReportName:="rptIncidentReport", acViewPreview,,strWhere

HTH,
Brian


I have the below code to select a report from a list box and then open the
specific report. In it's current status it just prints the report, and I
want
to have it open as a preview. I have tried to insert the acView Preview
command, but I can't get it to work. Any ideas?

Private Sub Command5_Click()
Dim strWhere As String, varItem As Variant
' Request to edit items selected in the list box
' If no items selected, then nothing to do
If Me!List21.ItemsSelected.Count = 0 Then Exit Sub
' Loop through the items selected collection
For Each varItem In Me!List21.ItemsSelected
' Grab the CompanyID column for each selected item
strWhere = strWhere & Me!List21.Column(0, varItem) & ","
Next varItem
' Throw away the extra comma on the "IN" string
strWhere = Left$(strWhere, Len(strWhere) - 1)
' Open the companies form filtered on the selected companies
strWhere = "[Number] IN (" & strWhere & ")"


Debug.Print strWhere

DoCmd.OpenReport ReportName:="rptIncidentReport",
WhereCondition:=strWhere
DoCmd.Close acForm, Me.Name


End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top