Solved

Excel workbook not released after being read from MS Access

Posted on 2014-04-27
2
577 Views
Last Modified: 2014-04-27
I use VBA in Access to read data from an Excel workbook and append it to a table in Access.

Problem is that I cannot edit the Excel workbook afterwards, because it is "open in Excel". Below is the message that I get when trying to rename the Excel workbook. Then below that is the  code that I use.

What should I do?

Error message
Private Sub cmdReadAndAppend_Click()

Dim ExcelApp As excel.Application
Dim ExcelBook As excel.Workbook
Dim ExcelSheet As excel.Worksheet

Set ExcelApp = CreateObject("Excel.Application")
Set ExcelBook = ExcelApp.Workbooks.Open(txtWorkbookToOpen)
'Define which sheet to read from
Set ExcelSheet = ExcelBook.Worksheets("Agreement Terms")

'Set ExcelSheet = ExcelBook.Worksheets(1)

Dim var As Variant 'there may be a better type to use here
'var = WkBk.Sheets(1).Range("A1").Value
'var = WkBk.Sheets(ExcelSheet).Range("A1").Value

    var = ExcelSheet.Cells(1, 1).Value
'    var = ExcelBook.Sheets(ExcelSheet).Cells(1, 1).Value
    txtExcelValueIs = var
    MsgBox txtExcelValueIs

Dim Licensor As Variant
Dim Period As Variant
Dim Licensee_Id As Variant
Dim Licensee As Variant
Dim Agreement As Variant
Dim Agreement_Currency As Variant
Dim Promotional_Rate As Variant
Dim Agreement_Period_From As Date
Dim Agreement_Period_To As Date
Dim Template_Inclusion_Cutoff As Date
Dim Statement_Inclusion_Cutoff As Date
Dim Comment As Variant
    
Dim i As Integer

    Licensor = ExcelSheet.Cells(3, 2).Value

For i = 9 To 12
        Period = ExcelSheet.Cells(i, 1).Value
        Licensee_Id = ExcelSheet.Cells(i, 2).Value
        Licensee = ExcelSheet.Cells(i, 3).Value
        Agreement = ExcelSheet.Cells(i, 4).Value
        Agreement_Currency = ExcelSheet.Cells(i, 5).Value
        Promotional_Rate = ExcelSheet.Cells(i, 6).Value
        Agreement_Period_From = ExcelSheet.Cells(i, 7).Value
        Agreement_Period_To = ExcelSheet.Cells(i, 8).Value
        Template_Inclusion_Cutoff = ExcelSheet.Cells(i, 9).Value
        Statement_Inclusion_Cutoff = ExcelSheet.Cells(i, 10).Value
        Comment = ExcelSheet.Cells(i, 11).Value
    
Dim tablename As String
Dim variables As String
Dim values As String

    tablename = "tblAgreement_Terms"
    variables = "([Licensor], [Period], [Licensee_Id], [Licensee], [Agreement], [Agreement_Currency], [Promotional_Rate], [Agreement_Period_From], [Agreement_Period_To], [Template_Inclusion_Cutoff], [Statement_Inclusion_Cutoff], [Comment])"

    values = "(" & "'" & Licensor & "', " & "'" & Period & "'," & "'" & Licensee_Id & "'," & "'" & Licensee & "'," & "'" & Agreement & "'," & "'" & Agreement_Currency & "'," & "'" & Promotional_Rate & "'," & "'" & Agreement_Period_From & "'," & "'" & Agreement_Period_To & "'," & "'" & Template_Inclusion_Cutoff & "'," & "'" & Statement_Inclusion_Cutoff & "'," & "'" & Comment & "'" & ")"
    
'    MsgBox values
'    values = "( 'Disney' )"
'    MsgBox values
'    AppendToTable tablename, variables, values
    
DoCmd.RunSQL "INSERT INTO " & tablename & variables & "VALUES" & values & ";"
    
    
Next i

ExcelBook.Close savechanges:=False 'close without changes
Set ExcelSheet = Nothing
Set ExcelBook = Nothing
Set ExcelApp = Nothing

End Sub

Open in new window

0
Comment
Question by:Fritz Paul
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 27

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 40025599
There are several problems with your code:
1. all variables should be dimensioned in the beginning of the code, since that is the way the compiler works.
2. you are using a mish-mash of early and late binding, choose one and stick to it.

As far as your issue is concerned, I don't see a problem with the code, it seems to be closing excel. Insert a breakpoint at line 73, is Excel still open at that point?
0
 

Author Closing Comment

by:Fritz Paul
ID: 40025626
Thanks, I made the suggested changes and it works.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question