help with this code please

M

mhmaid

hi all,
I have one form with cbo to list reports, and two buttons one to preview ,
the other to print the selected report/s. Now I want to add one more button
to sent report to file, but i am unable to get it work.can someone help me
this is the code(I think the problem is in this line :DoCmd.OutputTo
acReport, stDocName, , strLinkCriteria )

Private Sub Command8_Click()
On Error GoTo Err_Command8_Click

Dim ctl As Control
Dim varItem As Variant
Dim strSQL As String, strDocName As String, strLinkCriteria As String

Set ctl = Me.lstReports
For Each varItem In ctl.ItemsSelected
strDocName = ctl.ItemData(varItem)
strLinkCriteria = "[IDNO]=[Forms]![NEW CASES]![IDNO]"
DoCmd.OutputTo acReport, stDocName, , strLinkCriteria
Next varItem

Exit_Command8_Click:
Exit Sub

Err_Command8_Click:
MsgBox Err.Description
Resume Exit_Command8_Click

End Sub

same code i am using to print or preview reports and its working ok.
 
S

Steve Schapel

Mhmaid,

This is not the valid syntax for the OutputTo method. You need to
specify arguments for the format and path/file for the outputted
document. You can't specify a where condition for OutputTo so the
criteria will have to be included in the query that the report is based
on. Assuming you are exporting to a snapshot file, the code will be
something like this...
DoCmd.OutputTo acOutputReport, stDocName, "SnapshotFormat(*.snp)",
"C:\YourFolder\Yourdoc.snp"
 
M

mhmaid

thanks steve for reply.i have tried the following but i get the following
method
"the object type urgument for the action or method is blank or invalid"

Private Sub Command8_Click()
On Error GoTo Err_Command8_Click

Dim ctl As Control
Dim varItem As Variant
Dim strSQL As String, strDocName As String, strLinkCriteria As String

Set ctl = Me.lstReports
For Each varItem In ctl.ItemsSelected
strDocName = ctl.ItemData(varItem)
DoCmd.OutputTo acOutputReport, stDocName,"SnapshotFormat(*.snp)",
"C:\myfiles\exprpt.snp"
Next varItem

Exit_Command8_Click:
Exit Sub

Err_Command8_Click:
MsgBox Err.Description
Resume Exit_Command8_Click

End Sub

what is the problem?
 
S

Steve Schapel

Mhmaid,

Looks like a typo... your variable is strDocName and in the OutputTo row
it is stDocName.

There is also a problem with the code, in that it will output a report
for each item in the listbox, but since the file name for the outputted
report is hard-coded, you will only end up with one file, so this aspect
needs some attention.
 

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