Solved

Lost Focus and validation

Posted on 2000-05-11
15
287 Views
Last Modified: 2010-05-02
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
Comment
Question by:chill
  • 5
  • 5
  • 2
  • +2
15 Comments
 
LVL 14

Expert Comment

by:mcrider
ID: 2799705
Well you could always put validation code into the OK button's Click Event.  If the validation fails, just exit the sub...


Cheers!®©
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2799716
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
 
LVL 12

Accepted Solution

by:
mark2150 earned 30 total points
ID: 2799906
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Expert Comment

by:sanjay4u
ID: 2800011
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
 
LVL 14

Expert Comment

by:mcrider
ID: 2800043
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
 
LVL 12

Expert Comment

by:mark2150
ID: 2800044
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
 
LVL 14

Expert Comment

by:mcrider
ID: 2800063
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
 
LVL 12

Expert Comment

by:mark2150
ID: 2800085
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
 
LVL 14

Expert Comment

by:mcrider
ID: 2800156
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
 
LVL 12

Expert Comment

by:mark2150
ID: 2800311
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
 
LVL 14

Expert Comment

by:wsh2
ID: 2800727
<offthread>
chill:
Just so you know.. mcrider and mark2150 are married.. <j/k>.. <lol> and a <wink>.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2800732
BTW.. both of them are Excellent Experts.. <smile>.
0
 

Author Comment

by:chill
ID: 2803263
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
 
LVL 12

Expert Comment

by:mark2150
ID: 2804109
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
 

Author Comment

by:chill
ID: 2910308
Comment accepted as answer
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question