Hi there; after searching related threads (& having a headache now), I'm ending up in starting this thread.
During running code (see below), I see my 'available RAM Memory' shrinking from > 300MB (physical 512 installed)to a very low level (< 30MB) and not freeing-up between steps. The thing that worries me is, at end of code, and after 'dbs.Close' there is no way I can free-up RAM without closing the database first.
The code is running through a lot of dynamic Qdf's (SQL's stored in tbl_SQL. Between each Qdf (intensive SQL-step), I want to free RAM in order not to exhaust the program and being able to run the next step smoothly. I want to free it similar to as when you close the database manually, reopen and proceed next qdf. Here's the code 'as is':
-----------
Private Sub cmdLoadData_Click()
Dim dbs As DAO.Database
Set dbs = CurrentDb
Dim rstLoadDB2Data, rstTskAuth, rstCountLines As DAO.Recordset
Dim ctl As Control
Set ctl = Me.IndProgress
ctl.Max = 100 '= max % of progressbar
Dim qdf As QueryDef
Dim idx As Integer
Dim strOvw, strCPU, strLst, strArea, strSqlLines, strStart, strEnd, strLaps, strSequence, strRptD2R, strRptR2D, strRptPND, strRptPth, strFilCrDtDiscr, strReport, strReportObj As String
Dim fs, F, d, n
Dim sql, sql2 As String
Dim WMI
Dim wmiWin32Objects
Dim wmiWin32Object
strLst = Now() & " § Starting Refresh Procedure "
strStart = Now()
strLaps = Now()
DoCmd.SetWarnings False
ctl = 0
ctl.Max = 100 'Set max count for the progressbar
DoCmd.RunCommand acCmdRefreshPage
Set rstTskAuth = dbs.OpenRecordset("tblTaskAuthLists", dbOpenDynaset) 'User authorized ?
rstTskAuth.FindFirst "[tskName] = '" & "DataLoad" & "' and [tskNetUser] = '" & Form_frmLogin!LogonNetUser & "'"
If rstTskAuth.NoMatch Then
MsgBox (Form_frmLogin!LogonNetUser & " not authorized")
Else 'User = authorized
strArea = rstTskAuth!tskArea '= User's BusinessArea
rstTskAuth.Close
Set rstTskAuth = Nothing
Set rstCountLines = dbs.OpenRecordset("tbl_SQL", dbOpenDynaset) 'Counting sql lines User's BusinessArea
Do Until rstCountLines.EOF
With rstCountLines
If [rstCountLines]![SqlCat] = "RMS_DataRefresh" And [rstCountLines]![SqlBusinessDivision] = strArea Then
strSqlLines = strSqlLines + 1
End If
End With
rstCountLines.MoveNext
Loop
rstCountLines.Close
Set rstCountLines = Nothing
DoCmd.RunCommand acCmdRefreshPage
With Me.lstOvw
lstOvw.RowSourceType = "Value List"
lstOvw.RowSource = strLst
End With
DoCmd.RunCommand acCmdRefreshPage
For idx = 1 To strSqlLines Step 1
Set rstLoadDB2Data = dbs.OpenRecordset("tbl_SQL", dbOpenDynaset)
DoCmd.RunCommand acCmdRefreshPage
rstLoadDB2Data.FindFirst "[SqlCat] = 'RMS_DataRefresh' and [SqlBusinessDivision] = '" & strArea & "' and int([SqlSequence]) = '" & idx & "' and int([SqlActiv]) = '-1'"
If rstLoadDB2Data.NoMatch Then
Else
Set qdf = dbs.CreateQueryDef(idx, rstLoadDB2Data!SqlString)
DoCmd.RunCommand acCmdRefreshPage
DoCmd.OpenQuery idx, acViewNormal, acEdit
Set WMI = GetObject("WinMgmts://" & ComputerName)
Set wmiWin32Objects = WMI.InstancesOf("Win32_Processor")
With wmiWin32Object
For Each wmiWin32Object In wmiWin32Objects
strCPU = .loadpercentage
Next
End With
Set WMI = Nothing
Set wmiWin32Objects = Nothing
Set wmiWin32Object = Nothing
Set fso = CreateObject("Scripting.FileSystemObject")
Set F = fso.GetFile(CurrentProject.Path & "\" & CurrentProject.name)
strLst = strLst & ";" & Now() & " § " & rstLoadDB2Data!SqlObject & rstLoadDB2Data!SqlSequence & " " & rstLoadDB2Data!SqlNaming & ", Time elapsed: " & CVDate(TimeSerial(Hour(Now()), Minute(Now()), Second(Now())) - TimeSerial(Hour(strLaps), Minute(strLaps), Second(strLaps))) & ", Curr dB-Size: " & ((F.Size / 1024000) & " MB") & ", RAM-AVA: " & ((atGetMem(2) / 1024000) & " MB") & " or " & Int((atGetMem(2) / atGetMem(1) * 1000)) / 10 & " %" & ", CPU dropped to: " & strCPU & " %"
DoCmd.RunCommand acCmdRefreshPage
With Me.lstOvw
lstOvw.RowSourceType = "Value List"
lstOvw.RowSource = strLst
End With
DoCmd.RunCommand acCmdRefreshPage
rstLoadDB2Data.Edit
rstLoadDB2Data!SqlRunDate = Now()
ctl = Int((idx / strSqlLines) * 100)
rstLoadDB2Data!SqlRunComment = "Refreshed successfully"
rstLoadDB2Data!SqlTimLoad = CVDate(TimeSerial(Hour(Now()), Minute(Now()), Second(Now())) - TimeSerial(Hour(strLaps), Minute(strLaps), Second(strLaps)))
rstLoadDB2Data.Update
DoCmd.RunCommand acCmdRefreshPage
DoCmd.RunCommand acCmdRefreshPage
DoCmd.DeleteObject acQuery, idx
End If
rstLoadDB2Data.Close
Set rstLoadDB2Data = Nothing
strLaps = Now()
Next idx
Set qdf = Nothing
Let idx = 0
dbs.Close
Set dbs = Nothing
End If
Let ctl = 0
strLst = strLst & ";-----;" & Now() & " § Update Procedure Successfull " & ";-----;Total Time elapsed: " & CVDate(TimeSerial(Hour(Now()), Minute(Now()), Second(Now())) - TimeSerial(Hour(strStart), Minute(strStart), Second(strStart)))
With Me.lstOvw
lstOvw.RowSourceType = "Value List"
lstOvw.RowSource = strLst
End With
DoCmd.SetWarnings True
ErrorHandler:
Exit Sub
End Sub
---------------
Probably overlooked something important. Can anybody suggest ? Thks in advance !!
During running code (see below), I see my 'available RAM Memory' shrinking from > 300MB (physical 512 installed)to a very low level (< 30MB) and not freeing-up between steps. The thing that worries me is, at end of code, and after 'dbs.Close' there is no way I can free-up RAM without closing the database first.
The code is running through a lot of dynamic Qdf's (SQL's stored in tbl_SQL. Between each Qdf (intensive SQL-step), I want to free RAM in order not to exhaust the program and being able to run the next step smoothly. I want to free it similar to as when you close the database manually, reopen and proceed next qdf. Here's the code 'as is':
-----------
Private Sub cmdLoadData_Click()
Dim dbs As DAO.Database
Set dbs = CurrentDb
Dim rstLoadDB2Data, rstTskAuth, rstCountLines As DAO.Recordset
Dim ctl As Control
Set ctl = Me.IndProgress
ctl.Max = 100 '= max % of progressbar
Dim qdf As QueryDef
Dim idx As Integer
Dim strOvw, strCPU, strLst, strArea, strSqlLines, strStart, strEnd, strLaps, strSequence, strRptD2R, strRptR2D, strRptPND, strRptPth, strFilCrDtDiscr, strReport, strReportObj As String
Dim fs, F, d, n
Dim sql, sql2 As String
Dim WMI
Dim wmiWin32Objects
Dim wmiWin32Object
strLst = Now() & " § Starting Refresh Procedure "
strStart = Now()
strLaps = Now()
DoCmd.SetWarnings False
ctl = 0
ctl.Max = 100 'Set max count for the progressbar
DoCmd.RunCommand acCmdRefreshPage
Set rstTskAuth = dbs.OpenRecordset("tblTaskAuthLists", dbOpenDynaset) 'User authorized ?
rstTskAuth.FindFirst "[tskName] = '" & "DataLoad" & "' and [tskNetUser] = '" & Form_frmLogin!LogonNetUser & "'"
If rstTskAuth.NoMatch Then
MsgBox (Form_frmLogin!LogonNetUser & " not authorized")
Else 'User = authorized
strArea = rstTskAuth!tskArea '= User's BusinessArea
rstTskAuth.Close
Set rstTskAuth = Nothing
Set rstCountLines = dbs.OpenRecordset("tbl_SQL", dbOpenDynaset) 'Counting sql lines User's BusinessArea
Do Until rstCountLines.EOF
With rstCountLines
If [rstCountLines]![SqlCat] = "RMS_DataRefresh" And [rstCountLines]![SqlBusinessDivision] = strArea Then
strSqlLines = strSqlLines + 1
End If
End With
rstCountLines.MoveNext
Loop
rstCountLines.Close
Set rstCountLines = Nothing
DoCmd.RunCommand acCmdRefreshPage
With Me.lstOvw
lstOvw.RowSourceType = "Value List"
lstOvw.RowSource = strLst
End With
DoCmd.RunCommand acCmdRefreshPage
For idx = 1 To strSqlLines Step 1
Set rstLoadDB2Data = dbs.OpenRecordset("tbl_SQL", dbOpenDynaset)
DoCmd.RunCommand acCmdRefreshPage
rstLoadDB2Data.FindFirst "[SqlCat] = 'RMS_DataRefresh' and [SqlBusinessDivision] = '" & strArea & "' and int([SqlSequence]) = '" & idx & "' and int([SqlActiv]) = '-1'"
If rstLoadDB2Data.NoMatch Then
Else
Set qdf = dbs.CreateQueryDef(idx, rstLoadDB2Data!SqlString)
DoCmd.RunCommand acCmdRefreshPage
DoCmd.OpenQuery idx, acViewNormal, acEdit
Set WMI = GetObject("WinMgmts://" & ComputerName)
Set wmiWin32Objects = WMI.InstancesOf("Win32_Processor")
With wmiWin32Object
For Each wmiWin32Object In wmiWin32Objects
strCPU = .loadpercentage
Next
End With
Set WMI = Nothing
Set wmiWin32Objects = Nothing
Set wmiWin32Object = Nothing
Set fso = CreateObject("Scripting.FileSystemObject")
Set F = fso.GetFile(CurrentProject.Path & "\" & CurrentProject.name)
strLst = strLst & ";" & Now() & " § " & rstLoadDB2Data!SqlObject & rstLoadDB2Data!SqlSequence & " " & rstLoadDB2Data!SqlNaming & ", Time elapsed: " & CVDate(TimeSerial(Hour(Now()), Minute(Now()), Second(Now())) - TimeSerial(Hour(strLaps), Minute(strLaps), Second(strLaps))) & ", Curr dB-Size: " & ((F.Size / 1024000) & " MB") & ", RAM-AVA: " & ((atGetMem(2) / 1024000) & " MB") & " or " & Int((atGetMem(2) / atGetMem(1) * 1000)) / 10 & " %" & ", CPU dropped to: " & strCPU & " %"
DoCmd.RunCommand acCmdRefreshPage
With Me.lstOvw
lstOvw.RowSourceType = "Value List"
lstOvw.RowSource = strLst
End With
DoCmd.RunCommand acCmdRefreshPage
rstLoadDB2Data.Edit
rstLoadDB2Data!SqlRunDate = Now()
ctl = Int((idx / strSqlLines) * 100)
rstLoadDB2Data!SqlRunComment = "Refreshed successfully"
rstLoadDB2Data!SqlTimLoad = CVDate(TimeSerial(Hour(Now()), Minute(Now()), Second(Now())) - TimeSerial(Hour(strLaps), Minute(strLaps), Second(strLaps)))
rstLoadDB2Data.Update
DoCmd.RunCommand acCmdRefreshPage
DoCmd.RunCommand acCmdRefreshPage
DoCmd.DeleteObject acQuery, idx
End If
rstLoadDB2Data.Close
Set rstLoadDB2Data = Nothing
strLaps = Now()
Next idx
Set qdf = Nothing
Let idx = 0
dbs.Close
Set dbs = Nothing
End If
Let ctl = 0
strLst = strLst & ";-----;" & Now() & " § Update Procedure Successfull " & ";-----;Total Time elapsed: " & CVDate(TimeSerial(Hour(Now()), Minute(Now()), Second(Now())) - TimeSerial(Hour(strStart), Minute(strStart), Second(strStart)))
With Me.lstOvw
lstOvw.RowSourceType = "Value List"
lstOvw.RowSource = strLst
End With
DoCmd.SetWarnings True
ErrorHandler:
Exit Sub
End Sub
---------------
Probably overlooked something important. Can anybody suggest ? Thks in advance !!