Every night i create a batch reports and send them with email to people.
The first reports go quickly but then memory usage of acces keep growing and then start to get slowly.
I Think i close every recorset that i use but still keep getting slow.
The reports have a chart inside so maybe thats what use the memory.
I need some help to keep getting it smoothly because now i talk about 200 reports but soon will be many more.
I get my data for the reports from 2 SQL servers wich i put in temp table wich i clean after the report is created and then refill same table with new data
This code check a table to determine wich reports have to been created
This code will get the data put it in a table
another one because have more systems (SQL's) to get data from
and this is the email function
The full db
https://drive.google.com/file/d/0BynMVePAwPceQ3R4TEtGc3E4REk/view?usp=sharing
screenshot of memory when still creating reports an did create 90 reports as attachment
If anybody have suggestions or solutions to speed it up I will be very happy
The first reports go quickly but then memory usage of acces keep growing and then start to get slowly.
I Think i close every recorset that i use but still keep getting slow.
The reports have a chart inside so maybe thats what use the memory.
I need some help to keep getting it smoothly because now i talk about 200 reports but soon will be many more.
I get my data for the reports from 2 SQL servers wich i put in temp table wich i clean after the report is created and then refill same table with new data
This code check a table to determine wich reports have to been created
Code:
Public Function THistoriek()
Dim rst3 As DAO.Recordset
Set rst3 = CurrentDb.OpenRecordset("Select * from dbo_tbl_Temprapporten_Ontvangers WHERE [SYSTEEM]=1 AND [PERIODE]=1", dbOpenDynaset, dbSeeChanges)
With rst3
While Not rst3.EOF
Tagnr = rst3.Fields(3).Value
TToestel = rst3.Fields(1).Value
OPMERKING = rst3.Fields(6).Value
Call GetSauterDaily("" & Tagnr & "", "" & TToestel & "", "" & OPMERKING & "")
Pause (2)
.MoveNext
Wend
End With
Set rst3 = Nothing
Set rst3 = CurrentDb.OpenRecordset("Select * from dbo_tbl_Temprapporten_Ontvangers WHERE [SYSTEEM]=2 AND [PERIODE]=1", dbOpenDynaset, dbSeeChanges)
With rst3
While Not rst3.EOF
Tagnr = rst3.Fields(3).Value
TToestel = rst3.Fields(1).Value
OPMERKING = rst3.Fields(6).Value
Call GetSiemensDaily("" & Tagnr & "", "" & TToestel & "", "" & OPMERKING & "")
Pause (2)
.MoveNext
Wend
End With
Set rst3 = Nothing
If (Weekday(Date, vbFriday) = 1) Then
Set rst3 = CurrentDb.OpenRecordset("Select * from dbo_tbl_Temprapporten_Ontvangers WHERE [SYSTEEM]=1 AND [PERIODE]=2", dbOpenDynaset, dbSeeChanges)
With rst3
While Not rst3.EOF
Tagnr = rst3.Fields(3).Value
TToestel = rst3.Fields(1).Value
OPMERKING = rst3.Fields(6).Value
Call GetSauterWeekly("" & Tagnr & "", "" & TToestel & "", "" & OPMERKING & "")
Pause (2)
.MoveNext
Wend
End With
rst3.Close
Set rst3 = Nothing
Set rst3 = CurrentDb.OpenRecordset("Select * from dbo_tbl_Temprapporten_Ontvangers WHERE [SYSTEEM]=2 AND [PERIODE]=2", dbOpenDynaset, dbSeeChanges)
With rst3
While Not rst3.EOF
Tagnr = rst3.Fields(3).Value
TToestel = rst3.Fields(1).Value
OPMERKING = rst3.Fields(6).Value
Call GetSiemensWeekly("" & Tagnr & "", "" & TToestel & "", "" & OPMERKING & "")
Pause (2)
.MoveNext
Wend
End With
rst3.Close
Set rst3 = Nothing
End If
Set rst3 = CurrentDb.OpenRecordset("Select * from dbo_tbl_Temprapporten_Ontvangers WHERE [SYSTEEM]=1 AND [PERIODE]=3", dbOpenDynaset, dbSeeChanges)
With rst3
While Not rst3.EOF
Tagnr = rst3.Fields(3).Value
TToestel = rst3.Fields(1).Value
Call GetSauterErbis("" & Tagnr & "", "" & TToestel & "", "")
.MoveNext
Wend
End With
rst3.Close
Set rst3 = Nothing
Set rst3 = CurrentDb.OpenRecordset("Select * from dbo_tbl_Temprapporten_Ontvangers WHERE [SYSTEEM]=2 AND [PERIODE]=3", dbOpenDynaset, dbSeeChanges)
With rst3
While Not rst3.EOF
Tagnr = rst3.Fields(3).Value
TToestel = rst3.Fields(1).Value
Call GetSiemensErbis("" & Tagnr & "", "" & TToestel & "", "")
.MoveNext
Wend
End With
rst3.Close
Set rst3 = Nothing
End Function
This code will get the data put it in a table
Code:
Public Function GetSauterDaily(tagid As String, TNUMMER As String, opm As String)
Dim rst As DAO.Recordset
Dim dt As String
Dim rst2 As DAO.Recordset
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strSQL As String
Dim rs2 As DAO.Recordset
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
Set rst2 = CurrentDb.OpenRecordset("dbo_tbl_Temprapporten_Historiek", dbOpenDynaset, dbSeeChanges)
dt = 1000 * DateDiff("s", #1/1/1970 2:00:00 AM#, Now() - 1.1)
tagid2 = (65536 * 10) + tagid
conn.Open "Driver={SQL Server};" & _
"Server=CI2008BAZUUR;" & _
"Database=NPO_VfiTag;" & _
"Trusted_Connection=Yes"
strSQL = "SELECT [time], [gateId], [flags], [value]" & _
"FROM [VfiTagNumHistory] " & _
"WHERE ([time] > '" & dt & "')" & _
"AND ([gateId] = '" & tagid2 & "') " & _
"AND ([flags] = '2')"
rs.Open strSQL, conn, adOpenForwardOnly
If Not (rs.EOF And Not rs.BOF) Then
Do Until rs.EOF
With rst2
.AddNew
newtime = DateAdd("s", rs.Fields(0).Value / 1000, #1/1/1970 2:00:00 AM#)
.Fields(1).Value = TNUMMER
.Fields(2).Value = newtime
.Fields(3).Value = rs.Fields(3).Value
.Update
rs.MoveNext
End With
Loop
End If
rst2.Close
Set rst2 = Nothing
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
'alarmhsitoriek
Set conn = New ADODB.Connection
Set rst2 = CurrentDb.OpenRecordset("dbo_tbl_Temprapporten_Alarmen", dbOpenDynaset, dbSeeChanges)
conn.Open "Driver={SQL Server};" & _
"Server=CI2008BAZUUR;" & _
"Database=NPO_VfiAlarm;" & _
"Trusted_Connection=Yes"
strSQL = "SELECT [Start_Time],[End_Time],[Text] " & _
"FROM [VfiAlarmHistory] " & _
"WHERE [text] like '%" & TNUMMER & "%'"
rs.Open strSQL, conn, adOpenForwardOnly
If Not (rs.EOF And rs.BOF) Then
With rs
.MoveFirst
While Not .EOF
With rst2
.AddNew
newtime = DateAdd("s", rs.Fields(0).Value / 1000, #1/1/1970 2:00:00 AM#)
.Fields(1).Value = newtime
.Fields(2).Value = "IN ALARM"
.Fields(3).Value = rs.Fields(2).Value
.Fields(4).Value = TNUMMER
.Update
.AddNew
newtime = DateAdd("s", rs.Fields(1).Value / 1000, #1/1/1970 2:00:00 AM#)
.Fields(1).Value = newtime
.Fields(2).Value = "UIT ALARM"
.Fields(3).Value = rs.Fields(2).Value
.Fields(4).Value = TNUMMER
.Update
rs.MoveNext
End With
Wend
End With
End If
rs.Close
Set rs = Nothing
rst2.Close
Set rst2 = Nothing
conn.Close
Set conn = Nothing
Call SetTnummer(TNUMMER)
Call SetOpmerking(opm)
Wie = DLookup("ONTVANGER", "dbo_tbl_Temprapporten_Ontvangers", "[T-Nummer] = '" & TNUMMER & "'")
Ond = "Historische Gegevens GBS voor " & TNUMMER
Call SendEmail("" & Wie & "", "" & Ond & "", , "filip.vandenbosch@uzleuven.be", "rpt_Temprapporten_Historiek")
DoCmd.RunSQL ("DELETE * FROM dbo_tbl_Temprapporten_Historiek")
DoCmd.RunSQL ("DELETE * FROM dbo_tbl_Temprapporten_Alarmen")
End Function
another one because have more systems (SQL's) to get data from
Code:
Public Function GetSiemensDaily(tagid As String, TNUMMER As String, opm As String)
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strSQL As String
Dim rs2 As DAO.Recordset
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
'Gelogde waardes
Set rst2 = CurrentDb.OpenRecordset("dbo_tbl_Temprapporten_Historiek", dbOpenDynaset, dbSeeChanges)
conn.Open "Driver={SQL Server};" & _
"Server=SQLCI2008R;" & _
"Database=DIV23!PRJ=UZ_Leuven!DB=ISHTTND;" & _
"Trusted_Connection=Yes"
strSQL = "SELECT [DateTimeStamp],[Value] FROM [TrendRecord] " _
& "WHERE [TrendLogId] = '" & tagid & "' And [DateTimeStamp] > Getdate()-1.1 AND [Qualitytag]='192'"
rs.Open strSQL, conn, adOpenForwardOnly
If Not (rs.EOF And rs.BOF) Then
With rs
.MoveFirst
While Not .EOF
With rst2
.AddNew
.Fields(1).Value = TNUMMER
.Fields(2).Value = rs.Fields(0).Value
.Fields(3).Value = rs.Fields(1).Value
.Update
rs.MoveNext
End With
Wend
End With
End If
rs.Close
Set rs = Nothing
rst2.Close
Set rst2 = Nothing
conn.Close
Set conn = Nothing
'Alarmhistory
Set rst2 = CurrentDb.OpenRecordset("dbo_tbl_Temprapporten_Alarmen", dbOpenDynaset, dbSeeChanges)
conn.Open "Driver={SQL Server};" & _
"Server=SQLCI2008R;" & _
"Database=DIV23!PRJ=UZ_Leuven!DB=ISHTLOG;" & _
"Trusted_Connection=Yes"
strSQL = "SELECT [DateTimeStamp],[EventText],[TechDescription],[ArgValueReal],[ArgUnitText] " _
& "FROM [LogEntry] " _
& "WHERE [EventText] IN ('alarm into', 'alarm low', 'alarm high', 'alarm return to normal','alarm fault') " _
& "AND [MgtStationName] = 'desigo1' " _
& "AND [TechDescription] like '%" & TNUMMER & "%'" _
& "AND [DateTimeStamp] > getdate() -1 " _
& "order by [DateTimeStamp] asc"
rs.Open strSQL, conn, adOpenForwardOnly
If Not (rs.EOF And rs.BOF) Then
With rs
.MoveFirst
While Not .EOF
With rst2
.AddNew
.Fields(1).Value = rs.Fields(0).Value
.Fields(2).Value = rs.Fields(1).Value
.Fields(3).Value = rs.Fields(2).Value
.Fields(4).Value = TNUMMER
.Fields(5).Value = rs.Fields(3).Value
.Fields(6).Value = rs.Fields(4).Value
.Update
rs.MoveNext
End With
Wend
End With
End If
rs.Close
Set rs = Nothing
rst2.Close
Set rst2 = Nothing
conn.Close
Set conn = Nothing
Call SetTnummer(TNUMMER)
Call SetOpmerking(opm)
Wie = DLookup("ONTVANGER", "dbo_tbl_Temprapporten_Ontvangers", "[T-Nummer] = '" & TNUMMER & "'")
Ond = "Historische Gegevens GBS voor " & TNUMMER
Call SendEmail("" & Wie & "", "" & Ond & "", , "filip.vandenbosch@uzleuven.be", "rpt_Temprapporten_Historiek")
DoCmd.RunSQL ("DELETE * FROM dbo_tbl_Temprapporten_Historiek")
DoCmd.RunSQL ("DELETE * FROM dbo_tbl_Temprapporten_Alarmen")
End Function
and this is the email function
Code:
Public Function SendEmail(Email_To As String, Subject As String, Optional Body As String, Optional Email_BCC As String, Optional rapport As String, Optional printout As String) As Boolean
Dim Msg As String
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
Dim NotificationTypeDescription As String
Dim DocPath As String
On Error Resume Next
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If
Set oItem = oOutlookApp.CreateItem(olMailItem)
With oItem
If LenB(Email_To) <> 0 Then .To = Email_To
If Not IsNull(Email_BCC) And LenB(Email_BCC) <> 0 Then .CC = Email_BCC
.Subject = Subject
.Body = Body
.BodyFormat = olFormatPlain
'Add the document as an attachment, you can use the .displayname property
'to set the description that's used in the message
If Not IsNull(Attachdoc) Then
DocPath = CurrentProject.path & "\Temp\"
'DoCmd.OpenReport rapport, acViewPreview, , , acWindowNormal
DoCmd.OutputTo acOutputReport, rapport, acFormatPDF, DocPath & rapport & ".PDF", False
If (printout = "y") Then DoCmd.printout
.Attachments.Add DocPath & rapport & ".PDF"
End If
.Send
End With
DoCmd.Close acReport, rapport
If bStarted Then
' MsgBox "Sending email re:" & Chr$(13) & Subject & Chr$(13) & Chr$(13) & "(You will need to CLICK YES AGAIN when MS Outlook asks" & Chr$(13) & "whether to permit this...)", vbOKOnly, "Sending Automatic Email..."
oItem.Send
' oOutlookApp.Quit
Else
oItem.Display
End If
On Error Resume Next
Set oItem = Nothing
Set oOutlookApp = Nothing
SendEmailViaOutlook = True
End Function
The full db
https://drive.google.com/file/d/0BynMVePAwPceQ3R4TEtGc3E4REk/view?usp=sharing
screenshot of memory when still creating reports an did create 90 reports as attachment
If anybody have suggestions or solutions to speed it up I will be very happy

Attachments
Last edited: