M
Mattias Jonsson
All,
I have devised a strategy for how to work with dialogue boxes in general
that works beautifully except for one detail. It will cause the computer to
run at 100% CPU usage while the dialogue is displayed. Does anybody know a
better way to do this?
Overview
- The form is Modal and BorderStyle=Dialog
- The form OK button has code that sets Me.Visible=False
1 Open the form in hidden mode and manipulate it
2 Set the form Visible=True and it will behave like a dialogue
3 The function that opened the form loops in the background
4 When the user is done with the form, the function stops looping
5 If the form was set to Visible=False instead of being closed then we
can still get values from the form objects
6 Close the form and return something from the function
Here are the pros of the method
- I can access the form's properties and objects directly before showing it
to the user instead of passing around global variables
- I can initialize the form using more [and other types of] information than
what I could fit in OpenArgs
- I can open an instance of a form as a dialogue which would normally
preclude using OpenArgs
**Sample code (from a class module, clsSearch)**
'Instance of frmSearch
Private frmSearch As Form_frmSearch
'Loads frmSearch
'Function returns True if a specimen is selected
'Function returns False if search is cancelled
'After searching, the selected ID's can be accessed
' by using Count and Item properties
Public Function LoadSearch(Optional MultiSelect As Boolean = False) As
Boolean
On Error GoTo Err_LoadSearch
Dim ysnStay As Boolean
'Delete old search results
arrSelectedID.Reset
If frmSearch Is Nothing Then
'Create the form
Set frmSearch = New Form_frmSearch
'Multiselect=2, No multiselect=0
frmSearch.MultiSelect = IIf(MultiSelect, 2, 0)
With frmSearch
'Make the form use the cached lookup filter
Set .objLookupFilter = objLookupFilter
'Refresh the form to reflect any existing filtering
.RefreshLookupFilter
'Make the form visible
.Visible = True
.txtLabIdentifier.SetFocus
End With
Else
'The form is already loaded
'Make the form visible
frmSearch.Visible = True
End If
Do While SearchFormIsVisible = True
'Code will loop [wait] here while frmSearch
' is loaded and visible
'**** Here's the loop ****
DoEvents
Loop
'Disable error handling
On Error Resume Next
'Get selected SpecimenID array from search form
'The array will be Nothing if the form was
' closed. The form will be closed for example
' if the user clicks "X"
arrSelectedID.arr = frmSearch.arrSpecimenID.arr
'Reset the form's array in case it will be
' reused (see "Stay!" functionality below)
frmSearch.arrSpecimenID.Reset
'Return True if we have selected SpecimenID's
LoadSearch = arrSelectedID.Count > 0
'Get current Stay! status
'Stay! means that we don't unload the form
' between searches, but the user will
' come back to the search form in exactly
' the same state as it was left
'If the user clicks Cancel then Stay! will
' be set to False
ysnStay = frmSearch.chkStay
If Not ysnStay Then Set frmSearch = Nothing
End_LoadSearch:
On Error Resume Next
Exit Function
Err_LoadSearch:
InputBox Err.Description, "ERROR", Err.Number
Resume End_LoadSearch
End Function
'Returns True if frmSearch is loaded and visible
Public Function SearchFormIsVisible() As Boolean
On Error Resume Next
SearchFormIsVisible = frmSearch.Visible
End Function
**End code**
Hope you can ignore irrelevant parts of code. Most properties of the class
module have been left out for clarity. It is the DoEvents loop that causes
the 100% CPU usage. It is not noticeable at all to the user but for example
if I want to use this approach on a Citrix server (multi user) then it might
cause problems - so if anybody knows another method or a variation that
would offer similar pros then please let me know.
Thanks,
Mattias Jonsson
I have devised a strategy for how to work with dialogue boxes in general
that works beautifully except for one detail. It will cause the computer to
run at 100% CPU usage while the dialogue is displayed. Does anybody know a
better way to do this?
Overview
- The form is Modal and BorderStyle=Dialog
- The form OK button has code that sets Me.Visible=False
1 Open the form in hidden mode and manipulate it
2 Set the form Visible=True and it will behave like a dialogue
3 The function that opened the form loops in the background
4 When the user is done with the form, the function stops looping
5 If the form was set to Visible=False instead of being closed then we
can still get values from the form objects
6 Close the form and return something from the function
Here are the pros of the method
- I can access the form's properties and objects directly before showing it
to the user instead of passing around global variables
- I can initialize the form using more [and other types of] information than
what I could fit in OpenArgs
- I can open an instance of a form as a dialogue which would normally
preclude using OpenArgs
**Sample code (from a class module, clsSearch)**
'Instance of frmSearch
Private frmSearch As Form_frmSearch
'Loads frmSearch
'Function returns True if a specimen is selected
'Function returns False if search is cancelled
'After searching, the selected ID's can be accessed
' by using Count and Item properties
Public Function LoadSearch(Optional MultiSelect As Boolean = False) As
Boolean
On Error GoTo Err_LoadSearch
Dim ysnStay As Boolean
'Delete old search results
arrSelectedID.Reset
If frmSearch Is Nothing Then
'Create the form
Set frmSearch = New Form_frmSearch
'Multiselect=2, No multiselect=0
frmSearch.MultiSelect = IIf(MultiSelect, 2, 0)
With frmSearch
'Make the form use the cached lookup filter
Set .objLookupFilter = objLookupFilter
'Refresh the form to reflect any existing filtering
.RefreshLookupFilter
'Make the form visible
.Visible = True
.txtLabIdentifier.SetFocus
End With
Else
'The form is already loaded
'Make the form visible
frmSearch.Visible = True
End If
Do While SearchFormIsVisible = True
'Code will loop [wait] here while frmSearch
' is loaded and visible
'**** Here's the loop ****
DoEvents
Loop
'Disable error handling
On Error Resume Next
'Get selected SpecimenID array from search form
'The array will be Nothing if the form was
' closed. The form will be closed for example
' if the user clicks "X"
arrSelectedID.arr = frmSearch.arrSpecimenID.arr
'Reset the form's array in case it will be
' reused (see "Stay!" functionality below)
frmSearch.arrSpecimenID.Reset
'Return True if we have selected SpecimenID's
LoadSearch = arrSelectedID.Count > 0
'Get current Stay! status
'Stay! means that we don't unload the form
' between searches, but the user will
' come back to the search form in exactly
' the same state as it was left
'If the user clicks Cancel then Stay! will
' be set to False
ysnStay = frmSearch.chkStay
If Not ysnStay Then Set frmSearch = Nothing
End_LoadSearch:
On Error Resume Next
Exit Function
Err_LoadSearch:
InputBox Err.Description, "ERROR", Err.Number
Resume End_LoadSearch
End Function
'Returns True if frmSearch is loaded and visible
Public Function SearchFormIsVisible() As Boolean
On Error Resume Next
SearchFormIsVisible = frmSearch.Visible
End Function
**End code**
Hope you can ignore irrelevant parts of code. Most properties of the class
module have been left out for clarity. It is the DoEvents loop that causes
the 100% CPU usage. It is not noticeable at all to the user but for example
if I want to use this approach on a Citrix server (multi user) then it might
cause problems - so if anybody knows another method or a variation that
would offer similar pros then please let me know.
Thanks,
Mattias Jonsson