Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default SetFocus Problems

The following example has Userform1 in which the following objects
have been placed in the order listed:
TextBox1
TextBox2
CommandButton1
CommandButton2

In Module1 is the following code:

Sub Auto_Open()
Sheets("sheet1").Select
UserForm1.Show
End Sub

In the Userform1 Code sheet is the following Code:

Private Sub UserForm_Initialize()
CommandButton1.TakeFocusOnClick = False
CommandButton2.TakeFocusOnClick = False
Set U1 = UserForm1
Set U1T1 = UserForm1.TextBox1
Set U1T2 = UserForm1.TextBox2

End Sub


Private Sub CommandButton1_Click()
If U1T1 = Empty Then
U1.Hide
MsgBox "Data must be entered in the First TextBox"
U1T1.SetFocus
U1.Show
End If

If IsNumeric(U1T1) = False Then
U1.Hide
MsgBox "The data entered in the first TextBox must be numeric"
U1T1 = Empty
U1T1.SetFocus
U1.Show
End If

If U1T2 = Empty Then
U1.Hide
MsgBox "Data must be entered in the Second TextBox."
U1T2.SetFocus
U1.Show
End If

If IsNumeric(U1T2) = False Then
U1.Hide
MsgBox "Only Numeric Data may be entered in the second TextBox"
U1T2 = Empty
U1T2.SetFocus
U1.Show
End If

U1.Hide

With Sheets("Sheet1")
..[a1] = U1T1
..[a2] = U1T2
End With

MsgBox "Click CommandButton_1 to Continue or CommandButton_2 to
Cancel"

Sheets("Sheet1").[a1] = Empty
Sheets("Sheet1").[a2] = Empty
U1T1 = Empty
U1T2 = Empty

Auto_Open

End Sub

When the Macro commences Userform1 appears with the focus on textbox1.
At this point I can immediately commence entering data in TextBox1.
However if I enter text or no data into textbox1 and click
CommandButton1, Userform1 hides and a message box is displayed that I
have to close by clicking OK to continue. When I click OK, the
message box is closed and Userform1 is displayed but textbox1 has lost
the focus and to get the focus back to textbox1 I need to use the
mouse, tab or arrow keys. If, upon opening the file, I enter numeric
data in textbox1 and then click CommandButton1 I get a message box
telling me that I must enter data in textbox2 and to continue I must
close the message box by clicking OK. When the Userform1 reappears
there is no focus on textbox2 and to continue I again need to use the
mouse, tab or arrow keys to regain focus. Can someone please tell me
how to make the relevant textbox automatically have the focus when
userform1 reappears after I activate the message box OK button? Any
help would really be appreciated. I have spent hours trying to solve
this issue. If it is any help this program works perfectly on my work
NT platform but not on my Home XP or windows 98 platform. Office 2000
has been installed on each of these platforms.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default SetFocus Problems

Zane,

Following doenst work as you expect

If U1T1 = Empty Then
U1.Hide
MsgBox "Data must be entered in the First TextBox"


it tests the VARIANT U1T1, not the control it points to.
(you've set the VARIANT to an object, thus it's no longer empty)


try

if U1T1.Text="" then


TIP1:
in the VBE make sure the LOCALS window is visible.
Set a few breakpoints in your PROC (with F9)
and fire up your form.

It will stop in breakmode..
and now your LOCALS windows shows all your variables and objects and
their values and properties.



TIP2:
DECLARE your variables. and dont depend on implicit declarations.
You'll find that you make less typo's. and when declared as proper
datatypes, your coding will be easier cause the VBE's intellisense
will popup all appropriate properties for the variables you're working
with.

put OPTION EXPLICIT in the first lines of your code, and start of with
just DIM all your variables as variants. Then learn to declare them in
their proper data types.

suc6


keepITcool

< email : keepitcool chello nl (with @ and .)
< homepage: http://members.chello.nl/keepitcool


(Zane Greer) wrote:

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
SetFocus on a different sheet in VBA AaronC Excel Discussion (Misc queries) 1 June 9th 05 11:18 PM
TextBox SetFocus Problem Tom Ogilvy Excel Programming 1 September 12th 03 01:27 PM
SetFocus problem RFraley Excel Programming 2 September 7th 03 01:18 PM
SetFocus method in VBA Robin S. Excel Programming 2 July 16th 03 10:20 PM
TextBox.SetFocus steve Excel Programming 4 July 16th 03 07:40 PM


All times are GMT +1. The time now is 10:16 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"