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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting focus on controls on forms from VBA functions 2

Status
Not open for further replies.

wdennis

Technical User
May 31, 2000
9
0
0
US
I've written the following routine which I call from the click event of an OK button on a custom dialog form:<br><br>Function UseDialog(strNameDialog As String, ctlText As Control)<br><br>'This procedure insures that the user has entered a project<br>'number into the text box named txtProjNum.&nbsp;&nbsp;If a project<br>'number has not been entered, the user is reminded with a<br>'message box.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Inspect text box for user input and display<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'reminder if box is empty<br>If IsNull(ctlText) Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Arguments for MsgBox<br>&nbsp;&nbsp;&nbsp;&nbsp;mstrMsg = &quot;Please enter a project number in the text box.&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;mintStyle = vbExclamation + vbOKOnly<br>&nbsp;&nbsp;&nbsp;&nbsp;mstrTitle = &quot;WHOOPS! NO PROJECT NUMBER&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Call message box function<br>&nbsp;&nbsp;&nbsp;&nbsp;MsgBox mstrMsg, mintStyle, mstrTitle<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Move focus to text box for project number<br>&nbsp;&nbsp;&nbsp;&nbsp;DoCmd.GoToControl(ctlText) <br>Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Or hide dialog while data transfers to main form<br>&nbsp;&nbsp;&nbsp;Forms(&quot;fdlgEnterProjectNumber&quot;).Visible = False<br>End If<br><br>End Function<br><br>This is the line I use to call the function:<br>&nbsp;&nbsp;&nbsp;Call UseDialog(&quot;fdlgEnterProjectNumber&quot;, txtProjNum)<br>Everything seems to be working fine except the line<br>&nbsp;&nbsp;&nbsp;DoCmd.GoToControl(ctlText)<br>I'm getting a message that I've used the GoToControl method without including a control name.&nbsp;&nbsp;How do I refer to the control named &quot;txtProjNum&quot; using the variable &quot;ctlText&quot; which I passed from the call?<br><br>Also, if I replace the line<br>&nbsp;&nbsp;&nbsp;Forms(&quot;fdlgEnterProjectNumber&quot;).Visible = False<br>with<br>&nbsp;&nbsp;&nbsp;Forms.strFormName.Visible = False<br>a message tells me that the &quot;object doesn't support this property or method.&quot;<br><br>What am I missing here?&nbsp;&nbsp;I'm a little new at this.&nbsp;&nbsp;I can get the code to work in the form module using &quot;Me&quot; to reference the form, but I'd like to progress beyond that and learn to put most of the code in functions separate from the forms so they'll run faster.&nbsp;&nbsp;Sure could use some help.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thanks in advance, wdennis&nbsp;&nbsp;<br>&nbsp;&nbsp;<br>
 
Try this:<br><br>==========<br>Public Sub UseDialog(strForm As String, strField As String, strText As String)<br>&nbsp;&nbsp;&nbsp;&nbsp;If strText = &quot;&quot; Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;Please enter a project number in the text box.&quot;, vbExclamation + vbOKOnly, &quot;No Project Number&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Forms(strForm)(strField).SetFocus<br>&nbsp;&nbsp;&nbsp;&nbsp;Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Forms(strForm).Visible = False<br>&nbsp;&nbsp;&nbsp;&nbsp;End If<br>End Sub<br>==========<br><br>And call it like this:<br><br>Call UseDialog(&quot;fdlgEnterProjectNumber&quot;, &quot;txtProjNum&quot;, Me.txtProjNum)<br><br>OR, this may work as well:<br><br>Call UseDialog(Me.Name, &quot;txtProjNum&quot;, Me.txtProjNum)<br> <p>Jim Lunde<br><a href=mailto:compugeeks@hotmail.com>compugeeks@hotmail.com</a><br><a href= Application Development
 
Hi Jim,<br><br>I tried the changes you suggested and got a message that complained of an invalid use of Null.&nbsp;&nbsp;I don't understand why since I thought an empty string was completely different from a null.&nbsp;&nbsp;However, changing the 3rd argument to ctlText as Control in the definition and then changing this line:<br>&nbsp;&nbsp;If strText = &quot;&quot; Then<br>to<br>&nbsp;&nbsp;If IsNull(ctlText) Then<br>seems to do the trick.&nbsp;&nbsp;I can see now why I needed a third argument in the call, but still don't see why it needed to be declared as type Control?&nbsp;&nbsp;Can you explain this?&nbsp;&nbsp;It still looks to me like the code you wrote should have run?<br><br>Thanks so much for your help, wdennis
 
Yeah, that makes sense. I forgot that if you call the procedure and nothing has been entered in the field on the form, it is actually Null, not blank (&quot;&quot;).<br><br>I'm glad you got it to work. <p>Jim Lunde<br><a href=mailto:compugeeks@hotmail.com>compugeeks@hotmail.com</a><br><a href= Application Development
 
Screen.ActiveForm will allow you to reference the form that has just lost the focus - see help file for details - does not work for subforms.
 
Screen.ActiveForm will not reference a form the has already lost the focus, it will only reference a form that HAS the focus.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top