I have a function to draw some shapes on a form depending on values in queries/tables. The whole procedure takes up to ten seconds however, and as I'm new to coding, I assume I might have a lot to gain on making my code more efficient...?
Are there any very obvious time-consumers in my code, which can be settled much more efficiently?
Any guidance would be greatly appreciated.
(In the db I have a main form "Calendar," which has 67 subforms (frmSub01-67) in which drawings are made. The "finished product" looks more or less like this:
Are there any very obvious time-consumers in my code, which can be settled much more efficiently?
Any guidance would be greatly appreciated.
(In the db I have a main form "Calendar," which has 67 subforms (frmSub01-67) in which drawings are made. The "finished product" looks more or less like this:
Code:
Public Function makeArrows() Dim RecSetSortAll As Recordset Dim RecSetDatePoint As Recordset Dim RecSetRooms As Recordset Dim ctl As Control Dim frmSub As Form Dim strSubForm As Form startTime = Time() '*************************** '''initializing recordsets '*************************** 'Table!DatePointer.Refresh ' Tables!Bookings.Refresh 'Tables!Contacts.Refresh 'Queries!qSortAll.Refresh Set RecSetDatePoint = CurrentDb.OpenRecordset("DatePointer") Set RecSetSortAll = CurrentDb.OpenRecordset("qSortAll") Set RecSetRooms = CurrentDb.OpenRecordset("qAllRoomsList") 'Set rsRoomPoint = CurrentDb.OpenRecordset("qRoomPoint") 'Set rsTotRooms = CurrentDb.OpenRecordset("Rooms") ''' find rsDatePointer for calendarview start date, formerly variable "c" dStart = RecSetDatePoint.Fields("RStartDate").Value ''' find total no of rooms / rows in calendar 'totRooms = rsTotRooms.RecordCount 'MsgBox totRooms 'd = 0 'intNoRooms = RoomPoint 'str( = "frmSub1" 'str1 = "frmSub01" '*************************************************** '''looping thru every subform control to disconnect '*************************************************** For Each ctl In Forms!Calendar If ctl.ControlType = acSubform Then If Left(ctl.Name, 3) = "ctr" Then 'srcObject = Forms!Calendar!(ctl.Name).SourceObject 'MsgBox ctl.Name ''' disconnecting subform from mainform, opening subform Forms!Calendar!(ctl.Name).SourceObject = "" 'Forms!Calendar!(ctl.Name).Height = 400 End If End If Next ctl 'MsgBox "disconnected" '****************************************** ''' delete all controls in all subforms ... '****************************************** p = 0 Do While p < 67 p = p + 1 If p < 10 Then strForms = "frmSub0" & p Else strForms = "frmSub" & p End If DoCmd.OpenForm strForms, acDesign, , , acFormEdit, acHidden Do While Forms(strForms).Controls.Count > 0 DeleteControl strForms, Forms(strForms).Controls(0).Name Loop 'With Forms(strForms).Controls ' Do While .Count > 0 ' Call DeleteControl(strForms, .Item(0).Name) ' Loop 'End With DoCmd.Close acForm, strForms, acSaveYes Loop 'MsgBox p & " subforms massacred..." '***************************** ' ARROWS LOOP for ALL ROOMS '***************************** q = 0 'frmSub number count Do Until RecSetRooms.EOF '************************************************** 'drawing room# box for each room (each RecSetRooms) '************************************************** q = q + 1 '******************************************* If q < 10 Then strForms = "frmSub0" & q Else strForms = "frmSub" & q 'determining which form, counting roomlist!! '******************************************* roomNow = RecSetRooms.Fields("RoomNumber").Value 'MsgBox "drawing room-number " & roomNow & " in " & strForms & "..." DoCmd.OpenForm strForms, acDesign, , , acFormEdit, acHidden 'open frmSub for drawing '******************* 'DRAW: room-number box '******************* Set cRoomBox = CreateControl(strForms, acLabel) With cRoomBox .BackStyle = 1 .Width = 580 .Height = 375 .Left = 50 .Top = 25 .Caption = roomNow .BorderStyle = 1 .FontSize = 14 .TextAlign = 2 .BorderWidth = 1 .FontWeight = 700 '.BackColor = RGB(0, 255, 0) End With DoCmd.Close acForm, strForms, acSaveYes 'close frmSub after drawing '******** 'end draw '******** '************************************ 'start drawing bookings for this room '************************************ If Not RecSetSortAll.EOF Then 'make sure still bookings to be drawn, if not, return to room-number loop recordNow = RecSetSortAll.Fields("qBase.RoomNumber").Value 'recordset of bookings roomNow = RecSetRooms.Fields("RoomNumber").Value 'list of roomnumbers 'MsgBox "before if recnow = roomnow, values are " & recordNow & " = " & roomNow If recordNow = roomNow Then 'there are bookings for this room Do Until recordNow <> roomNow 'MsgBox "inside loop of recnow <> roomnow, w values " & recordNow & " <> " & roomNow '*************************** ' open subform (again), draw '*************************** '******************************************* If q < 10 Then strForms = "frmSub0" & q Else strForms = "frmSub" & q 'determining which form, counting roomlist!! '******************************************* '(not really necessary, as q only changes with roomlist) 'MsgBox "match! starting drawing in " & strForms & "..." DoCmd.OpenForm strForms, acDesign, , , acFormEdit, acHidden 'open frmSub for drawing '// finding variables slBeg = RecSetSortAll.Fields("SlotBegin").Value slEnd = RecSetSortAll.Fields("SlotEnd").Value rcBookID = RecSetSortAll.Fields("Bookings.ID").Value leftPos = (slBeg - dStart) * 1217 + 850 bConf = RecSetSortAll.Fields("Confirmed").Value 'MsgBox bConf 'widthMsg = ((slEnd - slBeg) * 1217) - 380 'MsgBox "starting pos = " & leftPos & ", and width = " & widthMsg '// TAIL IMAGE Set cImg1 = CreateControl(strForms, acImage) If bConf = True Then With cImg1 .BackStyle = 0 .Width = 432 .SizeMode = 0 .Height = 360 .Left = leftPos .Top = 30 .ControlTipText = rcBookID .OnClick = "" .Picture = "C:\Users\Lailita\Documents\arrows\yellowA.wmf" End With Else With cImg1 .BackStyle = 0 .Width = 432 .SizeMode = 0 .Height = 360 .Left = leftPos .Top = 30 .ControlTipText = rcBookID .OnClick = "" .Picture = "C:\Users\Lailita\Documents\arrows\yshadeA.wmf" End With End If '// HEAD IMAGE Set cImg2 = CreateControl(strForms, acImage) If bConf = True Then With cImg2 .BackStyle = 0 .Width = 432 .Height = 360 .SizeMode = 0 .ControlTipText = rcBookID .Left = leftPos + ((slEnd - slBeg) * 1217) - 380 '.Left = leftPos + 300 .Top = 30 .Picture = "C:\Users\Lailita\Documents\arrows\yellowAh.wmf" End With Else With cImg2 .BackStyle = 0 .Width = 432 .SizeMode = 0 .Height = 360 .ControlTipText = rcBookID .Left = leftPos + ((slEnd - slBeg) * 1217) - 380 '.Left = leftPos + 300 .Top = 30 .Picture = "C:\Users\Lailita\Documents\arrows\yshadeAh.wmf" End With End If '// Shaded Box Set cImg3 = CreateControl(strForms, acImage) If bConf = False Then With cImg3 .BackStyle = 0 .Width = ((slEnd - slBeg) * 1217) - 432 - 40 .Height = 360 .SizeMode = 0 .ControlTipText = rcBookID .Left = leftPos + 250 '.Left = leftPos + 300 .Top = 30 .Picture = "C:\Users\Lailita\Documents\arrows\yshadeB.wmf" End With Else With cImg3 .BackStyle = 0 .Width = ((slEnd - slBeg) * 1217) - 432 - 40 .Height = 360 .SizeMode = 0 .ControlTipText = rcBookID .Left = leftPos + 250 '.Left = leftPos + 300 .Top = 30 .Picture = "C:\Users\Lailita\Documents\arrows\yellowB.wmf" End With 'MsgBox widthMsg End If '// BODY LABEL Set cLbl1 = CreateControl(strForms, acLabel) With cLbl1 .Visible = True .BackColor = RGB(255, 194, 14) .ForeColor = RGB(255, 255, 255) .FontWeight = 900 .TopMargin = 34 .TextAlign = 2 .BackStyle = 0 .FontWeight = 900 .Top = 40 .Height = 330 .Left = leftPos + 250 .Width = ((slEnd - slBeg) * 1217) - 432 - 40 '.Width = 200 .Caption = rcBookID End With If bConf = False Then With cLbl1 .ForeColor = RGB(0, 0, 0) End With End If DoCmd.Close acForm, strForms, acSaveYes 'close frmSub after drawing of booking ' MsgBox "success, moving to next booking..." RecSetSortAll.MoveNext 'moving to next booking If Not RecSetSortAll.EOF Then recordNow = RecSetSortAll.Fields("qBase.RoomNumber").Value 'roomnumber of next booking Else Exit Do End If Loop Else 'MsgBox "no more bookings for this room, moving on to next room.." ' RecSetSortAll.MoveNext End If Else 'RecSetSortAll.EOF If..Then 'MsgBox "recsetsortall.eof" End If RecSetRooms.MoveNext Loop 'RecSetRooms.EOF loop 'MsgBox "reconnecting" '************************************************* '''looping thru every subform control to RECONNECT '************************************************* For Each ctl In Forms!Calendar If ctl.ControlType = acSubform Then If Left(ctl.Name, 3) = "ctr" Then 'srcObject = Forms!Calendar!(ctl.Name).SourceObject 'MsgBox ctl.Name ''' disconnecting subform from mainform, opening subform srcOb = "frmSub" & Right(ctl.Name, 2) Forms!Calendar!(ctl.Name).SourceObject = srcOb End If End If Next ctl 'MsgBox "reconnected" 'DoCmd.Requery 'MsgBox "refreshing" Forms!Calendar.Refresh 'MsgBox "refreshed" endTime = Time() elapsedTime = endTime - startTime MsgBox "that took " & Second(elapsedTime) & " secs." End Function
Comment