VenuChakkoth
asked on
Help with docmd.transferspreadsheet for importing a dynamic range from excel to a temporary table in Ms Access 2007
Hi Experts,
I need your expertise on a project that I am currently working. I need to import a dynamic range from multiple spreadsheets into an temporary table in access for further processing. I have defined the ranges and the transferspreadsheet for import is working fine as well. However, I am facing the problem of an additional Excel instance getting created while the transferspreadsheet command is executed.
I have the excel file open, it defines the ranges based on key words and it executes the transfer spreadsheet command, post which I close the workbook and quit the excel instance... However, the file is still open.
Any thoughts or any better ways of getting this done? I need help in achieving this task without creating a new instance of excel or I should be able to close all instances of excel opened by my code at the end.
I am adding extracts of the code that I am using.
If xlApp Is Nothing Then
Set xlApp = CreateObject("Excel.Applic ation")
xlApp.Visible = False
xlOpen = True
xlApp.ScreenUpdating = False
xlApp.DisplayStatusBar = False
xlApp.EnableEvents = False
End If
xBook = Split(![txtXmlPath], "\", , vbTextCompare)
xBook = xBook(UBound(xBook))
If IsFileOpen(.txtXmlPath) Then
xlApp.Workbooks(xBook).Clo se False
xlApp.Workbooks.Open .txtXmlPath, ReadOnly:=False, Notify:=False
Else
xlApp.Workbooks.Open .txtXmlPath, ReadOnly:=False, Notify:=False
End If
If IsNumeric(.Check0) Then
Set xSht = xlApp.Workbooks(xBook).Wor ksheets("9 20-" & Trim(.Check0))
Else
Set xSht = xlApp.Workbooks(xBook).Wor ksheets(CS tr(.Check0 ))
End If
Then I define the range
If IsTableExists("tblDeals") Then
DoCmd.Close acTable, "tblDeals", acSaveNo
DoCmd.DeleteObject acTable, "tblDeals"
End If
If IsTableExists("tbloBanners ") Then
DoCmd.Close acTable, "tbloBanners", acSaveNo
DoCmd.DeleteObject acTable, "tbloBanners"
End If
If Not upcRge Is Nothing Then DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xm l, "tblDeals", [Forms]![frmInput]![txtXml Path], True, xSht.Name & "!" & Replace(upcRge.Address, "$", vbNullString, , , vbTextCompare)
If Not obRge Is Nothing Then DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xm l, "tbloBanners", [Forms]![frmInput]![txtXml Path], True, xSht.Name & "!" & Replace(obRge.Address, "$", vbNullString, , , vbTextCompare)
Thank you..
I need your expertise on a project that I am currently working. I need to import a dynamic range from multiple spreadsheets into an temporary table in access for further processing. I have defined the ranges and the transferspreadsheet for import is working fine as well. However, I am facing the problem of an additional Excel instance getting created while the transferspreadsheet command is executed.
I have the excel file open, it defines the ranges based on key words and it executes the transfer spreadsheet command, post which I close the workbook and quit the excel instance... However, the file is still open.
Any thoughts or any better ways of getting this done? I need help in achieving this task without creating a new instance of excel or I should be able to close all instances of excel opened by my code at the end.
I am adding extracts of the code that I am using.
If xlApp Is Nothing Then
Set xlApp = CreateObject("Excel.Applic
xlApp.Visible = False
xlOpen = True
xlApp.ScreenUpdating = False
xlApp.DisplayStatusBar = False
xlApp.EnableEvents = False
End If
xBook = Split(![txtXmlPath], "\", , vbTextCompare)
xBook = xBook(UBound(xBook))
If IsFileOpen(.txtXmlPath) Then
xlApp.Workbooks(xBook).Clo
xlApp.Workbooks.Open .txtXmlPath, ReadOnly:=False, Notify:=False
Else
xlApp.Workbooks.Open .txtXmlPath, ReadOnly:=False, Notify:=False
End If
If IsNumeric(.Check0) Then
Set xSht = xlApp.Workbooks(xBook).Wor
Else
Set xSht = xlApp.Workbooks(xBook).Wor
End If
Then I define the range
If IsTableExists("tblDeals") Then
DoCmd.Close acTable, "tblDeals", acSaveNo
DoCmd.DeleteObject acTable, "tblDeals"
End If
If IsTableExists("tbloBanners
DoCmd.Close acTable, "tbloBanners", acSaveNo
DoCmd.DeleteObject acTable, "tbloBanners"
End If
If Not upcRge Is Nothing Then DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xm
If Not obRge Is Nothing Then DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xm
Thank you..
ASKER
Thank you Donald.
We have a problem with closing all workbooks since we need to have other workbooks open while we import the data from excel to access.
Is there a way to kill just the instance of excel that is created while the docmd.transferspreadsheet (import) is done?
We have a problem with closing all workbooks since we need to have other workbooks open while we import the data from excel to access.
Is there a way to kill just the instance of excel that is created while the docmd.transferspreadsheet (import) is done?
Are you certain that you are not creating any 'phantom' Excel instance at any point by using an un-prefixed Excel object?
I see you have upcRge and obRge in your code which appear to be Excel objects but you haven't shown how they are declared and set..
I see you have upcRge and obRge in your code which appear to be Excel objects but you haven't shown how they are declared and set..
I close out all open excel objects I shut down sheets and workbooks.
Once you have closed out of access do a CTR-ALT-DEL and loog at the running tasks. If there is a EXCEL.EXE running then you still have an open object.
Do a search for every instance that you open one.
It can get tricky and I know you have just put in sample code - not all the code- But peter is right about the upcRge and objRge.
Once you have closed out of access do a CTR-ALT-DEL and loog at the running tasks. If there is a EXCEL.EXE running then you still have an open object.
Do a search for every instance that you open one.
It can get tricky and I know you have just put in sample code - not all the code- But peter is right about the upcRge and objRge.
ASKER
Hi Donald and Peter,
Sorry for the delayed response. I was traveling.
I will explain what I'm trying to do. There is one instance of Excel that is open. The upcRge and obRge are all part of that instance. However when I do the transferspreadsheet command a new instance is getting created that's causing the file locked for editing error notification and this is what I'm trying to fix. I hope I didn't confuse you...
Sorry for the delayed response. I was traveling.
I will explain what I'm trying to do. There is one instance of Excel that is open. The upcRge and obRge are all part of that instance. However when I do the transferspreadsheet command a new instance is getting created that's causing the file locked for editing error notification and this is what I'm trying to fix. I hope I didn't confuse you...
So when you run the transfer you are accessing the xls field defined/named/stored in [Forms]![frmInput]![txtXml Path].
Do you ever close the referenced Excell sheet/workbook?
Don
Do you ever close the referenced Excell sheet/workbook?
Don
ASKER
Hi Don,
No, the referenced workbook is closed only after the transferspreadsheet is fired.
No, the referenced workbook is closed only after the transferspreadsheet is fired.
What I do is also close the workbook after the transfer
e.g code in my access app
For I = 1 To (intCount)
Set objSheet = objWkBook.Sheets(I)
strWSname = objSheet.Name
'Debug.Print i; intCount; strWSname 'use this for testing on the immediate window
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
"" & tblName & strWSname & "", strFileName, True, "" & strWSname & "!"
Next I
objWkBook.Application.Quit
Set objWkBook = Nothing
Set objSheet = Nothing
ASKER
Hi Don,
I am following the same procedure to close the workbook and quit excel after running the docmd.transferspreadsheet.
However, what is happening is that the transferspreadsheet command is opening another instance of Excel and since its opening the same workbook again, I am getting the "File locked for editing" error notification. So., when the code closes the workbook and quits excel, I still have an open instance of excel and I cannot follow the code to kill all existing excel sessions because I have users working on other workbooks at the same time when the code is run.
Oh! one point that striked me now from your code is to try killing the excel instance using the objWkBook.application.quit method. I will try this and let you know.
Thank you...
I am following the same procedure to close the workbook and quit excel after running the docmd.transferspreadsheet.
However, what is happening is that the transferspreadsheet command is opening another instance of Excel and since its opening the same workbook again, I am getting the "File locked for editing" error notification. So., when the code closes the workbook and quits excel, I still have an open instance of excel and I cannot follow the code to kill all existing excel sessions because I have users working on other workbooks at the same time when the code is run.
Oh! one point that striked me now from your code is to try killing the excel instance using the objWkBook.application.quit
Thank you...
ASKER
Hi,
I tried that as well. Even thought I have called application.quit after doing the transfer, I have the same workbook open in Read Only Mode and I get the notification that the file is available for editing. Choose Read Write to open it for editing...
Thank you
I tried that as well. Even thought I have called application.quit after doing the transfer, I have the same workbook open in Read Only Mode and I get the notification that the file is available for editing. Choose Read Write to open it for editing...
Thank you
ASKER
Hi All,
Just to add on to my previous comments - My code works just fine if there are no other excel workbooks open at the time of running the code.
However, if there are other excel workbooks open, I will still have the workbook that is opened by the code, open as a read only version and I get the notification that the file is locked for editing,
Just to add on to my previous comments - My code works just fine if there are no other excel workbooks open at the time of running the code.
However, if there are other excel workbooks open, I will still have the workbook that is opened by the code, open as a read only version and I get the notification that the file is locked for editing,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I've requested that this question be deleted for the following reason:
I have figured out a solution to my problem. This question may be closed. Thank you
I have figured out a solution to my problem. This question may be closed. Thank you
I would leave it as open so other people will have the answer. JUst ask if it can be made as comment and not use your points
ASKER
Thanks Don. I am not deleting the post. Please consider this as my comment and donot use my points.
ASKER
I figured out a solution to my problem. Refer to Comment ID: 36938332
modCloseExcelWorkbooks
which has the code which I call in my application to close all excel workbooks etc.
Once I am done importing a worksheet I call the routine to make sure all open workbooks are closed.
IT sounds like you may have one open and it is causing issues.
Open in new window