Clicking a button seems to do some of the work, but not all, in the handler

tfurnivall

New Member
Joined
Jan 15, 2012
Messages
48
This one really has me stumped!

I'm using a button to either Clear a form, or reload the values. If we are in Edit mode, then the button is captioned Refresh, and will clear the form (removing any error notifications) and reload the data into the form fields. If we are not in Edit mode, then the button is captioned Clear, and will simplyclear the form fields (and any associated error notifications).

Code for the handler is as follows:
Code:
Private Sub cmdClearRefresh_Click()

TraceEvent Me.Name, "cmdClearRefresh", "Click", ProcessEvents, Me.cmdClearRefresh.Caption

Me.lblStatusInfoLine.Caption = UCase(Me.cmdClearRefresh.Caption) & " form fields"
If Me.cmdClearRefresh.Caption = "Clear" Then
   ClearFormFields
Else
   ClearFormFields
   LoadObjectIntoFormFields
End If
SetFormAction ViewAction                 '   Also sets captions!

'   We always reset the focus to the first key field
Me.cboKey1.SetFocus

End Sub

The code to clear the form fields is this:
Code:
Sub ClearFormFields()

TraceProc "ClearFormFields"

'   Clear all form fields, including those that are not visible,
'   and set the focus to the first key field (provided it is visible)!
'   We also set the background color to FormNoErrorCOlor

TraceProc "ClearFormFields", Me.Name

'   Disable event processing so that we don't get inundated with work!
ProcessEvents = False

'   Key fields
Me.cboKey1.Text = ""
Me.cboKey1.BackColor = FormNoErrorColor

Me.cboKey2.Text = ""
Me.cboKey2.BackColor = FormNoErrorColor

'   Data fields
Me.txtField1 = ""
Me.txtField1.BackColor = FormNoErrorColor

Me.txtField2 = ""
Me.txtField2.BackColor = FormNoErrorColor

'   Navigation area
Me.txtNavigationKey.Text = ""
Me.txtNavigationKey.BackColor = FormNoErrorColor

'   Lastly, clear the status line
ClearStatusLine

ProcessEvents = True

End Sub

I'm provoking an error in field cboKey2, by clearing it and attempting to update (ie Edit) the underlying object, using the following procedure (ValidateFormFields), and its helper procedure SetErrorField:
Code:
Sub SetErrorField(ctl As MSForms.Control, Message As String)

TraceProc "SetErrorField", ctl.Name, Message

If FormErrorCount = 0 Then
   ctl.BackColor = FormErrorColor
   ctl.SetFocus
   Me.lblStatusInfoLine.Caption = Message
Else
   ctl.BackColor = FormSecondErrorColor
End If
FormErrorCount = FormErrorCount + 1

End Sub

Code:
Function ValidateFormFields() As Boolean

Dim TempValue As Boolean

TraceProc "ValidateFormFields", Me.cmdAction.Caption, KeyOfReference, ReferenceKeyValue

'   We assert that Validation passes. Then any error is added to the error count,
'   and also has the form set to error color.
FormErrorCount = 0
FormErrorMessage = ""
FormErrorFlag = False

'   Check each field. They should not need formatting, although they may be empty.
'   A field previously stored with an apostrophe (ie bypass formatting) and which has been
'   untouched since display, will therefore transfer back to the storage mechanism
'   without problem.

'   Check that unique keys are going to maintain their uniqueness.
'   Key1
Me.cboKey1.BackColor = FormNoErrorColor
If Me.cboKey1.Text = "" Then
   SetErrorField Me.cboKey1, "This is a required field!"
   FormErrorFlag = True
Else
End If


'   Key2
Me.cboKey2.BackColor = FormNoErrorColor
If Me.cboKey2.Text = "" Then
   SetErrorField Me.cboKey2, "This is a required field!"
   FormErrorFlag = True
Else
End If


'   Check that foreign keys are compatible, and unique where necessary


'   Check relationships between fields

End Function

