Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to set the focus to a TextBox control???

Status
Not open for further replies.

ricaforrica

Programmer
Jun 30, 2005
65
PT
Hello,

I want to set the focus to a TextBox control, in a Word macro. How can I make it?

I'm using a _KeyUp event, which detects the Tab key on textbox1, for instance, and focus textbox3. I can do it with:

TextBox3.Activate
or
TextBox3.Select

but when go back to Word and press Tab, it makes the regular document text blink!

Can anyone help me with this issue?
Thanks in advance,

Ricardo Pinto
 
Ah here is the thread. Got lost for a while.

Could you please clarify the textbox scenario. In the previous thread, there was mention of SetFocus. If what you are using is a ActiveX textbox (from the Controls toolbar) there is no SetFocus.

Have you tried having the instruction to .Activate or .Select in a separate Sub, then calling it?

Could you post more code? I have been trying to duplicate the text blinking effect and can not. Please describe in full all the steps. thanks.

Gerry
 
Hello Gerry!

I'm using ActiveX textbox from the Controls toolbar. I tried your suggestion of having the instruction on a different Sub, but it still has the blinking effect!

Here is the code:

Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

'--------------------------------------
' Inicialize controls
'--------------------------------------
Dim IDText As String
IDText = TextBox1.Text

'--------------------------------------
' Checks Tab key
'--------------------------------------

If KeyCode = vbKeyTab Then

Debug.Print TextBox1.Text
'--------------------------------------
' Create and open connection
'--------------------------------------
Dim oCn As New ADODB.Connection
oCn.Open "Driver={Microsoft ODBC for Oracle};" & _
"Server=myserver;" & _
"Uid=user;" & _
"Pwd=pass"
'---------------------------------------
' open recordset
'---------------------------------------
Dim oRs As New ADODB.Recordset
Set oRs = oCn.Execute("SELECT descricao FROM utilizador WHERE lower(utilizador_id) = lower('" & IDText & "')")
If Not oRs.EOF Then
TextBox2.Text = oRs.Fields(0).Value
TextBox3.Activate
Else
TextBox2.Text = ""
MsgBox "Utilizador não existente"
TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)
End If
'---------------------------------------
' cleanup
'---------------------------------------


oCn.Close
Set oRs = Nothing
Set oCn = Nothing

End If
End Sub

The objective of this code is to detect the tab key on textbox1, fill textbox2 with a value from database and put the cursor on textbox3. But the following instruction

TextBox3.Activate (or TextBox3.Select)

makes the text blink 2/3 times! It works great if I remove these instructions...
Do you know any other way of passing the focus?

Thanks for your help!

Ricardo.

Ps: I also have another sub for a button_click event. I post it if you like to simulate all the conditions I have to create the blinking effect:

Private Sub Assinar_Click()

'--------------------------------------
' inicializa as variáveis
'--------------------------------------
Dim id As String
Dim desc As String
Dim pass As String

id = LCase(TextBox1.Text)
desc = LCase(TextBox2.Text)
pass = LCase(TextBox3.Text)

If id = "" Then
MsgBox "Preencha o nome de utilizador, por favor"
Else
If pass = "" Then
MsgBox "Preencha a password, por favor"

Else


'--------------------------------------
' cria a ligação
'--------------------------------------
Dim oCn As New ADODB.Connection
oCn.Open "Driver={Microsoft ODBC for Oracle};" & _
"Server=hsdemo.world;" & _
"Uid=gh_pc;" & _
"Pwd=gh_pc"

'---------------------------------------
' abre o recordset
'---------------------------------------
Dim oRs As New ADODB.Recordset
Debug.Print "SELECT utilizador_pwd FROM utilizador WHERE utilizador_id='" & id & "' AND descricao='" & desc & "'"
Set oRs = oCn.Execute("SELECT lower(utilizador_pwd) FROM utilizador WHERE lower(utilizador_id)=lower('" & id & "')")
If Not oRs.EOF Then
Debug.Print oRs.Fields(0).Value
If oRs.Fields(0).Value = pass Then
MsgBox "Assinatura concluída com sucesso"
Else
MsgBox "Assinatura inválida!"
End If
oRs.MoveNext
Else
MsgBox "Assinatura inválida!"
End If
'---------------------------------------
' faz o cleanup
'---------------------------------------
oCn.Close
Set oRs = Nothing
Set oCn = Nothing
End If
End If

