Thanks for the Reply Mary,
I have a VB application that generates reports in batch process as a windows
service. It opens an Access DB and runs a report against either an AS400 or
MSSQL database. Then it saves the results of the report to a snapfile. I
want to eventually turn that into a PDF file. However i just need to get the
current code updated and running. Here is the funciton that does the work.
Public Sub CAccessSnapShot()
Dim vSQLText As String = ""
Dim sAccessError As String = ""
Dim strMDBName, strSNPName As String
Dim oCmdOutput As Access.DoCmd
Try
Dim dtSQLText As DataTable
' GET THE SQL ASSOCIATED WITH THIS REPORT
dtSQLText = cFunctions.GetSQL(pReportRefNum, pQConnectionString)
If dtSQLText.Rows.Count > 0 Then
vSQLText = CStr(dtSQLText.Rows(0).Item("SQLText"))
Else
vSQLText = ""
End If
' GET THE NAME OF THE TEMPORARY MDB AND MAKE A COPY TO WORK WITH
'vStep = 1
'cFunctions.WriteEventLog("Step " & vStep)
strNwFileNM = cFunctions.getLongRptName(pQConnectionString,
pReportName) + "--" + cFunctions.GetNewFileName()
If CStr(strNwFileNM).Length > 60 Then
strNwFileNM = CStr(strNwFileNM).Substring(0, 60)
End If
'vStep = 2
'cFunctions.WriteEventLog("Step " & vStep)
strMDBName = strNwFileNM + ".mdb"
strMDBPath = pInputFileDir + strNwFileNM + ".mdb"
'vStep = 3
'cFunctions.WriteEventLog("Step " & vStep)
Try
objAccess.CloseCurrentDatabase() ' SMT 6-20
Catch ex As Exception
'cFunctions.WriteEventLog("Close Current DB: " &
pReportRefNum & " --- " & ex.Message.ToString)
End Try
'vStep = 4
'cFunctions.WriteEventLog("Step " & vStep)
' OPEN THE NEW COPY OF THE MDB
Dim copymdb As Boolean = False
Dim copycnt As Integer = 0
While copymdb = False And copycnt < 5
Try
cFunctions.copyfile(pInputFileDir + pInputFileName,
strMDBPath)
copymdb = True
'vStep = 5
'cFunctions.WriteEventLog("Step " & vStep)
Catch errorVariable As Exception
'cFunctions.deletefile(strMDBPath)
'Exit While
Thread.Sleep(3000)
Finally
copycnt += 1
End Try
End While
'MOVE THE RETURN TO Q FROM COPY MDB OUTSIDE OF THE LOOP
If copymdb = False Then
cFunctions.UpdateReportReturnToQueue(pReportRefNum,
pQConnectionString)
objAccess.CloseCurrentDatabase()
cFunctions.WriteEventLog("return to queue :" & pReportRefNum
& " error at copy mdb: ")
Else
Dim openmdb As Boolean = False
Dim opencnt As Integer = 0
'vStep = 6
'cFunctions.WriteEventLog("Step " & vStep)
While openmdb = False And opencnt < 5
Try
'cFunctions.WriteEventLog("Step Try to Open DB: " &
pReportRefNum)
objAccess.OpenCurrentDatabase(strMDBPath, False)
openmdb = True
'cFunctions.WriteEventLog("Opened DB: " &
pReportRefNum)
' GET THE NEW NAME OF THE SNAP FILE
Catch ex As Exception
'cFunctions.WriteEventLog("Step Did not Open DB: " &
pReportRefNum)
cFunctions.WriteEventLog("OpenDB Error -- rptrefnum:
" & pReportRefNum & " cnt: " & CStr(opencnt) & "-" & ex.Message & " -- " &
strMDBPath)
Thread.Sleep(3000)
Try
'cFunctions.WriteEventLog("OpenDB Error try to
close")
objAccess.DoCmd.Close(Access.AcObjectType.
acReport, pReportName, Access.AcCloseSave.acSaveNo)
objAccess.CloseCurrentDatabase()
Catch ex1 As Exception
cFunctions.WriteEventLog("Close Current DB 2: " &
pReportRefNum & " --- " & ex1.Message.ToString)
End Try
Finally
opencnt += 1
End Try
End While
'cFunctions.WriteEventLog("outside openmdb: " & pReportRefNum)
If openmdb = False Then
'cFunctions.WriteEventLog("failed to open and return to
queue" & pReportRefNum)
Try
cFunctions.UpdateReportReturnToQueue(pReportRefNum,
pQConnectionString)
cFunctions.WriteEventLog("Could not Open MDB Returned
to Queue : " & pReportRefNum & " --- ")
Catch ex As Exception
cFunctions.UpdateReportError(pReportRefNum, CStr
(vStep) + " == " + ex.Message.ToString, pQConnectionString)
End Try
Else
' RUN THE FUNCTION FROM ACCESS TO CREATE THE REPORT
SNAPSHOT
Try
'cFunctions.WriteEventLog("new snp name: " &
strSNPName)
sAccessError = objAccess.Run("SetReportParams", CStr
(vSQLText), CStr(pConnectString), CStr(pReportName))
cFunctions.WriteEventLog("strSNPName: " & strSNPName)
objAccess.DoCmd.OpenReport(pReportName, Access.AcView.
acViewPreview)
cFunctions.WriteEventLog("Access OpenRpt: " &
pReportRefNum)
'If pReportType = "SNP" Then
oCmdOutput = objAccess.DoCmd
'**** THIS IS WHERE THE ERROR OCCURS
oCmdOutput.OutputTo(Access.AcOutputObjectType.
acOutputReport, pReportName, "Snapshot Format", pOutputFileDir & strSNPName,
False)
'***********
cFunctions.WriteEventLog("Access Output: " &
pReportRefNum)
cFunctions.UpdateReportComplete(pReportRefNum,
strSNPName, pQConnectionString)
Catch ex As Exception
cFunctions.WriteEventLog("Access Rpt Error: " & ex.
Message)
End Try
End If
' IF AN ERROR OCCURED MARK THE REPORT AS ERRORED
Thread.Sleep(5000)
If sAccessError <> "" Then
cFunctions.UpdateReportError(pReportRefNum, CStr(vStep) +
" == " + sAccessError + " Access Error", pQConnectionString)
cFunctions.WriteEventLog("GPReportServer.NET-Access-Error
(" & pReportRefNum & " errored at step " & CStr(vStep) & "): " & sAccessError)
cFunctions.UpdateReportReturnToQueue(pReportRefNum,
pQConnectionString)
cFunctions.WriteEventLog("return to queue (" &
pReportRefNum & " Access Error ")
Else
'If writesnpfile = True Then
'cFunctions.UpdateReportComplete(pReportRefNum,
strSNPName, pQConnectionString)
'Else
'cFunctions.UpdateReportError(pReportRefNum, CStr(vStep)
+ " == " + sAccessError + " Access Error", pQConnectionString)
'cFunctions.WriteEventLog("Could not write snp file (" &
pReportRefNum)
'cFunctions.UpdateReportReturnToQueue(pReportRefNum,
pQConnectionString)
'cFunctions.WriteEventLog("return to queue (" &
pReportRefNum & " write snp file Error ")
End If
End If
' REPORT IS COMPLETE CLOSE MDB
vStep = 5
objAccess.DoCmd.Close(Access.AcObjectType.acReport, pReportName,
Access.AcCloseSave.acSaveNo)
objAccess.CloseCurrentDatabase()
'End If
'End If
Catch errorVariable As System.Runtime.InteropServices.COMException
' ERROR HANDLING
'GC.Collect()
'Thread.Sleep(5000)
' WRITE ANY ERROR TO EVENT LOG
objAccess.DoCmd.Close(Access.AcObjectType.acReport, pReportName,
Access.AcCloseSave.acSaveNo)
objAccess.CloseCurrentDatabase()
cFunctions.WriteEventLog("ReportServer.NET-Access-COMError (" &
pReportRefNum & ") errored at step " & CStr(vStep) & "): " & errorVariable.
Message.ToString & " mdb name: " & strMDBPath)
cFunctions.UpdateReportError(pReportRefNum, "COM Exc == " +
errorVariable.Message.ToString, pQConnectionString)
cFunctions.UpdateReportReturnToQueue(pReportRefNum,
pQConnectionString)
cFunctions.WriteEventLog("return to queue (" & pReportRefNum & ")
InteropServices.COMException ")
Catch errorVariable As System.Threading.ThreadAbortException
objAccess.DoCmd.Close(Access.AcObjectType.acReport, pReportName,
Access.AcCloseSave.acSaveNo)
objAccess.CloseCurrentDatabase()
cFunctions.WriteEventLog("ReportServer.NET-Access-
ThreadAboutException (" & pReportRefNum & " ThreadAbortException " & CStr
(vStep) & "): " & errorVariable.Message.ToString)
cFunctions.UpdateReportError(pReportRefNum, "ThreadAbort == " +
errorVariable.Message.ToString, pQConnectionString)
cFunctions.UpdateReportReturnToQueue(pReportRefNum,
pQConnectionString)
cFunctions.WriteEventLog("return to queue (" & pReportRefNum & "
ThreadAbortException ")
Exit Sub
Catch errorVariable As Exception
' ERROR HANDLING
objAccess.DoCmd.Close(Access.AcObjectType.acReport, pReportName,
Access.AcCloseSave.acSaveNo)
objAccess.CloseCurrentDatabase()
cFunctions.WriteEventLog("ReportServer.NET-Access-Error (" &
pReportRefNum & " errored at step " & CStr(vStep) & "): " & errorVariable.
Message.ToString)
cFunctions.UpdateReportError(pReportRefNum, "LastAccessError == "
+ errorVariable.Message.ToString + " Exception" + CStr(vStep),
pQConnectionString)
cFunctions.UpdateReportReturnToQueue(pReportRefNum,
pQConnectionString)
cFunctions.WriteEventLog("return to queue (" & pReportRefNum & "
NET-Access-Error ")
Finally
' WAIT FOR 5 SECONDS TO GIVE ACCESS TIME TO CLOSE MDB AND THEN
DELETE THE WORKING FILE
'Thread.Sleep(8000)
cFunctions.deletefile(strMDBPath)
End Try
End Sub
Post by Mary Chipman [MSFT]Do you have a link to the online documentation? Generally speaking,
DoCmd was designed to access Access UI objects, mirroring macro
commands, although it has evolved considerably in Access 2007. What
are you attempting to use outputto command for? There might perhaps be
a better way to tackle the problem that doesn't use outputto.
--Mary
Post by smthomasI have a project i am upgrading from the Access XP to Access 2007 interop.
[quoted text clipped - 10 lines]
Post by smthomasThanks for any help.
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/Forums.aspx/dotnet-interop/200910/1