PC Review


Reply
Thread Tools Rate Thread

Run-Time Error '440' Method 'Run' of object '_Application' failed

 
 
New Member
Join Date: Feb 2011
Posts: 1
 
      27th Feb 2011
Hi, I have a VB app that opens an Access DB to run a macro to generate data in a table and then it call an excel macro which also accesses the same DB to get the data and put it in to two pivot tables on two different sheets. The problem I have is when the VB app calls the run method for the excel macro I get the error mentioned int title Run-Time Error '440' Method 'Run' of object '_Application' failed. The code for the VB app is below and the code for the Excel Macro is below that: I am not sure but is it possible that I need to close the connection to the DB in excel macro before making another connection for the second pivot table? I should also mention that this worked before I added the second pivot table.

I am desperate for a solution any help would be greatly appreciated.

VB APP CODE BEGIN

Private Sub Form_Load()
'Create the Access Data
Dim ObjAccess As Access.Application
Dim filesys
Set filesys = CreateObject("Scripting.FileSystemObject")
If filesys.FileExists("C:\Projects\salesqtybymonth\salesqtybymonth.mdb") Then filesys.Deletefile "C:\Projects\salesqtybymonth\salesqtybymonth.mdb"
If filesys.FileExists("C:\Projects\salesqtybymonth\salesqtybymonthmaster.mdb") Then filesys.copyfile "C:\Projects\salesqtybymonth\salesqtybymonthmaster.mdb", "C:\Projects\salesqtybymonth\salesqtybymonth.mdb"
If filesys.FileExists("C:\Projects\salesqtybymonth\salesqtybymonth.xls") Then filesys.Deletefile "C:\Projects\salesqtybymonth\salesqtybymonth.xls"
Set filesys = Nothing
Set ObjAccess = New Access.Application
ObjAccess.Visible = False
strMDbFile = "C:\Projects\salesqtybymonth\salesqtybymonth.mdb"
ObjAccess.OpenCurrentDatabase "C:\Projects\salesqtybymonth\salesqtybymonth.mdb"
ObjAccess.DoCmd.RunMacro ("RUN_SALES_QTY_QUERY")
ObjAccess.Quit
'Destroy the object
Set ObjAccess = Nothing
'MAKE EXCEL SPREADSHEET
'Create the Excel Object
Dim ObjExcel As Excel.Application
Set ObjExcel = New Excel.Application
ObjExcel.DisplayAlerts = False
ObjExcel.Visible = False
strXlsFile = "C:\Projects\salesqtybymonth\salesqtybymonthmastertemplate.xlt"
Set ObjWorkbook = ObjExcel.Workbooks.Open(strXlsFile)
Set objWorksheet = ObjWorkbook.Worksheets(1)
ObjExcel.Run ("salesqtybymonth")
ObjExcel.Quit
Set ObjExcel = Nothing

VB APP CODE END

EXCEL MACRO BEGIN
Sub salesqtybymonth()

'Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "YourSheet"
Sheets("Sheet1").Select
ActiveSheet.Name = "SalesByQuantity"

With ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal)
.Connection = Array(Array( _
"ODBC;DSN=MS Access Database;DBQ=C:\projects\salesqtybymonth\salesqtybymonth.mdb;DefaultDir=C:\projects\salesqtybymonth;DriverId=25;FI" _
), Array("L=MS Access;MaxBufferSize=2048;PageTimeout=5;"))
.CommandType = xlCmdSql
.CommandText = Array( _
"SELECT tblGetallWithPeriodName.ITM_CD, tblGetallWithPeriodName.SO_STORE_CD, tblGetallWithPeriodName.SumOfQTY, tblGetallWithPeriodName.VE_CD, tblGetallWithPeriodName.VSN, tblGetallWithPeriodName.DESCRI" _
, _
"PTION, tblGetallWithPeriodName.MISC_INV_PER_DES, tblGetallWithPeriodName.PERIOD" & Chr(13) & "" & Chr(10) & "FROM `C:\projects\salesqtybymonth\salesqtybymonth`.tblGetallWithPeriodName tblGetallWithPeriodName" & Chr(13) & "" & Chr(10) & "ORDER BY tblGetallWi" _
, "thPeriodName.VE_CD, tblGetallWithPeriodName.VSN")
.CreatePivotTable TableDestination:="SalesByQuantity!R4C1", TableName:= _
"PivotTable2", DefaultVersion:=xlPivotTableVersion10
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("VE_CD")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("VSN")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("DESCRIPTION")
.Orientation = xlRowField
.Position = 3
End With
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable2").PivotFields("SumOfQTY"), "Sum of SumOfQTY", xlSum
With ActiveSheet.PivotTables("PivotTable2").PivotFields("PERIOD")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("SO_STORE_CD")
.Orientation = xlPageField
.Position = 1
End With
Range("A4").Select
ActiveSheet.PivotTables("PivotTable2").PivotFields("VE_CD").Subtotals = Array( _
False, False, False, False, False, False, False, False, False, False, False, False)
Range("B4").Select
ActiveSheet.PivotTables("PivotTable2").PivotFields("VSN").Subtotals = Array( _
False, False, False, False, False, False, False, False, False, False, False, False)
Range("C4").Select
ActiveSheet.PivotTables("PivotTable2").PivotFields("DESCRIPTION").Subtotals = _
Array(False, False, False, False, False, False, False, False, False, False, False, False)
'ActiveSheet.Copy
Application.CommandBars("PivotTable").Visible = False
ActiveWorkbook.ShowPivotTableFieldList = False
Range("A1").Select
ActiveCell.FormulaR1C1 = "Past Year Sales Qty by Month as of " & Now()
'Dim WS As Worksheet
'Set WS = Sheets.Add
'Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "MySheet"
'Sheets.Add
Sheets("Sheet2").Select
ActiveSheet.Name = "SalesByAmount"
Range("A4").Select
With ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal)
.Connection = Array(Array( _
"ODBC;DSN=MS Access Database;DBQ=C:\projects\salesqtybymonth\salesqtybymonth.mdb;DefaultDir=C:\projects\salesqtybymonth;DriverId=25;FI" _
), Array("L=MS Access;MaxBufferSize=2048;PageTimeout=5;"))
.CommandType = xlCmdSql
.CommandText = Array( _
"SELECT tblGetallWithPeriodNameAmt.ITM_CD, tblGetallWithPeriodNameAmt.SO_STORE_CD, tblGetallWithPeriodNameAmt.SumOfAMT, tblGetallWithPeriodNameAmt.VE_CD, tblGetallWithPeriodNameAmt.VSN, tblGetallWithPeriodNameAmt.DESCRI" _
, _
"PTION, tblGetallWithPeriodNameAmt.MISC_INV_PER_DES, tblGetallWithPeriodNameAmt.PERIOD" & Chr(13) & "" & Chr(10) & "FROM `C:\projects\salesqtybymonth\salesqtybymonth`.tblGetallWithPeriodNameAmt tblGetallWithPeriodNameAmt" & Chr(13) & "" & Chr(10) & "ORDER BY tblGetallWi" _
, "thPeriodNameAmt.VE_CD, tblGetallWithPeriodNameAmt.VSN")
.CreatePivotTable TableDestination:="SalesByAmount!R4C1", TableName:= _
"PivotTable3", DefaultVersion:=xlPivotTableVersion10
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("VE_CD")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("VSN")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("DESCRIPTION")
.Orientation = xlRowField
.Position = 3
End With
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("SumOfAMT"), "Sum of SumOfAMT", xlSum
With ActiveSheet.PivotTables("PivotTable3").PivotFields("PERIOD")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("SO_STORE_CD")
.Orientation = xlPageField
.Position = 1
End With
Range("A4").Select
ActiveSheet.PivotTables("PivotTable3").PivotFields("VE_CD").Subtotals = Array( _
False, False, False, False, False, False, False, False, False, False, False, False)
Range("B4").Select
ActiveSheet.PivotTables("PivotTable3").PivotFields("VSN").Subtotals = Array( _
False, False, False, False, False, False, False, False, False, False, False, False)
Range("C4").Select
ActiveSheet.PivotTables("PivotTable3").PivotFields("DESCRIPTION").Subtotals = _
Array(False, False, False, False, False, False, False, False, False, False, False, False)
'ActiveSheet.Copy
Application.CommandBars("PivotTable").Visible = False
ActiveWorkbook.ShowPivotTableFieldList = False
Range("A1").Select
ActiveCell.FormulaR1C1 = "Past Year Sales Amount by Month as of " & Now()

ActiveWorkbook.SaveAs Filename:="C:\projects\salesqtybymonth\salesqtybymonth.xls", FileFormat:= _
xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
ActiveWorkbook.Close
End Sub

EXCEL MACRO END
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Method "Method 'Open' of object 'Workbooks' failed priustoric@yahoo.com Microsoft Excel Programming 2 2nd Jun 2010 05:28 PM
Http Post Error: A connection attempt failed because the connectedparty did not properly respond after a period of time, or establishedconnection failed because connected host has failed to respond. Deepu Microsoft Dot NET Framework 1 9th Jul 2009 05:09 PM
Error: Method 'OpenTextFile' of object 'IFileSystem' failed. Learner Microsoft Excel Discussion 3 23rd Feb 2005 10:22 PM
Error: Method 'OpenTextFile' of object 'IFileSystem' failed. tomjcb@gmail.com Microsoft Excel Discussion 0 23rd Feb 2005 04:14 PM
"Run-time error '1004'" Method 'Range' of object '_global' failed. haisat Microsoft Excel Programming 0 20th Oct 2003 12:13 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:30 PM.