Code to 'Tab'

N

Nick T

Hi,
Got a form with a various text boxes on it. They are all allocated a 'tab
index number' in their properties settings. When i press the tab key on my
keyboard, it tabs through the text boxes just fine. However i want to carry
out this process by using a cmd button. Any suggestions for the code i need
to do this??

Many thanks
 
M

Mr. B

Nick T,

Here is some code that will let you use a command button to move through
each textbox on a form using a command button. If you continue to click the
command button when you reach the last textbox on the form, it will move the
focus to the first textbox on the form starting all over again. There is no
functionality for moving backward, but you would need another command button
for that functionality.

This code assumes that you have all of your text boxes with the appropriate
tab order for the way you want the user to move through each text box. The
tab index for your text boxes must be concecutive. They also must be from 0
through the max number of your text boxes. For example, 0 to 10, would have
eleven text boxes.

Place the following in the area just below the "Option Explicit" statement
in your VAB code module for your form to declare some variables:

Dim ctrl As Control

Dim intCtrlIndxNum As Integer
Dim intTextBoxCnt As Integer

Now, place the next few lines of code in the On Open Event of your form:

For Each ctrl In Controls
If ctrl.ControlType = acTextBox Then
intTextBoxCnt = intTextBoxCnt + 1
End If
Next

Next, place the next line of code in the GotFocus event of each of your text
box controls:

intCtrlIndxNum = Me.ActiveControl.TabIndex

Next, place the following code in the On Click event of your command button:

For Each ctrl In Controls
If ctrl.ControlType = acTextBox Then
If intCtrlIndxNum + 1 = intTextBoxCnt Then
intCtrlIndxNum = -1
End If
If ctrl.TabIndex = intCtrlIndxNum + 1 Then
ctrl.SetFocus
Exit Sub
End If
End If
Next

If you have set the Tab Index for your text boxes correctly and place the
code in correct places you are now ready to try your command button. You can
place your cursor in any text box and then click the command button to move
to the next one and so on.

-----
HTH
Mr. B
http://www.askdoctoraccess.com/
Doctor Access Downloads Page:
http://www.askdoctoraccess.com/DownloadPage.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top