Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Making a field required with Set focus

Posted on 1999-07-12
6
Medium Priority
?
153 Views
Last Modified: 2010-05-02
I want to make sure the use of my app enters properly formatted data into a text box.  The code I have does that, but when the user moves away form the box the set focus command is still active.

How can I get code the set focus to stop if the user decided to move to another part of my app?

Below is the code I am using.


Many Thanks in advance Kevin!



Private Sub TXTMBRNUMBER_LostFocus()
msg = "The Member Number must be exactly 10 digits in length"     ' Define message.
Style = vbOKOnly                      'Define buttons.
mylen = Len(TXTMBRNUMBER.Text)        'find length of string

If mylen > 11 Then           ' checks length of string
response = MsgBox(msg, Style, "Invalid Text Entry")
TXTMBRNUMBER.Text = " "
TXTMBRNUMBER.SetFocus
Exit Sub
End If
If mylen < 10 Then          ' checks length of string
response = MsgBox(msg, Style, "Invalid Text Entry")
TXTMBRNUMBER.Text = " "
TXTMBRNUMBER.SetFocus
Exit Sub
End If
0
Comment
Question by:kpt112
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 10

Expert Comment

by:caraf_g
ID: 1523767
Instead of in the LostFocus procedure, why not code this validation in the (for example) click of the OK button? (It is quite irritating to have messages pop up every time you leave a control). Anyway - by doing that you avoid the whole problem.
0
 
LVL 1

Accepted Solution

by:
michelv earned 60 total points
ID: 1523768
Instead of using the lostfocus event, add an OK button and check all fields for correct data after a user pushes the OK button. That's how I always do it...

so like this:

Private Sub Command1_Click() 'This is the OK Button
  if CheckFields then
     'do stuff if all fields are correct
  endif
End Sub

Private Function CheckFields() As Boolean
  if len(text1) < 10 or len(text1)>10 then
     msgbox "Error"
     text1.SetFocus
  endif
  CheckFields = true
End Function


Hope this helped...
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1523770
michelv, that's eerie! We both had the same idea in the exact same minute :-)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Expert Comment

by:michelv
ID: 1523771
Yeah I saw that to, haha, fun !
Maybe we're interconnected through some kind of interstellar hyperkinetic beam or something *grin*
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1523772
"teedeedeedee teedeedeedee" <theme from "the twilight zone">
0
 

Author Comment

by:kpt112
ID: 1523773
The problem is not with checking the length. The problem is if a user does type the incorrect length I want a message to popup and the focus to be set back to the item. When I use the set focus command it keeps setting the focus until the program blows up.  My text field can be for 10 or 11 digits. I want to know of a way to break out of the set focus if the user goes some where else.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…
Suggested Courses

662 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