>So, CajunCenturion's advise/tip is only one which is absolutely right!
Well, I think I mentioned the same as an option.
But also every one seems to be missing the other mentioned option, which is calling the VB form's ValidateControls method.
That is what it is there for: To cause the last control to fire it's validate event.
>In the form unload... I want to capture the LAST control visited
mbaddoo22:
Move your validation into the control's Validate Event
Then, call the Me.ValidateControls method in the Form_QueryUnload event:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
On Error Resume Next
Me.ValidateControls
If Err.Number Then
Err.Clear
Cancel = True
End If
End Sub
If the code for validation is in the Validate event of the controls which need validation, then calling the mentioned method will validate the last control which has the focus, or which has not lost the focus. All other controls will have already been validated, as long as the CauseValidation property for each control on the form is set to True.
The correct method would then check to see in the UnloadQuery event if the user wants to save the changes.
This requires of course a class level variable to identify if any changes have been made:
Private m_bPossibleSave As Boolean
This variable is set to true in the Change event of each control which changes data.
Then, in the QueryUnload event, this variable is checked.
If True, the user is asked if they want to save the data.
If yes, the ValidateControls method is called. Note that no seperate proceedure is needed here.
If the ValidateControls returns an error, then the Cancel boolean variable in some controls Validate event was set to True.
The next step, if an error was returned, would be to Cancel the Unload method.
If the ValidateControls method does not return an error, then the Save method is called.
The LostFocus event probably should be left alone, with respect to validation.
If you want the last control with the focus to get validated, then, again, you just call the VB form method ValidateControls.
Now, the last thing that is missing, is the fact that same controls could have invalid data as retrieved from the database (NULL data, etc.).
Here is a bit of an outline on how to expand on this idea. Just drop this into a from and add a textbox called Text1:
Option Explicit
Private m_bPossibleSave As Boolean
Private m_bLoadingData As Boolean 'Used such as when loading the form and data the first time.
'Called from the MDI form
Public Sub Display()
LoadData
Me.Show
End Sub
Private Sub LoadData()
m_bLoadingData = True
'Get data from Db or set default values
Text1.Text = "0"
DoEvents 'make sure control events have had time to take place
m_bLoadingData = False
End Sub
Private Sub Text1_Validate(Cancel As Boolean)
If Not IsNumeric(Text1.Text) Then
MsgBox "You must enter a correct value"
Cancel = True
Text1.Text = "0" 'Default value
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
End If
End Sub
Private Sub Text1_Change()
m_bPossibleSave = Not m_bLoadingData
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim Ret As VbMsgBoxResult
If m_bPossibleSave Then
Ret = MsgBox("Do you want to save the changes?", vbYesNoCancel)
Select Case Ret
Case Ret = vbNo
'do nothing
Case vbCancel
'About exiting form
Cancel = True
Case vbYes
On Error Resume Next
Me.ValidateControls
If Err.Number Then
Err.Clear
Cancel = True
Else
'All OK, so save the changes. Call the SaveData function to save the changes.
'If this function returns True, then changes were successful. If False, then an error has occured.
If Not SaveData() Then
'Save was not successful.
Cancel = True
End If
End If
End Select
End If
End Sub
Private Function SaveData() As Boolean
'code to Save
SaveData = True
End Function
[/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!