I've tried to attach the whole spreadsheet for anyone who is kind enough to dig deeper, but couldn't :(. However I hope that this will be enough to see what is going on. The various trace functions are available in the uploaded spreadsheet, and operateas might be expected.

When I click on the ClearRefresh button while the caption is Refresh, the fields are reloaded, but the error conditions are not cleared. The trace shows a call to LoadObjectIntoForm which seems to come from the Exit handler for cboKey1 (Trace for the entire run is attached). However, there seems to be no click on the Refresh button - the trace shows a click on the same button after the caption has reverted to Clear.

I've lost an event which I know I caused, and I can't tell where to find it! Any expert in events able to help?

Here's the complete trace for this run:
Code:
2015-06-29 17:45:07 TraceInitEV      6-21.EVTRACE.txt 1                0                
2015-06-29 17:45:07 SetFormAction    Undefined        
2015-06-29 17:45:07 ClearFormFields  
2015-06-29 17:45:07 ClearFormFields  ModelForm        
2015-06-29 17:45:07 ClearStatusLine  
2015-06-29 17:45:08 63908.9          ModelForm        cboKey1          Change           True             a                
2015-06-29 17:45:08 63908.9          ModelForm        cboKey1          Change           False            A                
2015-06-29 17:45:08 SetFormAction    Seek             1                A                
2015-06-29 17:45:09 63909.1          ModelForm        cboKey1          Change           True             Ab               
2015-06-29 17:45:09 63909.3          ModelForm        cboKey1          Change           True             Abc              
2015-06-29 17:45:09 63909.5          ModelForm        cboKey1          Change           True             Abc              
2015-06-29 17:45:09 63909.7          ModelForm        cboKey1          Change           True             Abc d            
2015-06-29 17:45:09 63909.7          ModelForm        cboKey1          Change           False            Abc D            
2015-06-29 17:45:09 63909.8          ModelForm        cboKey1          Change           True             Abc De           
2015-06-29 17:45:09 63909.9          ModelForm        cboKey1          Change           True             Abc Def          
2015-06-29 17:45:10 63910.6          ModelForm        cboKey1          AfterUpdate      True             
2015-06-29 17:45:10 63910.6          ModelForm        cboKey1          Exit             True             
2015-06-29 17:45:10 SetFormAction    Seek             
2015-06-29 17:45:10 FindClassEntry   1                Abc Def          
2015-06-29 17:45:10 FindClassEntry   Abc Def          True             
2015-06-29 17:45:10 FindClassEntry   $A$2             
2015-06-29 17:45:10 LoadEntryIntoObject              2                
2015-06-29 17:45:10 SetFormAction    View             
2015-06-29 17:45:10 LoadObjectIntoForm               1                Abc Def          False            
2015-06-29 17:45:10 63910.6          ModelForm        cboKey2          Change           False            First Value      
2015-06-29 17:45:10 63910.6          ModelForm        txtField1        Change           False            ABC              
2015-06-29 17:45:10 63910.6          ModelForm        txtField2        Change           False            unformatted text 
2015-06-29 17:45:10 ConfigureFormForAction           -1               1                Abc Def          
2015-06-29 17:45:10 63910.6          ModelForm        cboKey2          AfterUpdate      True             
2015-06-29 17:45:13 63913.7          ModelForm        cboKey2          Change           True             
2015-06-29 17:45:13 SetFormAction    Edit             
2015-06-29 17:45:14 63915.0          ModelForm        cboKey2          AfterUpdate      True             
2015-06-29 17:45:14 63915.0          ModelForm        cboKey2          Exit             True             
2015-06-29 17:45:16 63916.6          ModelForm        txtField1        Change           True             a                
2015-06-29 17:45:16 63916.9          ModelForm        txtField1        AfterUpdate      True             
2015-06-29 17:45:16 63916.9          ModelForm        txtField1        Exit             True             
2015-06-29 17:45:22 63922.7          ModelForm        txtField2        Exit             True             
2015-06-29 17:45:22 63922.7          ModelForm        cmdAction        Click            True             Edit             
2015-06-29 17:45:22 ValidateFormFields               Edit             1                Abc Def          
2015-06-29 17:45:22 SetErrorField    cboKey2           required field! 
2015-06-29 17:45:22 63922.7          ModelForm        cboKey2          Exit             True   

<<here is="" where="" i="" created,="" would="" expect="" to="" see,="" the="" click="" on="" cmdclearrefresh="" with="" caption="" of="" refresh="">>

          
2015-06-29 17:46:02 63962.0          ModelForm        cboKey1          Exit             True             
2015-06-29 17:46:02 SetFormAction    Seek             
2015-06-29 17:46:02 FindClassEntry   1                Abc Def          
2015-06-29 17:46:02 FindClassEntry   Abc Def          True             
2015-06-29 17:46:02 FindClassEntry   $A$2             
2015-06-29 17:46:02 LoadEntryIntoObject              2                
2015-06-29 17:46:02 SetFormAction    View             
2015-06-29 17:46:02 LoadObjectIntoForm               1                Abc Def          False            
2015-06-29 17:46:02 63962.0          ModelForm        cboKey2          Change           False            First Value      
2015-06-29 17:46:02 63962.0          ModelForm        txtField1        Change           False            ABC              
2015-06-29 17:46:02 ConfigureFormForAction           -1               1                Abc Def          
2015-06-29 17:46:07 63967.7          ModelForm        cmdExit          Click            True
</here>
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Solved!

I had another button that was rewsetting the focus to the first key field. When I exited this field it re-executed the search for the key value (which had not changed), thus reloading the underlying data fields. This is exactly what the Refresh button is meant to do, except that the Refresh button also resets the error notifications.

So - there was no 'missing' event, it as a different event - properly captured and traced, that did similar work.

Isn't event handling fun!

Tony
 
Upvote 0

Forum statistics

Threads
1,222,902
Messages
6,168,938
Members
452,227
Latest member
sam1121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top