[RESOLVED] Can a menu bar cause validation?
Hello again,
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?
Re: Can a menu bar cause validation?
Should we guess what you've done in the control and menu? How about some code?
Code:
' and ALWAYS
' remember
' code
' tags
Re: Can a menu bar cause validation?
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...
Re: Can a menu bar cause validation?
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.
Re: Can a menu bar 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.
Re: Can a menu bar cause validation?
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
Re: Can a menu bar cause validation?
How about trying it out and perhaps share us the results?
Re: Can a menu bar cause validation?
I gave it a shot and had two issues.
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?
Re: Can a menu bar cause validation?
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.
Re: Can a menu bar cause validation?
Quote:
Originally Posted by
WizBang
Try the SetFocus method for a control instead of SendKeys. Or you can change the focus via API.
The question is, how do I pick the control to use for ctl.SetFocus?
Edited to add: The Is operator worked properly for the comparison. Thanks; that's part of the issue resolved.
Re: Can a menu bar cause validation?
SPY++ will give you addresses of any contol. It's built into VB6 IDE
Re: Can a menu bar cause validation?
Upon a brief testing your idea of using SendKeys seem to work, what problem are you actually encountering with that?
Re: Can a menu bar cause validation?
dglienna: I want to move focus to the next control in the tab order that can take focus.
dee-u: Maybe the TAB was being thrown off by stepping through code in the debugger. I'll put the breakpoint after it, and try again.
Re: Can a menu bar cause validation?
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.
What can I try next?
Re: Can a menu bar cause validation?
A mere
worked for me. How many controls do you have in your form? Or could you attach a sample form?