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

Using a variable in a form name to set focus 1

Status
Not open for further replies.

Jimmy211

Technical User
Aug 8, 2002
42
US
Hi,

I'm wondering if its possible to use a variable when setting the focus to a particular form and field. I have a table with the name of the forms and fields. Based on the current form that's open and what other forms are open, I want the code to lookup in the table which form and field it should go to next when a tab or return is pressed. Setting the form and field that get the focus using a variable seems to be a problem. My code looks like this:

Private Sub LBTST4_MON_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case Shift
Case 1 ' Go backwards
Case Else 'Go forwards
If KeyCode = 9 Or KeyCode = 13 Then
Dim dform, dfield, formname As String
'look up the form and field to go to next
dform = FormNav1(Forms!frmmaster1!frameGroup, "frmTest4Rm", 1)
dfield = FormNav2(Forms!frmmaster1!frameGroup, "frmTest4Rm", 1)
Debug.Print dform, dfield
Me.Parent.[" & dform & "].SetFocus
Me.Parent.[" dform & "]![" & dfield &"].SetFocus
KeyCode = 0
End If
End Select
End Sub

I get an error message when it gets to the setfocus lines. Anyone know how to do this better?
Thanks,
Jimmy211
 
Basically, if you have the form name in a string variable, you should be able to do something like this (typed not tested - note the usage of the form keyword when working with subforms):

[tt]forms(strForm).setfocus
forms(strform).controls(strSubform).setfocus
me.parent(strsubform).setfocus
me.parent(strsubform).form.controls(strcontrol).setfocus[/tt]

Roy-Vidar
 
It worked! Thank you very much RoyVidr! Star for you!

Jimmy211
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top