Sargad_Strut
New Member
- Joined
- Mar 28, 2014
- Messages
- 44
Hello friendly helpers,
I have run into a problem when looping through Access queries via VBA. I have multiple levels that I am looping through; the first table (query) consists of unique project leaders, I then loop through these to find associated projects, after which i loop through the projects to ultimately find details about items that each project leader should be aware of. That's the quick backdrop. Now to the issue.
The workbook has been working great for years, but recently, after adding more and more people and projects, the macro crashes. First I get a dialogue "Select data source", where MS Access Database is one of the options. Regardless of action (Ok or Cancel), I am presented with the following error message:
Run-time error '-2147217842 (80040e4e)':
[Microsoft][ODBC Microsoft Access Driver] Too many client tasks.
The debugger points to "ActiveWorkbook.Connections("Query from MS Access Database3").Refresh" (see full code below).
I put a counter in the code and it turns out it crashes on the 64th refresh, which does not sound like a coincidence. Here's one of the quieries in the loop:
Can I close the connection somehow to avoid the issue - or do you have any other suggestions?
Thanks in advance.
I have run into a problem when looping through Access queries via VBA. I have multiple levels that I am looping through; the first table (query) consists of unique project leaders, I then loop through these to find associated projects, after which i loop through the projects to ultimately find details about items that each project leader should be aware of. That's the quick backdrop. Now to the issue.
The workbook has been working great for years, but recently, after adding more and more people and projects, the macro crashes. First I get a dialogue "Select data source", where MS Access Database is one of the options. Regardless of action (Ok or Cancel), I am presented with the following error message:
Run-time error '-2147217842 (80040e4e)':
[Microsoft][ODBC Microsoft Access Driver] Too many client tasks.
The debugger points to "ActiveWorkbook.Connections("Query from MS Access Database3").Refresh" (see full code below).
I put a counter in the code and it turns out it crashes on the 64th refresh, which does not sound like a coincidence. Here's one of the quieries in the loop:
VBA Code:
Sub refreshActiveProjects_projektLedare2(projLeader As Integer)
folderPath = Application.ThisWorkbook.Path
If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
connString = folderPath & "Master.accdb"
With ActiveWorkbook.Connections("Query from MS Access Database3"). _
ODBCConnection
.BackgroundQuery = False
.CommandText = Array( _
"SELECT Projektledare2, ProjectName" & Chr(13) & "" & Chr(10) & "FROM Projects" & Chr(13) & "" & Chr(10) & "WHERE (Active=True) AND (Projektledare2=" & projLeader & ")" & Chr(13) & "" & Chr(10) & "ORDER BY ProjectName" _
)
.CommandType = xlCmdSql
.Connection = Array(Array( _
"ODBC;DSN=MS Access Database;DBQ=" & connString & ";DriverId=25;FIL=MS Access;MaxBufferSize=2" _
), Array("048;PageTimeout=5;"))
.RefreshOnFileOpen = False
.SavePassword = False
.SourceConnectionFile = ""
.SourceDataFile = ""
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
.AlwaysUseConnectionFile = False
End With
With ActiveWorkbook.Connections("Query from MS Access Database3")
.Name = "Query from MS Access Database3"
.Description = ""
End With
ActiveWorkbook.Connections("Query from MS Access Database3").Refresh
End Sub
Can I close the connection somehow to avoid the issue - or do you have any other suggestions?
Thanks in advance.