Sending the user to a specific control on the form

W

Walter

I have found a workaround for my problem, but I'm curious as to why the problem exists in the first place. On my userform, I have four textboxes that are required. I don't want the user to be able to enter any other data until the first field has been filled, then the second, third and fourth. After that, the rest of the form is free game. I tried first to use SetFocus to send the user back to the textbox, but instead, the cursor moved on to the next box. This happened in every instance where I used SetFocus to go to a textbox. It works fine if I use it to send the focus to a command button. My workaround was to add this:

Private Sub UserForm_Activate()
If CaseNoBox.Text = "" Then
cbxCaseType.Enabled = False
cbxLName.Enabled = False
cbxFName.Enabled = False
DeftAddressBox.Enabled = False
cbxCity.Enabled = False
cmbStates.Enabled = False
ZipBox.Enabled = False
NumCharges.Enabled = False
SpinButton1.Enabled = False
cbxCharge1.Enabled = False
btnNext.Enabled = False
btnPrint.Enabled = False
End If
....(there's a lot more stuff in this Sub)

Then I enabled specific controls as in the following example:

Private Sub CaseNoBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If CaseNoBox.Text = "" Then
MsgBox "This is a required field. You must enter a number before proceeding.", vbOKOnly, "Required Field"
MinEntryForm.CaseNoBox.SetFocus
Else
cbxCaseType.Enabled = True
cbxLName.Enabled = True
End If
End Sub

This works, but it seems that it would be a lot easier if I could just make the focus continually return to the same textbox whenever the user tries to leave, until data is entered in the box.

Any ideas?
 
W

Word Heretic

G'day Walter <[email protected]>,

What about just selecting the control's range in question?

Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Walter reckoned:
 
W

Walter

Sorry, I'm having no luck figuring out how to do that. Could you point me in the right direction? The controls I'm having trouble with are text and combo boxes, and I get syntax errors when I try to put .Range or .Select with them.

Thanks in advance,
Walter
 
W

Word Heretic

G'day Walter <[email protected]>,

Assuming you have used a unique bookmark name for the field (default
behaviour) you can do something like

ActiveDocument.Bookmarks("UniqueID").range.fields(1).select

Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Walter reckoned:
 
W

Walter

Sorry Steve,
I think I created my own confusion here. I'm talking about the text and combo boxes on the webform, or whatever Microsoft wants us to call it, that was created in VBA, as opposed to form fields in a Word document. Your suggestion would certainly work fine with form fields, but I don't think the VBA controls have a range property. Do you know of any way to make the focus stay in or continually return to a textbox control until data has been entered?
 
W

Word Heretic

G'day Walter <[email protected]>,

Ah - are you talking about UserForms? We have a group dedicated to
them - the VBA dialogs.

I need a better spec to help you, give me a walk through of the the
problematic vs desired functionality.

Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Walter reckoned:
 
W

Walter

Ah, yes, UserForms. Please tell me where to find VBA dialogs!

Meanwhile, here's what I'm trying to do. The first textbox on the UserForm is for a case number. I don't want the user to be able to use any of the other text/combo boxes to enter data until a valid case number has been entered. Reason: The case numbers are generated by another program. This UserForm takes data to produce a number of paper forms which will be filled out in during a court session. On exit from the Case Number text box, the number entered will be compared to all previously entered case numbers to prevent duplication. If an attempt is made, accidentally or intentionally, to bypass this box, I want to pop up a "required field" msgbox (done, works ok), and I want the focus to stay in that box, or return immediately to that box, until a valid number is entered.

The same thing needs to happen to the next two comboboxes, which are for last and first name. Reason: On exiting these boxes, the names will be compared to all previously entered names. If a match is found, then the personal information corresponding to the previously entered, matching name will be entered automatically.

Currently, even the workaround I implemented in the original post isn't working quite right. On leaving the Case Number textbox, the msgbox pops up fine. When I press OK, the focus just jumps over the controls that were enabled "OnExit" to the next control that was enabled before the "OnExit" function executed. That control is the "Print" button, which needs to remain enabled, as does the "Quit" button, so I can't just disable everything on the form.

Basically, I want to force the focus to stay or immediately return to the current box any time someone tries to change the focus, until data has been entered. Then I want the focus to move to the next box and act the same way until data is entered. The same with the following box. After that, the rest of the UserForm is fair game.

Any advice, even "go look at this other place and bug them", would be MUCH appreciated. Thanks!
 
W

Word Heretic

G'day Walter <[email protected]>,

OK. That was a much betterer explanation :)

I would opt for a different methodology, here are two quick
suggestions:

a) Pre-empt current form with a msgbox / form with single dropdown /
list to select the Case Number. Completing the pre-empted form shows
the current form.

b) Disable all other inputs on the form as std, and re-enable them in
the code that validates the Case Number input.


Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Walter reckoned:
 
W

Word Heretic

G'day Walter <[email protected]>,

Does Print _really_ make sense with no case number? If it does, don't
disable it. If it doesn't - disable it as well.

You can't tell when the app receives focus, so that rules out many of
those sort of games from extrapolations.

There is a TextBox1.SetFocus command but I am a bit suss of its
functioning. You thus invalidate the input and setfocus back there
again.


Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Walter reckoned:
 

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

Top