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

Setfocus and again

Status
Not open for further replies.

Vsnow42

Programmer
Sep 1, 2013
10
US
Please can someone help, I’m new to writing VBA code and got myself stuck. I have to use Access 2002 VBA.

I’m trying to move focus from one field to another, then another on a record in a subform, that is in datasheet format.

I have a record (row of info) in a subform that has the field BookedOutTo which after the user has finished entering a name no matter where they click on the subform it moves to a field called BookedOutBy, using Me!BookedOutBy.SetFocus in the On Lost Focus property.

This works

Then in the BookedOutBy field in the On Got Focus property I use code to get the user name of whoever is logged onto the computer and set the text of BookedOutBy to that. (ie it automatically enters the user name to log who did what).

This also works

However further along on this code I then try and move the focus to another field called BookedOutDate where I want it to automatically enter a date ( the intention was to just move focus after this to a QTY field, with no auto entry). However trying to set focus a second time fails and I get an error that says that BookedOutBy cannot get focus even though this was previously working. Despite the error message it does put the User name into the Field ?

I have tried putting the second Setfocus in the On Change property of BookedOutBy, but this also does not work. I tried making the Got Focus property change the name of a label and the On Change property to ask if the label has been changed before running the new set focus, but this also failed ( I think this is a timing thing as On Change seems to run after the first letter is entered, not when the auto input has finished).

Any help where I’m going wrong would be appreciated. All I want is to auto fill a few field before letting the user continue. So the main questing is how to a set a follow on setfocus command.


Thanks
Vsnow42
 
If you simply want to set the value of BookedOutBy, you really don't need to set focus to the control. The On Change does fire with every keystroke. The After Update would fire only once.

Also, using code to set a value in a control will not trigger the On Change or After Update events.

Duane
Hook'D on Access
MS Access MVP
 
How are ya Vsnow42 . . .

Remember you havn't saved the record yet, so you can write to as many controls as you like without worrying about detecting/using events. Then when you save the record everything's committed. Try the following in the [blue]On Lost Focus[/blue] event of [blue]BookedOutTo[/blue]:

Code:
[blue]   Me.BookedOutBy = "[purple][b]UserNameHere[/b][/purple]"
   Me.[purple][b]YourDateFieldName[/b][/purple] = Date
   Me.Qty.SetFocus[/blue]

Get the Idea!

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Thanks for the information both. When I tried to change the Field / control ( or whatever Microsoft wants to call it) it kept telling me I could not as it did not have focus. I sort of thought that you had to daisy chain from one field to the next changing things as you went, which seemed logical to me (seems not).

I have sort of accidently discovered on the lost focus of one field, you can set focus to any other, then directly below ( still in the same lost focus) you can set focus to the next and the next. Then on each of the got focus area's you can write code to change /set the field (does not feel so logical but seems to work).

Hope my explination made sense.Seems odd that once you "on the lost focus" of one field you set focus to the next field and the next and the next. You would have thought that once the new field ( first one you set focus to) had that focus that the previous field would have no more contol.

I may look at using your suggestions again and have another go, as they seem more logical to me.

Thanks again
 
You only need to set the focus if your code (which you haven't shared) uses the Text property. In Access VBA, we use the Value property which is the default property of some controls so it isn't necessary.

Duane
Hook'D on Access
MS Access MVP
 
Vsnow42 . . .

You can also use [blue]SetFocus[/blue] to position the focus to the control where you want the user input.

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top