Dear All,
I have written a subroutine that formats a subform based on whether there are records to display or not - basically, it hides some controls if there are no records to display and instead shows a label with a message that there are no records to display. The only problem is that it seems to run very slowly, it can take up to 30 seconds to display the subforms, and while that is happening, the user can't do anything else in the database.
Does anyone who is a sharper programmer than I have any tips for speeding up this code? I have pasted the entire subroutine below:
Thanks in advance!
Heather
Floyd Innovations ![[yinyang] [yinyang] [yinyang]](/data/assets/smilies/yinyang.gif)
I have written a subroutine that formats a subform based on whether there are records to display or not - basically, it hides some controls if there are no records to display and instead shows a label with a message that there are no records to display. The only problem is that it seems to run very slowly, it can take up to 30 seconds to display the subforms, and while that is happening, the user can't do anything else in the database.
Does anyone who is a sharper programmer than I have any tips for speeding up this code? I have pasted the entire subroutine below:
Code:
Public Sub SfrmNoData(CurrentForm As Form, SfrmName As String, RecordCount, _
NoDataLabelName As String, Optional HideBlankRows As Boolean = False)
'=============================================================================
' FormsCode.SfrmNoData
'-----------------------------------------------------------------------------
' Copyright by Heather L. Floyd - Floyd Innovations - [URL unfurl="true"]www.floydinnovations.com[/URL]
' Created 01-28-2004
'-----------------------------------------------------------------------------
' To show/hide subform controls based on whether there are records or not
' Any controls that should be displayed regardless of the number of records
' should have a suffix of "_AS" on their names. Any controls that should always
' be hidden should have a suffix of "_NS"
'
' *NOTE* You should have at least one control that can receive focus in the
' subform set to "_AS", and if you receive errors about no being able to hide
' the current control, have the subform's "OnOpen" Event set the focus to a
' control that will always be visible.
'-----------------------------------------------------------------------------
' Parameters:
' ARGUEMENT DESCRIPTION
'-----------------------------------------------------------------------------
' CurrentForm (Form) : Use 'ME' keyword
' sfrmName (String) : Name of subform in question
' RecordCount (Variant) : Number of records in subform
' (referencing a Count Query value is most dependable)
' NoDataLabelName (String) : Name of control that should display if no records
'-----------------------------------------------------------------------------
' Example:
'-----------------------------------------------------------------------------
' Placed in Master Form's OnCurrent Event, or in OnOpen Event
' SfrmNoData "sfrmAccounts_Deals", Me.txtDealCount, "lblNoData"
'-----------------------------------------------------------------------------
'=============================================================================
On Error GoTo ErrorCode
'Variables
Dim ctrl As Control
Dim Frm As Form
Dim iRecCount As Integer
Dim strFormName As String
'Values
'Debug.Print "RecordCount: " & RecordCount & "(" & TypeName(RecordCount) & ")"
strFormName = CurrentForm.Name
iRecCount = Nz(RecordCount, 0)
'Debug.Print "iRecCount: " & iRecCount
'Set frm = Forms(Screen.ActiveForm.Name)(SfrmName).Form
Set Frm = Forms(strFormName)(SfrmName).Form
'Debug.Print "Form: " & frm.Name
'Show/Hide controls
If iRecCount = 0 Then
'No data, hide all labels except NoDataLabel
For Each ctrl In Frm.Controls
'Debug.Print " " & ctrl.Name
If ctrl.Name = NoDataLabelName Then
ctrl.Visible = True
ElseIf InStr(ctrl.Name, "_AS") > 0 Then
ctrl.Visible = True
ElseIf InStr(ctrl.Name, "_NS") > 0 Then
ctrl.Visible = False
Else
ctrl.Visible = False
End If
'Debug.Print " Visble: " & ctrl.Visible
Next
Else
'Data, hide NoDataLabel, show others
For Each ctrl In Frm.Controls
If ctrl.Name = NoDataLabelName Then
ctrl.Visible = False
ElseIf InStr(ctrl.Name, "_AS") > 0 Then
ctrl.Visible = True
ElseIf InStr(ctrl.Name, "_NS") > 0 Then
ctrl.Visible = False
Else
ctrl.Visible = True
End If
Next
End If
'Set DataType to Snapshot
If HideBlankRows = True And iRecCount <> 0 Then
Frm.RecordsetType = 2 'Snapshot
Else
'leave unchanged
End If
ExitCode:
Exit Sub
' Error handling block added by Error Handler Add-In. DO NOT EDIT this block of code.
' Automatic error handler last updated at 01-28-2004 14:23:58
ErrorCode:
Select Case Err.Number
Case 2165 'can't hide object with focus
Resume Next
Case 2467 'object is closed
DoCmd.Close
DoCmd.OpenForm strFormName
'Debug.Print "Handled - Error " & Err.Number & ": " & Err.Description & " - FormsCode.SfrmNoData"
Resume ExitCode
Case 2465 'can't locate subform
'Debug.Print "Error " & Err.Number & ": " & Err.Description & " - FormsCode.SfrmNoData"
Resume ExitCode
Case 2475 'No form is open
'Debug.Print "Error " & Err.Number & ": " & Err.Description & " - FormsCode.SfrmNoData"
Resume ExitCode
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "FormsCode.SfrmNoData"
Resume ExitCode
End Select
' End Error handling block.
End Sub
Thanks in advance!
Heather
![[yinyang] [yinyang] [yinyang]](/data/assets/smilies/yinyang.gif)
![[yinyang] [yinyang] [yinyang]](/data/assets/smilies/yinyang.gif)