Hi,
I wonder is someone can help me?
Basically I have some VBA which exports a recordset to Excel and then formats the cells - the Excel application then stays open so the user can view the data. This all works fine until I come to run the code again, which susequently doesnt always run after closing down the Excel App.
It seems that the original process hasn't finished and I have to go into Task Manager every time to close it within 'Processes'.
I am using Access 2000 and Excel v9
Is there a sure way to kill the process each time? I have added my code below: -
DoCmd.Hourglass True
On Error GoTo errhndl1
strQueryPt1 = Me.cboQuery.Column(1) & Me.cboQuery.Column(2) & Me.cboQuery.Column(3)
strQueryPt2 = Me.cboFieldName.Column(2)
strQueryPt3 = Me.cboFieldName.Column(0)
strQueryPt4 = Me.cboFieldName.Column(3)
strQueryPt5 = Me.cboSearchType.Column(1)
datatype = Me.txtValue.Value
If strQueryPt4 = "text" Then
strQueryPt6 = "'" & datatype & "'"
Else
strQueryPt6 = "#" & MakeUsDate(datatype) & "#"
End If
strQueryAll = strQueryPt1 & " WHERE [" & strQueryPt2 & "]![" & strQueryPt3 & "] " & strQueryPt5 & " " & strQueryPt6
'''''export query results to new excel file
Set objXL = New Excel.Application
Set objWkb = Excel.Workbooks.Add 'Create New Excel Workboox
Set db = CurrentDb
Set rst = db.OpenRecordset(strQueryAll)
With objXL
Set objSht = objWkb.Worksheets("Sheet1")
objSht.Select
objSht.Cells.Select
excelborders
iNumCols = rst.Fields.Count 'Add recordset field headings to worksheet
For i = 1 To iNumCols
objSht.Cells(1, i).Value = rst.Fields(i - 1).Name
Next
objSht.Range("A2").CopyFromRecordset rst 'add recordset
End With
rst.Close
c = 1
Do Until Cells(1, c) = ""
excelheaders
c = c + 1
Loop
objSht.Cells.Select
objSht.Cells.EntireColumn.AutoFit
With objXL
.Visible = True
End With
exitsub:
Set objSht = Nothing
Set objWkb = Nothing
Set objXL = Nothing
Set rst = Nothing
Set db = Nothing
DoCmd.Hourglass False
Exit Sub
errhndl1:
MsgBox "An error has occurred, please contact the database administrator", vbExclamation
Resume exitsub
Ps I don't want the file saved in the VBA, I want the user to have a choice of either saving the file or quitting without saving it.
I wonder is someone can help me?
Basically I have some VBA which exports a recordset to Excel and then formats the cells - the Excel application then stays open so the user can view the data. This all works fine until I come to run the code again, which susequently doesnt always run after closing down the Excel App.
It seems that the original process hasn't finished and I have to go into Task Manager every time to close it within 'Processes'.
I am using Access 2000 and Excel v9
Is there a sure way to kill the process each time? I have added my code below: -
DoCmd.Hourglass True
On Error GoTo errhndl1
strQueryPt1 = Me.cboQuery.Column(1) & Me.cboQuery.Column(2) & Me.cboQuery.Column(3)
strQueryPt2 = Me.cboFieldName.Column(2)
strQueryPt3 = Me.cboFieldName.Column(0)
strQueryPt4 = Me.cboFieldName.Column(3)
strQueryPt5 = Me.cboSearchType.Column(1)
datatype = Me.txtValue.Value
If strQueryPt4 = "text" Then
strQueryPt6 = "'" & datatype & "'"
Else
strQueryPt6 = "#" & MakeUsDate(datatype) & "#"
End If
strQueryAll = strQueryPt1 & " WHERE [" & strQueryPt2 & "]![" & strQueryPt3 & "] " & strQueryPt5 & " " & strQueryPt6
'''''export query results to new excel file
Set objXL = New Excel.Application
Set objWkb = Excel.Workbooks.Add 'Create New Excel Workboox
Set db = CurrentDb
Set rst = db.OpenRecordset(strQueryAll)
With objXL
Set objSht = objWkb.Worksheets("Sheet1")
objSht.Select
objSht.Cells.Select
excelborders
iNumCols = rst.Fields.Count 'Add recordset field headings to worksheet
For i = 1 To iNumCols
objSht.Cells(1, i).Value = rst.Fields(i - 1).Name
Next
objSht.Range("A2").CopyFromRecordset rst 'add recordset
End With
rst.Close
c = 1
Do Until Cells(1, c) = ""
excelheaders
c = c + 1
Loop
objSht.Cells.Select
objSht.Cells.EntireColumn.AutoFit
With objXL
.Visible = True
End With
exitsub:
Set objSht = Nothing
Set objWkb = Nothing
Set objXL = Nothing
Set rst = Nothing
Set db = Nothing
DoCmd.Hourglass False
Exit Sub
errhndl1:
MsgBox "An error has occurred, please contact the database administrator", vbExclamation
Resume exitsub
Ps I don't want the file saved in the VBA, I want the user to have a choice of either saving the file or quitting without saving it.