My project's tester just found that while my infamous custom controls properly fire their validation methods when he saves data from a toolbar button, the menu command to save a record does NOT fire validation.
As the title says: How do I set a menu command to fire a control's validation event?
I don't think this is possible unless anyone proves me wrong. What you can do is to perform the validations before allowing to save after clicking the menu...
I had forgotten about this until today, when we got a defect report that led to the same issue: Controls that require going through their Validate event do not fire Validate when the user clicks on the menu bar (such as the classic File|Save).
The menu bar was made in VB 6 through the form editor in Visual Studio.
And my latest searches agree with dee-u, there appears to be no way to have the menu cause validation.
There is a click event for each menu item, so I suppose you could use that to trigger the Validate event. There you could set focus to a control which doesn't require validation, such as a button.
Please remember to rate the posts and threads that you find useful.
How can something be both new and improved at the same time?
Returning to this issue one more time, now with a code extract.
Here is my handler:
Code:
'**
'Menu Click Event Handler
Private Sub MnuSave_Click()
On Error Resume Next
'Manually fire validation for the current control
'If validation passes, call ToolAction
SendKeys "{TAB}" 'Does not actually pass focus or fire control validation
Call m_objForm.ToolAction(tbrBntSAVE_REC)
End Sub
The call to SendKeys does not actually move focus to another control.
Would it help if I did something like:
dim ctlTemp as Control
set ctlTemp = Me{form}.ActiveControl
Me.ActiveControl = (some other control -- this line needs work)
if ctlTemp = Me.ActiveControl then
.... do the save processing
end if
1. I was trying to use pointer comparisons in VB6; I can't do an equality comparison between control objects directly. I'll have to use control.Name and control.Index instead.
2. The line I tagged as needing work needs work. ActiveControl is read-only, and I didn't find a quick way to manipulate the Controls collection to move focus. Any suggestions?
Try the SetFocus method for a control instead of SendKeys. Or you can change the focus via API.
Try evaluating objects with the Is operator instead of the equals sign.
BTW, top level menus have a click event too, so you can use one event to do the change in focus for all the items in that menu's list. You could also make a sub to pass focus, and simply call it from whatever menu click events you need.
Please remember to rate the posts and threads that you find useful.
How can something be both new and improved at the same time?
The SendKeys didn't work. Here's the current code:
Code:
'**
'Menu Click Event Handler
Private Sub MnuSave_Click()
'On Error Resume Next
'Manually fire validation for the current control
'If validation passes, call ToolAction
Dim ctlTemp As Control
Dim ctlTemp2 As Control
Set ctlTemp = Me.ActiveControl
SendKeys "{TAB}"
Set ctlTemp2 = Me.ActiveControl
If Not (ctlTemp Is ctlTemp2) Then
Call m_objForm.ToolAction(tbrBntSAVE_REC)
End If
Set ctlTemp = Nothing
Set ctlTemp2 = Nothing
End Sub
I tested this and made no changes to data, so validation would have passed. I set my breakpoint on the IF statement, and found that ctlTemp and ctlTemp2 were pointing to the same control.
Bookmarks