Setfocus to a control where name of control saved in variable or field

  • Thread starter Thread starter Andrew
  • Start date Start date
A

Andrew

I have a form which has a number of search fields in the header section and
the results are displayed within the details section.

I want to be able to return the user back to the last field that they
changed within the Header Section if on clicking the custom search button no
records are found.

I have used 'Screen.PreviousControl.SetFocus' which mostly works, but only
works if the user directly clicks the search button after entering their
search in the field box. If the user enters something in one of the search
fields and then tabs to the next search field then this method does not
return the user to the last 'edited' field.

What I want to do is - after a user changes one of the searchfields, store
the 'name' of the search field either in a hidden field on the form or as
some type of form variable. After the user clicks the search button and no
records are returned, set the focus back to the 'field name' stored in the
hidden field or variable

ie - SomeVar = me.HiddenField.value
me.SomeVar.setfocus

unfortunately Access exits at this point or says somethink like 'type
mismatch'.


Is there anyway you can substitute in a variable name using the setfocus
method.

Thanks Andrew

PS I also tried - things like Me.Controls("SomeVar").SetFocus - but without
any luck
 
That way that I yould tackle this is to declare a module
level variable and set it to a unique number in the
AfterUpdate event handler of your searc fields
e.g
Private Sub Search1_AfterUpdate()
mlngReturnControl = 1
End sub

Private Sub Search2_AfterUpdate()
mlngReturnControl = 2
End Sub

Then in the event handler for the Search Button, put a test
to decide which control to return to.
e.g.
{when no results returned}
If mlngReturnControl = 1 Then
Search1.SetFocus
ElseIf mlngReturnControl = 2 Then
Search2.SetFocus
etc etc

If there are a number of controls, then a Select Case may
be a better construct to use. But, hopefully, you get the
principle.

Hope That Helps
Gerald Stanley MCSD
-----Original Message-----
I have a form which has a number of search fields in the header section and
the results are displayed within the details section.

I want to be able to return the user back to the last field that they
changed within the Header Section if on clicking the custom search button no
records are found.

I have used 'Screen.PreviousControl.SetFocus' which mostly works, but only
works if the user directly clicks the search button after entering their
search in the field box. If the user enters something in one of the search
fields and then tabs to the next search field then this method does not
return the user to the last 'edited' field.

What I want to do is - after a user changes one of the searchfields, store
the 'name' of the search field either in a hidden field on the form or as
some type of form variable. After the user clicks the search button and no
records are returned, set the focus back to the 'field name' stored in the
hidden field or variable

ie - SomeVar = me.HiddenField.value
me.SomeVar.setfocus

unfortunately Access exits at this point or says somethink like 'type
mismatch'.


Is there anyway you can substitute in a variable name using the setfocus
method.

Thanks Andrew

PS I also tried - things like
Me.Controls("SomeVar").SetFocus - but without
 
What I want to do is - after a user changes one of the searchfields, store
the 'name' of the search field either in a hidden field on the form or as
some type of form variable. After the user clicks the search button and no
records are returned, set the focus back to the 'field name' stored in the
hidden field or variable

ie - SomeVar = me.HiddenField.value
me.SomeVar.setfocus

unfortunately Access exits at this point or says somethink like 'type
mismatch'.


Is there anyway you can substitute in a variable name using the setfocus
method.

Thanks Andrew

PS I also tried - things like Me.Controls("SomeVar").SetFocus - but without
any luck

You were REALLY close. Lose the " marks around SomeVar and it will
work!
 
Thanks to both Gerald and John for your solutions,

Stanley, just for my future use - How do I declare a module level variable?

I tried declaring 'static' variables and 'friend' variables, but didn't
really know what I was doing, where to declare them and how I was meant to
reference that variable back to my search sub.

Any pointers would be appreciated.

Thanks Again
Andrew
 
Andrew

On the VB Coding Page (i.e. the page you see when you use
the Visual Basic Editor), module level variables are those
that are declared outside of any procedures (event
handlers, functions, subroutines etc). As such, the
convention is to declare them at the top of the page after
the 'Option' declarations (Option Database Compare, Option
Explicit etc).

Instead of using the Dim statement to declare them, you use
either Private or Public; the difference being whether you
wish to use the variable only within the module (Private)
or also outside the module (Public). The naming convention
is to prefix the variable name with m (Private) or g (Public)
e.g.

Private mlngReturnControl As Long 'Can Be used anywhere in
the module

Hope That Helps
Gerald Stanley MCSD
 
Back
Top