Code fires properly once only

IMO

Now Known as ___
Local time
Today, 11:03
Joined
Sep 11, 2002
Messages
723
I have a db which imports a sheet (Shipping Details) from Excel to a table. The user then selects a supplier code from a listbox for deletion and the following code runs a query (criteria based on the ListBox) to delete that supplier from the table, then transfers the table to a new sheet within the workbook, deletes the original sheet and renames the newly imported sheet. It then closes and saves the workbook without it being visible. This all works fine the first time it fires, but, if the user selects a second supplier for deletion, the code does not close and save the workbook.
Has anybody got an idea why this is? Any help greatly appreciated.

Thanks
IMO

Code:
Public Sub DeleteSheet()

DoCmd.OpenQuery "qryDeleteSupplier", acNormal, acEdit
DoCmd.TransferSpreadsheet acExport, 8, "Shipping Details", "H:\BIDataLoad\Supplier Orders.xls", True, ("Shipping_Details")

Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim db As Database
Const conWKB_NAME = "H:\BIDataLoad\Supplier Orders.xls"
  Set db = CurrentDb
  Set objXL = New Excel.Application

  With objXL
    .Visible = False
    Set objWkb = .Workbooks.Open(conWKB_NAME)

    .DisplayAlerts = False
    Sheets("Shipping Details").Select
    ActiveWindow.SelectedSheets.Delete
    Sheets("Shipping_Details").Select
    Sheets("Shipping_Details").name = "Shipping Details"
    .DisplayAlerts = True
    
            objXL.ActiveWorkbook.Save
            objXL.ActiveWorkbook.Close
            objXL.Quit
        
  End With
  
        Set objSht = Nothing
        Set objWkb = Nothing
        Set objXL = Nothing
        Set db = Nothing

End Sub
 
Don´t know if this is relevant, but I once did an export to excel of a query with a field (selected with listbox) as a criteria. I noticed it only worked occassionally like it was supposed to. In my case, I had forgotten to put me.refresh for the form, not updating the criteria field and thus executing the same query again.

Do you think something like that could be going on?

Fuga.
 
Thanks for the reply. The listbox refreshes ok, it seems to be this bit of code that only wants to fire once...
objXL.ActiveWorkbook.Save
objXL.ActiveWorkbook.Close
objXL.Quit

End With

Set objSht = Nothing
Set objWkb = Nothing
Set objXL = Nothing
Set db = Nothing
I've got no idea why!!!
banghead.gif
Anybody ever encountered this problem?

Thanks
IMO
 

Users who are viewing this thread

Back
Top Bottom