Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

Lost Focus and validation

I have made my OK button the default. When the user types data into one of the controls and presses ENTER then the enter click is fired before the previous control's lost focus. I have played around with the validate event and the CausesValidation property of the controls but dont seem to be able to force lost focus to execute before the OK button click. I need to do some
validation between other controls before the OK may execute.


Any ideas ?
0
chill
Asked:
chill
  • 5
  • 5
  • 2
  • +2
1 Solution
 
mcriderCommented:
Well you could always put validation code into the OK button's Click Event.  If the validation fails, just exit the sub...


Cheers!®©
0
 
mcriderCommented:
Something like this:

Private Sub Command1_Click()
    If Text1.Text = "" Then
        MsgBox "Fill In your name!"
        Text1.SetFocus
        Exit Sub
    End If
    'Finish processing OK CLICK...
End Sub



Cheers!®©
0
 
mark2150Commented:
mcriders method does work, but it's sloppy.

Set the [Ok] buttons .enabled property to False so its normally not on. Set the forms .KeyPreview property to True and then put a check in your forms KeyUp routine to see if the [Ok] button should be enabled. Don't use the Lost_Focus event at all.

Using mcriders example:

Form_KeyUp
  btn_ok.enabled = (len(text1.text) > 0)
End Sub

In this way you have a single point of control and you don't mislead the operator into thinking the button is active until the form has met all of the conditions. This is cleaner as the [Ok] button will only activate when it is ready to go.

In mcriders version you're effectively lying to the operator in that the [Ok] is on all the time even when it's NOT ready. You will annoy the users if you have this "false ready & nag" kind of logic.

M
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sanjay4uCommented:
Private Sub Command1_Click()

  'Call the validation function where
  'u  have all ur validations
  If Not Validate() Then
    Exit Sub
  End If

End Sub
0
 
mcriderCommented:
mark2150, There's nothing sloppy about my code. As for a single point of code, the default command button would be the single point of control.  Also, performing validation your way can slow down keystroke processing if there is alot of validation going on, since you are validating on every keystroke.  Your way can give the user the appearance of jerky/lagging text.
0
 
mark2150Commented:
sanjay4u,

Welcome to E-E!

But as a new member you need to know something about the protocol here on E-E.

You should refrain from posting "answers". Post everything as "comments" and the questioner will award points by using the "Accept comment as answer" feature. The only time the "Answer" button should be used is when the question is "Points for sanjay4u" or some variant.

Clicking the "answer" button moves the question from the "Awaiting answers list" to the "Locked" list and fewer experts will see the question and comment upon it. This does a disservice to the questioner.

Additionally unless your "answer" is definitive and absolutely correct, using "answer" is not appropriate. Your "answer" in this case is not obviously superior to the other comments posted and therefore "answer" should not have been used.

Please change your "answer" to a "comment" to allow the questioner to get additional opinions on this matter.

Again, welcome to E-E!

M
0
 
mcriderCommented:
sanjay4u,

I see you are new to EE.  Welcome... There are a few ground rules that EE Experts play by. Calling/performing the validation in the command click code was already suggested by me.  Posting another experts comment as an answer can get your account suspended.

Please read the thread before posting to it.


Again, Welcome!
0
 
mark2150Commented:
Mcrider,

giving a false [Ok] state is poor practice no matter how you justify it.

My example shows a *one* statement validation. I use this method a *lot* with slow machines and there is no noticable lag. Actually the system appears "snappy" as the button lights up immediately when the validation is satisfied and the operator receives a cue that they should continue. Conversely having the button greyed lets them know they're not done on the page yet - with no naggy message boxes popping.

Have seen examples of your type of solution where [Ok] is enabled all the time but isn't really. Operator clicks, "Fill this in". Operator fills in indicated field and trys again - "Fill that in". They fill that and try again - "Fill something else in". You can almost hear the teeth gritting. Less keystrokes for operator (no message boxes to reply to) and much cleaner if button doesn't light up until it is ready to be clicked on. Human factors - makes a *huge* difference in how program is perceived from "ease of use" standpoint.

You code your way, I'll code mine. But your example is "poor practice" in my shop - none of my staff are allowed to code that way.

M
0
 
mcriderCommented:
mark2150, Code a validation your way where you have to do a database table lookup and see how slow your keystrokes are...

There is a time and place for everything.


Cheers!®©
0
 
mark2150Commented:
db lookup doesn't occur in keystroke validator. It occurs in [Ok] button click event so is not impacted. Code in db lookup is simplified as it doesn't have to check for fields being filled in as [Ok]'s click event can't fire until validation is satisfied.

False ready and nag is *bad*.

M
0
 
wsh2Commented:
<offthread>
chill:
Just so you know.. mcrider and mark2150 are married.. <j/k>.. <lol> and a <wink>.
0
 
wsh2Commented:
BTW.. both of them are Excellent Experts.. <smile>.
0
 
chillAuthor Commented:
These are both solutions that no doubt will work. I was under the impression that VB6 had added some features for cross control validation. I was hoping someone would have a solution that used these features.

If my user clicks the OK button then all is well. It is when he presses ENTER on the default control - OK that I have a problem.
0
 
mark2150Commented:
That is why I suggest validation on the keystroke and not enabling the [Ok] until the validation is satisfied. The default [enter] doesn't have an effect if the control isn't enabled.

M
0
 
chillAuthor Commented:
Comment accepted as answer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now