I can't set focus where I want

Juan

Registered User.
Local time
Today, 23:44
Joined
Feb 16, 2006
Messages
32
Hi everyone

I have a main form with two subforms and one of the subforms has a further subform. Lets call them Main, Sub1, Sub2 and Sub1Sub.

I have the following code which triggers as soon as I open Main:

Private Sub Form_Current()

Sub1.SetFocus
On Error Resume Next
DoCmd.GoToRecord , , acLast
On Error GoTo 0
Forms!Main!txtBox1.SetFocus

the code contines .....

It works, in that the final record on the first subform is displayed and focus returns to the main form. I'd like to have focus move to a textbox on Sub1Sub, but when I change the final line to Forms!Sub1Sub!txtBox2.SetFocus, it generates an error "... can't find the form <Sub1Sub> referred to ... I use exactly the same name of the subform as displayed in the database window.

If I change the final line to Sub1Sub.SetFocus, or Sub1Sub!txtBox2.SetFocus, it generates an error "Object required".

I'm surprised that I can't set the focus on the second subform (Sub1Sub). Can someone tell me how I can set the focus where I want it to be? I hope I've explained the situation sufficiently.

Thank you.
 
I always seem to have to 'step' through the forms to get it to work

Me.[FirstSub].SetFocus
Me.[FirstSub].[Form]![SecondSub].SetFocus
Me.[FirstSub].[Form]![SecondSub].[Form].[MyField].SetFocus

HTH

Peter
 
Forms!Main!Sub1!Sub1Sub!txtBox2.SetFocus
 
Forms!MainForm!SubForm!SubFormSub.Form!field1.SetFocus

That should do the trick.

If you are trying to reference an object on the form use the ! after Form.

If you are trying to reference a property on the form then you would use the . after Form like this

Forms!frmActivityLog!sfrmActivityLog.Form.AllowEdits
 
Last edited:
Thanks for the responses.

I can go to the textbox on the subsubform "manually", but none of the code will do it for me! I know that I haven't denied data entry or locked the text box (for instance). This is infuriating, as well as confusing, particularly regarding the square brackets, Form vs Forms and the exclamation marks. At least I don't get any errors now when I use any of the code that you've provided - it's just that it won't do what I want! I'll continue with the permutations.

Juan
 
did you try my 3 step approach?
From help
Tip You can use the SetFocus method to move the focus to a subform, which is a type of control. You can also move the focus to a control on a subform by using the SetFocus method twice, moving the focus first to the subform and then to the control on the subform.

Peter
 
Hi Peter

Yes, I did and I have no idea why this isn't working. I'm at the early stage of the database design, so I think I'll delete the second subform, save the database and then recreate the second subform. I realise that the code will throw up an error until the second subform has been regenerated.

Juan
 
Hi Peter (again!)

I just deleted and recreated the subform and everything works fine now. That's all very well, but I'm intrigued to know why it didn't work in the first place. None of the fields were locked and they were enabled. I could move to the second subform with the mouse to enter data.

C'est la vie!

Thanks again for your input.

Juan
 
It could have been naming, It is not normaly a problem as Access gives the Subform the same name as the Source Object. You need to refer to the form name, not the source object. I think :(

Peter
 

Users who are viewing this thread

Back
Top Bottom