Hi
A problem that's only started happening on my spreadsheet in the last couple of months.
I've got some VBA that works fine if I sit there and watch it.....it does everything it should. It finishes all of its loops and Excel stays open as it should.
Whereas if I don't sit there watching for an hour and instead I [Ctrl][Alt][Delete] to lock the computer and go away....I come back after 30 minutes to check on it and see it's still running ok and then lock the computer again for another half hour. I eventually come back and find that it finished all its loops but Excel closed itself without saving.
NB: How do I know it did all of its loops? I'm confident because I can see in the properties of the Access file that it calls on, what time the Access file was last closed.
I've done a [Ctrl][F] inside the VBA and searched within Current Project for "Workbook.Close" or "Application.Quit" and it certainly isn't there - and has never been there. Nor is there anything telling it not to display alerts. So it ought to be asking are you sure you want to quit without saving changes.
Any idea why this is happening or how I could catch when it's about to happen so that I can see what's causing it?
I doubt the opening and closing of a database connection is relevant. But in case it is....Here's that code:
Thanks
A problem that's only started happening on my spreadsheet in the last couple of months.
I've got some VBA that works fine if I sit there and watch it.....it does everything it should. It finishes all of its loops and Excel stays open as it should.
Whereas if I don't sit there watching for an hour and instead I [Ctrl][Alt][Delete] to lock the computer and go away....I come back after 30 minutes to check on it and see it's still running ok and then lock the computer again for another half hour. I eventually come back and find that it finished all its loops but Excel closed itself without saving.
NB: How do I know it did all of its loops? I'm confident because I can see in the properties of the Access file that it calls on, what time the Access file was last closed.
I've done a [Ctrl][F] inside the VBA and searched within Current Project for "Workbook.Close" or "Application.Quit" and it certainly isn't there - and has never been there. Nor is there anything telling it not to display alerts. So it ought to be asking are you sure you want to quit without saving changes.
Any idea why this is happening or how I could catch when it's about to happen so that I can see what's causing it?
I doubt the opening and closing of a database connection is relevant. But in case it is....Here's that code:
Code:
Dim Conn as ADODB.Connection
Dim NumberofRecords as Long
Sub ImportData()
Dim AccessFile as string
Dim i as Byte
Dim QueryTableToImport as String
Dim QueryOutputRange as String
Dim ImportHeadingsTRUEFALSE as Boolean
AccessFile = Range("DBPath")
Set Conn = New ADODB.Connection
Conn.Open "Provider = Microsoft.ACE.OLDDB.12.0;Data Source=" & AccessFile
Conn.Execute "DELETE FROM Criteria"
Conn.Execute "INSERT INTO Criteria ([Reference Number]) values(" _
& "'" & Range("RefNo") & "')"
Do Until Len(Range("DataToImport").cells(i,1)) = 0
QueryToImport = Range("DataToImport").cells(i,1)
QueryOutputRange = Range("DataToImport").cells(i,2)
ImportHeadingsTRUEFALSE = Range(DataToImport").cells(i,3)
Call ImportQueryResults(QueryToImport, QueryOutputRange, ImportHeadingsTRUEFALSE)
Range("RecordCount").cells(i) = NumberofRecords
Loop
Conn.close
Set Conn = Nothing
End Sub
Sub ImportQueryResults(QueryName As String, OutputRange As String, ImportHeaders As Boolean)
Dim RS as ADODB.RecordSet
Dim strSQL as string
Set RS = New ADODB.Recordset
strSQL = "SELECT [" & QueryName & "]"
strSQL = strSQL & ".* FROM ["
strSQL = strSQL & QueryName & "];"
RS.Open strSQL, Conn, adlockReadOnly, adCmdText
NumberofRecords = RS.RecordCount
If NumberofRecords > 0 then
Range(OutputRange).CopyFromRecordset RS
End if
'Plus a bit of code for the handling of whether headers are wanted
RS.Close
Set RS = Nothing
End Sub
Thanks
Last edited: