PDA

View Full Version : Solved: Placing cursor in a specific textbox when form opens



GlennLee
06-18-2005, 04:32 PM
Hi All.

I simply want to ensure that a particular textbox has the focus and the cursor when a small pop-up VBA userform opens. I have tried to use PartNumberBox.setfocus but it doesn't seem to work. ("PartNumberBox" is the real name of the text box)
I have also tried some other items I found in other websites to no avail.:banghead:

Jacob Hilderbrand
06-18-2005, 04:39 PM
Me.TextBox1.SetFocus

This should work fine. Did you put the code on the UserForm Initialize Sub?

GlennLee
06-18-2005, 04:43 PM
I placed PartNumberBox.SetFocus immediately after the form.show command. This was in the first sub called from the main routine.

Do I need to use the "me." ?

Regards,

Glenn

GlennLee
06-18-2005, 04:52 PM
I did try your solution but to no avail. Thank you anyway.

Jacob Hilderbrand
06-18-2005, 05:05 PM
You need to put it in the Initialize Sub for the UserForm. Go to the UserForm code and select UserForm from the left drop-down and Initialize from the right. Put the code in that macro.

Norie
06-18-2005, 05:18 PM
What about using the tab order?

You can access that by right clicking on the form while in Design mode.

GlennLee
06-18-2005, 05:20 PM
I have it set to tab index "0" already. Thank you.

GlennLee
06-18-2005, 05:21 PM
Done. It still does not work. Do I need anything else in the sub with it?

Jacob Hilderbrand
06-18-2005, 05:24 PM
Can you post an attachment of what you are doing?

Norie
06-18-2005, 05:25 PM
Glenn

I edited my post, did you try using the Tab Order suggestion?

GlennLee
06-18-2005, 05:29 PM
Hi. I created the following sub:

Private Sub UserForm_Initialize()

OpDataEntryForm1.PartNumberBox.SelStart = 0
OpDataEntryForm1.PartNumberBox.SelLength = 0
OpDataEntryForm1.PartNumberBox.SetFocus

End Sub

Any other ideas?

Regards,
Glenn

Norie
06-18-2005, 05:29 PM
Glenn

I never posted any code.

GlennLee
06-18-2005, 05:30 PM
Yes. I had the textbox set to TabOrder "0". Thanks.

Norie
06-18-2005, 05:36 PM
Glenn

There isn't a TabOrder property.

I mean right click on the form in Design mode and then select Tab Order.

GlennLee
06-18-2005, 05:37 PM
Hi. Jake. I posted the sub in another reply. Further information: I do not show the form until I qualify an input bit and a static variable. I then clear the text boxes and show the form. I have to sign off from my desktop and sign in on my actual development system. I will be back shortly

Thank you,

Glenn

GlennLee
06-18-2005, 05:40 PM
I did that already. When I click on the tesxtbox and look at the taborder, it is zero.

Thanks

Glenn

GlennLee
06-18-2005, 05:41 PM
Public Sub OpDataEntry1()

Dim LoadOcyd1 As Integer
Dim testcell As Variant 'Test for bad data in spreadsheet cells

testcell = Worksheets("Operator").Range("BR14") 'ILoad station #1 Sensor
If CStr(testcell) = "Error 2042" Then 'Com Error
Exit Sub
ElseIf CStr(testcell) = "Error 2023" Then 'Application Error
Exit Sub
End If

LoadOcyd1 = Worksheets("Operator").Range("BR14") 'Load station #1 Sensor

If LoadOcyd1 = 0 Then 'When the Load Station sensor goes off, cancel keep

test1 = 0 'Clear the bit declaring that the pop-up already displayed

OpDataEntryForm1.Hide

Exit Sub

End If

If test1 = 1 Then

Exit Sub

End If

If LoadOcyd1 = 1 Then

If ReuseData1 = False Then

PartNumberBox.Value = ""
PnValid1 = False

NumberOfPanels.Value = ""
NumPnls1 = False

OpInitials.Value = ""
Initials1 = False

CommentBox.Value = ""

SendToPLC.Enabled = False

End If

If ReuseData1 = True Then ' Copy saved values to the load station

