Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to prevent Run time error '2302': Microsoft Office Access can't 1

Status
Not open for further replies.

GarHeard

Programmer
May 3, 2005
28
US
If one user of my application clicks on a button to create an Excel file report, my Access application works fine.

However, if that user has the Excel file open and then another uses clicks on a button to create yet another Excel file, I get the error message as follows:

Run time error '2302': Microsoft Office Access can't save the output data to the file you've selected

Is there a way that I can modify the following code to see if the Excel file is already open and if so, write to another file and open the new file.
Perhaps I could increment the file suffix name to create unique file names everytime the file is written to and opened.


Set com = New ADODB.Command
With com
.CommandType = adCmdStoredProc
.CommandText = "dbo.procSpForeign"
.Parameters.Append .CreateParameter("RptYear", adInteger, adParamInput, 4, intYearSP)
.ActiveConnection = cn
Set rstQueryFS = .Execute
End With
ExportedFile = "C:\UDL\FRGNCTRY.XLS"
DoCmd.OutputTo acOutputTable, "tblSpFCY", acFormatXLS, ExportedFile
If isFileExist(ExportedFile) Then StartDocXLS ExportedFile

Private Function StartDocXLS(FileName)
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet

'open excel template
Set xlApp = New Excel.Application
xlApp.Visible = True
Set xlWB = xlApp.Workbooks.Open(FileName)
Set xlWS = xlWB.Worksheets(1)
xlApp.ScreenUpdating = True
End Function

Private Function isFileExist(filePath As String) As Boolean
isFileExist = (filePath <> "" And Dir$(filePath) <> "")
End Function
 
Hi!

Try this:

ExportedFile = "C:\UDL\FRGNCTRY " & Format(Now()) & ".XLS"

hth


Jeff Bridgham
Purdue University
Graduate School
Data Analyst
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top