Hi all,
I am doing running some queries in ms access and output the results to ms excel spreadsheet. There are several subroutines that I run when I click on a button on the form in ms access to trigger the process.
Occassionally, when I run my the routines, ms access runs for some and left out the rest. Eg: I run a total of 8 routines in a button. Access would run 5 out of the 8 routines and hangs there.
Any reason for the bizzare behaviour?
I have attached the following codes that I used for reference.
(This is the first routine I run to open the spreadsheet)
(This is the second routine I run to run on the spreadsheet as activesheet)
Any ideas on why such behaviour is happening? Thanks for the advices.
I am doing running some queries in ms access and output the results to ms excel spreadsheet. There are several subroutines that I run when I click on a button on the form in ms access to trigger the process.
Occassionally, when I run my the routines, ms access runs for some and left out the rest. Eg: I run a total of 8 routines in a button. Access would run 5 out of the 8 routines and hangs there.
Any reason for the bizzare behaviour?
I have attached the following codes that I used for reference.
(This is the first routine I run to open the spreadsheet)
Sub exportOBVolAgeingDataFirst(sDate As Date, rng As String, stat As String, ag As String)
Dim adoConn As ADODB.Connection
Dim adors As ADODB.Recordset
Dim sql As String
sql = "TRANSFORM sum(vol)" _
& " SELECT AgeGroup" _
& " FROM popGroup" _
& " WHERE (weekDate Between (" & "#" & (sDate) & "#" & ") And Format(Date(),'m/dd/yyyy') AND AgeGroup= " & "'" & ag & "'" & " )" _
& " GROUP BY AgeGroup" _
& "PIVOT DateAdd('ww',DateDiff('ww',0,weekDate),0)-1"
filename = "\\pgfiles\_test$\Desktop\db1.mdb"
Call getConn(adoConn, adors, sql, filename, "", "")
Dim ExcelApp As Object
Dim ExcelSht As Excel.Worksheet
Dim ExcelWkb As Excel.Workbook
Dim counter As Integer
Dim tempPointer As Integer
On Error Resume Next
Set ExcelApp = CreateObject("Excel.Application")
DoCmd.SetWarnings False
ExcelApp.Application.DisplayAlerts = False
ExcelApp.Application.ScreenUpdating = False
ExcelApp.Visible = True
'Set ExcelWkb = ActiveWorkbook
Set ExcelWkb = ExcelApp.Workbooks.Open("\\pgfiles\_test$\Desktop\Dashboard.xls", False, False)
With ExcelWkb
.Sheets("popSht").Select
With .ActiveSheet
'tempPointer = 4
.Range(rng).CopyFromRecordset adors
'MsgBox adors.Fields.Count
'--------Header Data-----------------
For counter = 1 To adors.Fields.Count
.Cells(2, counter) = adors.Fields(counter - 1).Name
'tempPointer = tempPointer + 1
Next
'------------------------------------
End With
End With
ExcelApp.Application.ScreenUpdating = True
'ExcelSht.Range("A1").CopyFromRecordset adors
adors.Close
adoConn.Close
Set adors = Nothing
Set adoConn = Nothing
Set ExcelSht = Nothing
Set ExcelWkb = Nothing
Set ExcelApp = Nothing
'ExcelApp.Quit
End Sub
(This is the second routine I run to run on the spreadsheet as activesheet)
Sub exportOBVolAgeingDataSecond(sDate As Date, rng As String, stat As String, ag As String)
Dim adoConn As ADODB.Connection
Dim adors As ADODB.Recordset
Dim sql As String
sql = "TRANSFORM sum(vol)" _
& " SELECT AgeGroup" _
& " FROM popGroup" _
& " WHERE (weekDate Between (" & "#" & (sDate) & "#" & ") And Format(Date(),'m/dd/yyyy') AND AgeGroup= " & "'" & ag & "'" & " )" _
& " GROUP BY AgeGroup" _
& "PIVOT DateAdd('ww',DateDiff('ww',0,weekDate),0)-1"
filename = "\\pgfiles\_test$\Desktop\db1.mdb"
Call getConn(adoConn, adors, sql, filename, "", "")
Dim ExcelApp As Object
Dim ExcelSht As Excel.Worksheet
Dim ExcelWkb As Excel.Workbook
Dim counter As Integer
Dim tempPointer As Integer
On Error Resume Next
Set ExcelApp = GetObject(, Excel.Application)
'DoCmd.SetWarnings False
'ExcelApp.Application.DisplayAlerts = False
ExcelApp.Application.ScreenUpdating = False
'ExcelApp.Visible = True
Set ExcelWkb = ActiveWorkbook
With ExcelWkb
.Sheets("popSht").Select
With .ActiveSheet
'tempPointer = 4
.Range(rng).CopyFromRecordset adors
'MsgBox adors.Fields.Count
'--------Header Data-----------------
For counter = 1 To adors.Fields.Count
.Cells(2, counter) = adors.Fields(counter - 1).Name
'tempPointer = tempPointer + 1
Next
'------------------------------------
End With
End With
ExcelApp.Application.ScreenUpdating = True
'ExcelSht.Range("A1").CopyFromRecordset adors
adors.Close
adoConn.Close
Set adors = Nothing
Set adoConn = Nothing
Set ExcelSht = Nothing
Set ExcelWkb = Nothing
Set ExcelApp = Nothing
'ExcelApp.Quit
End Sub
Any ideas on why such behaviour is happening? Thanks for the advices.