Link to home
Start Free TrialLog in
Avatar of zimmer9
zimmer9Flag for United States of America

asked on

How to resolve Run-time error '3010' ?

Do you know why the compiler stops on the line below and gives me the following message in the following procedure:
I am trying to run a query and export it to Excel.
----------------------------------------------------------------------------------------------------------
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qryFA", "c:\FA.xls"

Run-time error '3010':
Table 'qryFA' already exists.
-----------------------------------------------------------------------------------------------------------------------------------------

When you export a query to excel it takes the name of the query as name of the sheet
you can rename your query with  NON-CTS and export it. or use the code below to transfer and rename the sheet
--------------------------------------
Private Sub cmdTransfer_Click()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qryFA", "c:\FA.xls"
   Set xlApp = CreateObject("Excel.Application")
   
   ' Open the spreadsheet to which you exported the data.
   Set xlBook = xlApp.Workbooks.Open("C:\FA.xls")
    Set xlSheet = xlBook.Worksheets("qryFA")
 'rename the sheet  
  xlSheet.Name = "NON-CTS"
   
         xlBook.Saved = True
         Set xlSheet = Nothing
         Set xlBook = Nothing
         'xlApp.Quit
         Set xlApp = Nothing
 
End Sub
Avatar of Leigh Purvis
Leigh Purvis
Flag of United Kingdom of Great Britain and Northern Ireland image

Are you 100% sure that the target workbook doesn't already have a sheet named qryFA from the last time?

Did you get that code from somewhere - or write it yourself?
Ah - I see you got it from Cap.
So it should work - I wonder why it isn't.

Confirm what I've asked above.

Should you perhaps have
xlBook.Save
instead of
xlBook.Saved = True
(I'd have thought that would discard changes rather than persist them - as it thinks the book is then saved).

And that would then leave you a qryFA sheet in your book as is being reported
Avatar of dmitryz6
dmitryz6

IF You have file ,you need to delete it ,use KILL command

Kill "c:\FA.xls"
DoCmd.TransferSpreadsheet acExport,acSpreadsheetTypeExcel9, "qryFA", "c:\FA.xls"

I am prefer to use

 docmd.OutputTo
I think the intention is to caress the sheet out of the way rather than blat it out of existence lol

Although that raises a point.
If you rename it to "NON-CTS" then you'll not be able to remane the next one to that.

Perhaps you might consider renaming it to something based on the date/time?
Then you could have a progression of sheets - from particular exports.
Avatar of zimmer9

ASKER

Kill "c:\FA.xls"
causes

Runtime error '70'
Permission denied
You'd have to do that before you open the file of course?
And make sure that no phantom Excel apps might still have a hold on it.

Is that actually what you want to do then?
Avatar of zimmer9

ASKER

How about this:

Is there a way to run the query qryFA and SELECT into Table tblTest without displaying the result set of the query qryFA.

Then DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "tblTest", "c:\FA.xls", NOCTR
Have you tried what we've asked though?

Have you opened c:\FA.xls and made sure there isn't already a sheet named qryFA ?
If so - get rid of it.
Then make the changes regarding saving it before running the code again.
See if it works - if it does, run it again - see if it works again.

As for selecting all into tblTest, well yes, you could.
But what would that achieve?
You'll still have to prevent there from being more than one sheet named tblTest!
Create "c:\FA.xls" manualy.Open it rename sheet to NON-CTS.
Go to your Access application.File ->get External data->Link tables _> change file tape to xls and link your spreadsheet.
You can Use Excel Link table.
Private Sub cmdTransfer_Click()
docmd.runsql "delete * from [NON-CTS]"
docmd.runsql "INSERT INTO [NON-CTS] SELECT qryFA.* FROM qryFA;"

end sub
Avatar of zimmer9

ASKER

How do I display the Spreadsheet automatially on the monitor after the user clicks a button to trigger the procedure ?
Avatar of zimmer9

ASKER

How do I display the Spreadsheet automatially on the monitor after the user clicks a button to trigger the following procedure ?


Private Sub List150_DblClick(Cancel As Integer)
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
If (Len(Dir("C:\FA.XLS")) > 0) Then
    Kill "C:\FA.XLS"
End If
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qryFA", "C:\FA.XLS"
Set xlApp = CreateObject("Excel.Application")
   
' Open the spreadsheet to which you exported the data.
Set xlBook = xlApp.Workbooks.Open("C:\FA.XLS")
Set xlSheet = xlBook.Worksheets("qryFA")
' rename the sheet
xlSheet.Name = "NON-CTS"
   
xlBook.Saved = True
Set xlSheet = Nothing
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
'myCostCenter = List150.List(0)
'Run qryFa
'DoCmd.OutputTo acOutputQuery, "qryFA", acFormatXLS, "c:\FA.xls", True, "NON-CTS"

End Sub
You already have that code
In your original procedure immediately before and after the suggestful line

' Open the spreadsheet to which you exported the data.


Just don't then execute the name change
'rename the sheet  
  'xlSheet.Name = "NON-CTS"
What are you doing?
Everything at the bottom is commented out.

What exactly have you done regarding solving the problem you asked about?
Have you even tried any of the suggestions?

<sighs>
Do you mean make Excel the active application?
At a really simple level...

AppActivate "Microsoft Excel - FA"
Avatar of zimmer9

ASKER

Yes I meant to make Excel the active application and display the spreadsheet when the user double clicks on a control.

AppActivate "Microsoft Excel - FA"

causes a Runtime error '5'
Invalid Procedure call or argument

Where do I insert this statement anyway ?
------------------------------------------------------------
Private Sub List150_DblClick(Cancel As Integer)
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
If (Len(Dir("C:\FA.XLS")) > 0) Then
    Kill "C:\FA.XLS"
End If
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qryFA", "C:\FA.XLS"
Set xlApp = CreateObject("Excel.Application")
' Open the spreadsheet to which you exported the data.
Set xlBook = xlApp.Workbooks.Open("C:\FA.XLS")
Set xlSheet = xlBook.Worksheets("qryFA")
' rename the sheet
' xlSheet.Name = "NON-CTS"
   
xlBook.Saved = True
Set xlSheet = Nothing
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing

End Sub
ASKER CERTIFIED SOLUTION
Avatar of Leigh Purvis
Leigh Purvis
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ignor my comments about Linkin to Excel microsoft not support deete from Link Excel table.May be your have this trable because you don't have permission foer this file(or folder).ormay be you stoped code and not closed xlSheet object.In this case reboot your computer try this again
 Kill "c:\FA.xls"
DoCmd.TransferSpreadsheet acExport,acSpreadsheetTypeExcel9, "qryFA", "c:\FA.xls"

Good luck
Try
----------------------------------------------------------------------------------------------------------
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qryFA", "c:\FA.xls", "NON-CTS_" & Fromat(Now(),"YYYY_MM_DD_HH_NN"
----------------------------------------------------------------------------------------------------------