End Sub
 
Hi,
textbox in word document can be either shape or inline shape. In both cases status bar blinks when switching between controls, but in my case the document is stable for inline shapes.
Both types can be converted programmatically (only?), I created inline textboxes with:

ThisDocument.InlineShapes.AddOLEControl "Forms.TextBox.1"

Concerning my other reply, SetFocus method exists only when the textbox is on the userform, so is not admissible in document.
Anyway, you could consider to separate validation from document and move it to custom dialog (userform).

combo
 
I can not duplicate this.

When I pick text from a activex textbox, pick up the Tab key press, dump its contents into another textbox, then move focus to a third textbox, I get no blinking at all. It seems to work fine. I put a bunch of text between them (etc etc), and I do not get blinking.

Except, as combo points out, in the status bar. The status bar blanks when focus is in an ActiveX control.

combo - put a text formfield (or if you prefer, a Forms textbox) into a document. Run:

Code:
MsgBox ActiveDocument.Shapes.Count & _
   vbCrLf & vbCrLf & _
   ActiveDocument.InlineShapes.Count

Both counts = 0. I am not sure, perhaps in other versions, a formfield textbox is a Shape, or an InlineShape. However, in Word 2002, they appear to be neither a Shape or an InlineShape. Even if you declare both a Shape object, and a InlineShape object and use them (as in a For Each) to run through the collection...there is nothing there. Yet the document HAS textboxes.

Formfield textboxes are their own objects, and can not be converted to either Shapes, or InlineShapes. At least I can not do so. If there is a way, please post some code.

ActiveX textboxes, on the other hand may be converted. However, it is always a bit tricky doing this, as the change from un-anchored to anchored can cause some unforseen effects. Anchored objects, when converted by code, by default move to the top of the current page. So it is always a good idea to include anchor locations when doing a conversion.

Back to this post though. Ricardo, I do not know what to suggest. I do not see why the other instructions would affect anything. Turn off screen refresh and update until the end of your code? I don't know...

Gerry
 
Hi.
Gery, Formfield textboxes (forms toolbar, native word field) can be accessed in FormFields or Bookmarks collections, it is a field with {FORMTEXT} code, it is not a shape.
As you pointed, textbox inserted from toolbox (activex, NSForms library), depending on formatting (position in text) the answer by your coce can be "0 1" or "1 0". I was not able to convert it to anything other.
Ricardo, as I already wrote, I wouldn't mix validation with document text. with one button click or on opening you can display a userform and focus on navigation there...

combo
 
combo - here was what you wrote:
textbox in word document can be either shape or inline shape.

I find it interesting that you repeated back to me what I answered. Textboxes in Word are NOT either shapes or inline shapes. They can be, but they do not have to be.

As for the "0 1" or "1 0". The return is completely dependent on if it is Shape, or InlineShape. And THAT is defined by whether the object is anchored, or not....among other properties.

Gerry
 
Gerry, you are absolutely right. Textbox in word doc can be either activex control or formfield, and without further type specification it could be confusing. Looking at the context of post and sample code I assumed that it is activex textbox and put the rest aside.

combo
 
Thanks for your replies!


Did you try it with a .dot document? I'm using a template and that could be the difference.
I thought it could be from mail merge, which is also used in my report, but the same effect ocurred with a simple document.
Maybe it is caused by the version of word: mine is Word 2002 with SP3.

I will try the userforms, but I don't know if it is intended in this case (my bosses will decide that).

Gerry, what do you mean with "Turn off screen refresh and update until the end of your code"

Thanks a lot!

Ricardo
 
I am also using 2002/ SP3. I can not duplicate this.

I am not sure what you mean by try it from a .dot file. Do you mean watching run IN the .dot file? Or running it from the document cloned from the .dot file?

The stuff you are doing are in procedures. Add this to them:
Code:
Application.ScreenUpdating = False
  '  ....do your stuff...
With Application
   .ScreenUpdating = True
   .ScreenRefresh
End With

Actually, I suspect that there may be something in the Change event of your control. Is there anything there? If not, you may want to try putting .Updating = False there, and see what happens. If it goes away that would narrow it down. Of course the textbox will NOT show its changes until you tab out, and the other code that puts .Updating back on runs.


Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top