?
Solved

How to use setFocus ?

Posted on 2003-03-22
11
Medium Priority
?
343 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
  • 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
Independent Software Vendors: 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

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

621 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