?
Solved

How to use setFocus ?

Posted on 2003-03-22
11
Medium Priority
?
332 Views
Last Modified: 2010-04-07
how come the txtNewIC1.SetFocus is not working ? The focus will still jump to the next textbox ?
--------------
Private Sub txtNewIC_Validate(Cancel As Boolean)
If optMalaysian.Value = True Then


    If Len(txtNewIC) <> 6 Then
    MsgBox "Please enter a valid IC number. ", vbCritical
    txtNewIC1.Enabled = False
    txtNewIC2.Enabled = False
    txtNewIC.Text = "######"
    txtNewIC.SetFocus
    Else
    txtNewIC1.Enabled = True
    txtNewIC1.SetFocus <-- error line here
    End If
                 
End If
End Sub
0
Comment
Question by:vb_newbie
[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
  • 2
  • +4
11 Comments
 
LVL 3

Expert Comment

by:Shaka913
ID: 8187280
The reason this doesn't work, is the validate event wont fire if you dont "leave" the textbox... i recommend doing this. on the keypress event of the NewIC set NewIC1 to enabled, then let validate disable it when you "validate"
0
 
LVL 3

Expert Comment

by:Shaka913
ID: 8187292
Here is the code...

Private Sub txtNewIC_KeyDown(KeyCode As Integer, Shift As Integer)
    txtNewIC1.Enabled = True
End Sub

Private Sub txtNewIC_Validate(Cancel As Boolean)



   If Len(txtNewIC) <> 6 Then
    MsgBox "Please enter a valid IC number. ", vbCritical
    txtNewIC1.Enabled = False
    txtNewIC2.Enabled = False
    txtNewIC.Text = "######"
    Cancel = True
   Else
    txtNewIC1.SetFocus '<-- error line here
    Cancel = False
   End If
               

End Sub
0
 

Author Comment

by:vb_newbie
ID: 8187363
Or another easier example for easier understanding ..
---------------------
Private Sub Text1_Validate(Cancel As Boolean)
If Text1 = "" Then
MsgBox " Pls enter string in text1"
Text1.SetFocus  <--- not working
End If
End Sub


Private Sub Text2_Validate(Cancel As Boolean)
If Text2 = "" Then
MsgBox " Pls enter string in text2"
Text2.SetFocus <--- not working
End If
End Sub

---------------------

if you click on text2 , though it'll popup the error msg for not entering any string in text1 , but the focus will still point to text2
0
Technology Partners: 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!

 

Author Comment

by:vb_newbie
ID: 8187370
Shaka913 , what i mean is the setFocus will not go back to the box which has error .. u got what i mean ?
0
 
LVL 1

Accepted Solution

by:
GERTJAN earned 40 total points
ID: 8187430
The SetFocus is maybe not working for one of the following reasons:

You can't move the focus to a form or control if the Enabled property is set to False. If the Enabled property has been set to False atdesign time, you must first set it to True before it can receive the focus using the SetFocus method.

You also can only move the focus to a visible form or control. Because a form and controls on a form aren't visible until the form's Load event has finished, you can't use the SetFocus method to move the focus to the form being loaded in its own Load event unless you first use the Show method to show the form before the Form_Load event procedure is finished.
0
 
LVL 3

Expert Comment

by:Shaka913
ID: 8187474
vb_newbie,
The code i showed you does what you want, if there is an error, it stays in the control with the error, if no error, allows the tab to move you to next area. you dont use setfocus inside of validate, you set cancel = true (to keep focus on control) or set Cancel = false (to allow the move to happen)

Hope this helps
0
 
LVL 6

Expert Comment

by:GPrentice00
ID: 8187526
Why not check your other posting that actually had the answer to your example?
0
 

Expert Comment

by:papmech
ID: 8189141
You can't set focus to a textbox that has not been enabled. You have to first enable the textbox then setfocus to it. Hope this helps.

Jon.
0
 

Expert Comment

by:archekrypt
ID: 8216332
the text box isn't enabled... try that!!!!!!!
0
 

Expert Comment

by:archekrypt
ID: 8216383
yeah!
i agree with ol' papmech ova here!

a object HAS to be ENABLED for it to see selected, or the focus to be set onto it!!!!!!!!!!
0
 

Expert Comment

by:torrid333
ID: 13778837
For textboxes that are ENABLED at design time... we have several in different Tabs... if we SetFocus to a box on a different tab it is not visible and crashes the program... what code can we use to determine if the text box is capable of accepting focus?

I suppose I could check the tab number but would prefer something similar to checking if it was enabled... something that is the same for all setfocus calls...  any ideas?
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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
Course of the Month10 days, 11 hours left to enroll

765 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