Solved

Lost Focus and validation

Posted on 2000-05-11
15
286 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Expert Comment

by:sanjay4u
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 12

Expert Comment

by:mark2150
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
<offthread>
chill:
Just so you know.. mcrider and mark2150 are married.. <j/k>.. <lol> and a <wink>.
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
BTW.. both of them are Excellent Experts.. <smile>.
0
 

Author Comment

by:chill
Comment Utility
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
Comment Utility
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
Comment Utility
Comment accepted as answer
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now