Question : Write Excel Data to Access via ADO - Possible Database Sharing Conflict

The included code module (Thanks to Rorya) writes specific columns from an Excel 2003 worksheet to an existing table in an Access 2003 database.  The code works perfectly if the Excel file has had no previous interaction with the Access database.  However, if the Excel file has executed a QueryTable extract of data from the database before this code writes to Access then it fails with a Run-Time Error '-2147467529 (80004005)' at line #30.

I believe this may be a sharing violation.  After the QueryTable read from Access has updated and before the Excel file is closed if I open the Access file (manually) it will only open as read only which seems to indicate Excel still has control of the file.  Is there a command I can add to this code that will allow it to run correctly after multiple QueryTable reads from the Access file?

Thanks,
Jerry  
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
Sub ExportDataFromWorkbookToAccess()
   ' Export data from an Excel 2003 workbook
   ' to a 2003 format Access database
   
   Dim cn As Object, strQuery As String
   Set cn = CreateObject("ADODB.Connection")
   
   myDB = Worksheets("Tables+").Cells(7, "M").Value
   ThisWB = "'" & ThisWorkbook.Path & "\" & ThisWorkbook.Name & "'"
 
   With cn
      .Provider = "Microsoft.Jet.OLEDB.4.0"
      .ConnectionString = myDB
      .Open
   End With
 
   strQuery = "INSERT INTO [ADPUpload] " & _
              "SELECT " & _
                 """3MT""" & " as [Co Code], " & _
                 """KCOM""" & " as [Batch ID], " & _
                 " ADPNum as [File #], " & _
                 "CostCenter as [Temp Dept], " & _
                  """I""" & " as [Earnings 3 Code], " & _
                 "Commissions as [Earnings 3 Amount], " & _
                 "[Sales Rep] " & _
              "FROM [Detail+$] " & _
              "IN " & ThisWB & " 'Excel 8.0;HDR=Yes;'  " & _
              "WHERE [Set of Books Id] = 1;"
   
   cn.Execute "DELETE FROM ADPUpload"
   cn.Execute strQuery
   cn.Close
   Set cn = Nothing
End Sub

Answer : Write Excel Data to Access via ADO - Possible Database Sharing Conflict

Can you amend it to use OLEDB (using the get data from Access option)? If not, you might try opening the Data Sources (ODBC) option under admin tools in control panel and set the Read Only property for the MS Access Databases driver to True. (bit of a long shot!)
Random Solutions  
 
programming4us programming4us