Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SetFocus & Validate event 2

Status
Not open for further replies.

sunaj

Technical User
Feb 13, 2001
1,474
0
0
DK
Why does this not work?

--------------------------------------------
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then Command1.SetFocus
End Sub

Private Sub Text1_Validate(Cancel As Boolean)
MsgBox "Validate"
End Sub
---------------------------------------------

-it works if I use the mouse to give focus to command1
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
I think that using the SetFocus command bypasses the Validate event. i.e. it doesn't fire when focus is changed explicitly with a SetFocus command, only when the user changes the focus with the mouse or the Tab key. gkrogers
 
Is the .CausesValidation property of the Command1 Button set to True? Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
yes. Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
This does not have anything to do with a command button, or the CauseValidation property directly.

Setting the focus to any other control in it's Key event when any keycode is used will cause the validation event not to fire. Only the LostFocus will then fire. [/b][/i][/u][sub]*******************************************************
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!
 
So how do I solve the problem of validating the text in the textbox and allowing the user to end the typing by pressing 'enter' ?

Answer:
--------------------------------------------
Dim FromEdit As Boolean

Private Sub Form_Load()
FromEdit = False
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then FromEdit = True: SendKeys ("{TAB}")
End Sub

Private Sub Text1_LostFocus()
If FromEdit Then
FromEdit = False
Command1.SetFocus
End If
End Sub

Private Sub Text1_Validate(Cancel As Boolean)
MsgBox "Validate"
End Sub
---------------------------------------------
-but not very nice....

Extra info:
I'm editing the values in a grid by positioning a textbox over the cell. The Text1_lostfocus event is used to hide the textbox when the user is finished editing the cell. Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Can't you just do something like:
[tt]
Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
Text2_Validate False
Command1.SetFocus
End If
End Sub
 
Now, why didn't I think of that combination?
I must be getting old..... Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Hang on -

What if I cancel=true in the validate event?
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
Text2_Validate True
End If
End Sub

Private Sub Text2_Validate(Cancel As Boolean)
MsgBox "Validate"

' Ok, do your check and set Cancel appropriately
' Validation code goes here


If Not Cancel Then
Command1.SetFocus
End If
End Sub
 
I should point out that I changed the Cancel parameter in the call in Text2_KeyDown to True just to demonstrate the principal. Normally, as in the first example, I'd expect it to be False in the call.
 
hhmm yes but that has the down side that if the user clicks on a different control (not command1) with the mouse the focus will shift to command1 - which will seem a little strange...
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
hey what happened to that 'Default' (if I am not wrong) Property of Command Buttons... Think this will solve it .. If what you are trying to achieve is to Set the focus on Command so that it is easy for the user to click it!!..
 
Cor, whaddya want? Blood?

Right, bearing in mind that parameters are passed by default, we can do the following:

Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
Dim CancelTest As Boolean
If KeyCode = vbKeyReturn Then
CancelTest = False
Text2_Validate CancelTest
If Not CancelTest Then
Command1.SetFocus
End If
End If
End Sub

Private Sub Text2_Validate(Cancel As Boolean)
MsgBox "Validate"

' Ok, do your check and set Cancel appropriately
' Validation code goes here

Cancel = False ' Or false. Whatever.

End Sub
 
No. The point is to allow the user to end the typing by pressing 'enter' and at the same time use the _validate event to validate the entered data. Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Down boy, down!

Strongm you have now earned the star I gave you yesterday. Thanks.
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
sunaj: I have done this before using both ways. The SendKeys seems to work best.
Calling the Validation event works well also.
But, if you need to cancel the event, then you need to also cancel the set focus.
The easiest way to do this is use a form level boolean, setting it to true if the validation fails.

To implemet this so that it does not get confused with other things which have cause the Validation event to fire, set the boolean to true before calling the event, and in the event set it to false if validation passed.
Once the event is called, and before calling the SetFocus, check if the boolean is True - if so, then do not call the SetFocus method.

Using strongm's example:
Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
m_bCancel = True
Text2_Validate False
If Not m_bCancel Then Command1.SetFocus
m_bCancel = False
End If
End Sub

Private Sub Text2_Validate(Cancel As Boolean)

'Validating code
'Did not Pass
Cancel = True

m_bCancel = Cancel
End Sub
[/b][/i][/u][sub]*******************************************************
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!
 
Darn it strongm, you were faster on the draw! [/b][/i][/u][sub]*******************************************************
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!
 
[thumbsup2]

Mind you it's getting harder to earn stars around here...
 
But, actually I posted and older method of mine.
Looking at some other code in long lost proceedures I also pass the Boolean to the Validate event (by reference you meant to say?).

That's again the age symptom. Or the beer... [/b][/i][/u][sub]*******************************************************
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!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top