Worksheets("Operator").Range("AA16") = Worksheets("Operator").Range("T16")
Worksheets("Operator").Range("AB16") = Worksheets("Operator").Range("U16")
Worksheets("Operator").Range("AC16") = Worksheets("Operator").Range("V16")
Worksheets("Operator").Range("AD16") = Worksheets("Operator").Range("W16")

SendToPLC.Enabled = True

End If

End If

OpDataEntryForm1.Show

test1 = 1 'This tells us that the form was already revealed for this load

'PartNumberBox.SelStart = 0
'PartNumberBox.SelLength = 0
'Me.PartNumberBox.SetFocus

End Sub
Private Sub UserForm_Initialize()

OpDataEntryForm1.PartNumberBox.SelStart = 0
OpDataEntryForm1.PartNumberBox.SelLength = 0
OpDataEntryForm1.PartNumberBox.SetFocus

End Sub

Norie
06-18-2005, 05:45 PM
Glenn

Maybe I'm missing something but as far as I know a textbox doesn't have a TabOrder property.

Try right clicking on the form not the textbox.

You should then get an option Tab Order.

And when you select that you should be able to change the tab order of all the controls on the userform.

GlennLee
06-18-2005, 05:52 PM
Thank you for showing me that feature. I did the right-click and selected tab order. It IS in the correct order with the textbox I wanted at the top of the list.

Thank you.

What I was saying was clicking on the textbox and looking at the properties, there is a "TabIndex" property, which I set to "0". I checked all of the textboxes (4) and they are all in the correct order.

Glenn

GlennLee
06-18-2005, 07:31 PM
I did NOT yet solve this, but I will try something else. Thank you to both people that tried to help.

Regards,

Glenn

johnske
06-19-2005, 04:16 AM
Hi Glenn,

Make sure you put this into the UserForm code module then try it out:
Option Explicit
Private Sub UserForm_Activate()
PartNumberBox.SetFocus
End Sub

HTH,
John :)

sheeeng
06-19-2005, 07:29 AM
Hi Glenn,

Make sure you put this into the UserForm code module then try it out:
Option Explicit
Private Sub UserForm_Activate()
PartNumberBox.SetFocus
End Sub

HTH,
John :)
Yup, this code is what you can use. Gleen, have a try. I tried it, it works. :thumb

GlennLee
06-19-2005, 09:14 AM
Hi John.

Thank you for taking the time to reply to my problem. I tried you offering and it still does not work. I must be doing something else that is either moving the focus or preventing it. I have moved the .SetFocus command to several places to no avail. I have even tried several places at once, also to no avail.

The focus almost always is in the correct spot when I first start the application. When the pop-up opens the second time, the focus is gone and I have to click in the box to set the focus. I have set the tab order (the desired textbox is set to zero) The whole thing is too complicated to go into in this venue.

Again, Thank you for your input. I have spent too much time trying to solve this minor problem, and it is not that important. I am closing this chapter and moving on. Thank you

Warmest Regards,

Glenn

Norie
06-19-2005, 09:20 AM
Glenn

When you say the pop-up opens for a second time do you mean that you are hiding then showing it.

If that is what you are doing I think the control that will have focus will be the one that had focus just before you hide the form.

GlennLee
06-20-2005, 05:24 AM
Hi. True, the pop up occurs when an external event happens, and can happen at any time.I wonder how to "Shift the focus" to the pop-up? I have tried UserForm.Activate then PartNumberBox.SetFocus, but that hasn't worked. I have also tried to .SetFocus at various points in the initial sub that is called when the external event happens, to no avail.

I have tried UserForm.Initialize as a sub.

HEY! I wonder if I DIM PartNumberBox at the top of the code under OPTION EXPLICIT if that would make a difference? PartNumberBox is the NAME of the TextBox in the popup. Also, the underlying screen remains visible.

Your thoughts?

Regards,

Glenn

johnske
06-20-2005, 06:19 AM
If you're showing a second userform after the first the focus will shift to the 2nd one, so you also need a userForm_Activate in the 2nd userform to set the focus back to the first userform e.g.Option Explicit

Private Sub UserForm_Activate()
'this code in userform2
UserForm1.PartNumberBox.SetFocus
End Sub

GlennLee
11-01-2005, 03:33 PM
Thank you for replying. I put that project to bed a while ago. But, beacuase you took the time to reply, I have saved your reply should I need to deal wuth that code ever again.

Thank you.

Warm Regards,

Glenn:hi: :thumb :friends: