469,288 Members | 2,353 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,288 developers. It's quick & easy.

How do I set focus from one subform to another?

Seth Schrock
2,962 Expert 2GB
I'm trying to set the focus from one subform to a control on another subform that is on the same parent form. Currently I have:

Expand|Select|Wrap|Line Numbers
  1. Forms!frmCustomersfrmFileTypes!FileName.SetFocus
but it doesn't work. I'm guessing that is because I'm starting from another subform because I've used this before to go from the parent form to the subform and it worked. I thought I could set the focus to the parent form and then use the above code but that didn't work either.
Dec 2 '11 #1

✓ answered by sierra7

Hi Seth
You must do this in stages. What I missed was ALL the instructions are coming from the first sub-form so must be routed through the Parent
Expand|Select|Wrap|Line Numbers
  1. Me.Parent.SetFocus
  2. Me.Parent![sfrmFileTypes].SetFocus
  3. Me.Parent![sfrmFileTypes].Form![FileName].SetFocus
The above works
S7

19 11083
NeoPa
32,173 Expert Mod 16PB
You don't mention pages or tabs, but I don't believe the answer changes even if you do.

Assuming a form with two subforms then, and the code running in the first subform you might try something like :

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSelectOther()
  2.     Call Me.Parent.[OtherSubformControl]![ControlOnOtherSubform].SetFocus
  3. End Sub
Dec 2 '11 #2
Seth Schrock
2,962 Expert 2GB
Both subforms are on tabs, but they are on the same tab. However, like you said, I don't think that that should change anything. I should have mentioned this is my OP.

I will give that a try on Monday when I get back to work.
Dec 3 '11 #3
NeoPa
32,173 Expert Mod 16PB
Cool. Let us know how it goes. I don't have any to test on so I'm posting from theory ;-)
Dec 3 '11 #4
Seth Schrock
2,962 Expert 2GB
Okay, I just tried it and get an error message saying: Compile error: Expected:. or ( and then it highlights the !. I've never used the Call me.Parent command, so I have no idea how to procede.
Dec 5 '11 #5
Seth Schrock
2,962 Expert 2GB
I'm beginning to think that I might know why it isn't working, although I still don't know how to fix it. My code is set within an OnChange event which checks to see if a control on the other subform is null, and if true (it is null) then it produces an error message and then sets the focus to that null control. However, because it is in an OnChange event, it make the record dirty (changed). Could this be stopping the focus from being able to be transferred? I didn't get any error messages from my original code, it just wouldn't change the focus over to the other control.
Dec 5 '11 #6
NeoPa
32,173 Expert Mod 16PB
It's hard to say with just a loose description of the code instead of the code itself Seth. The code could be almost anything which is a little hard to work from.
Dec 5 '11 #7
Seth Schrock
2,962 Expert 2GB
I will post the code tomorrow when I get to work. I should have posted it in my last post.
Dec 6 '11 #8
NeoPa
32,173 Expert Mod 16PB
Seth:
I should have posted it in my last post.
I won't argue with that, but I can say that it's pleasant working with someone who doesn't need everything hammered home.

You'll do fine, and the code tomorrow will also do nicely :-)
Dec 6 '11 #9
Seth Schrock
2,962 Expert 2GB
Here is the code.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Email_Change()
  2. If IsNull(Forms!frmCustomer!sfrmFileTypes!FileName) Then
  3.     MsgBox ("Please select a file name before entering an email address")
  4.     Call Me.Parent.[sfrmFileTypes]![FileName].SetFocus
  5. End If
  6. End Sub
Dec 6 '11 #10
NeoPa
32,173 Expert Mod 16PB
I'm not sure I know the answer to this off the top of my head Seth (and I don't have anything to play with and test).

I can't promise anything, but if you attach your sanitised database (Attach Database (or other work)) I'll look at it for you and see if I can determine what's going wrong.
Dec 7 '11 #11
sierra7
446 Expert 256MB
Hi
I think you have to do this in stages. First move the focus up to the Parent, then to the sub-form control, then finally to the required fieldname.
Expand|Select|Wrap|Line Numbers
  1. Me.Parent.SetFocus
  2. Me![SubformName].SetFocus
  3. Me![SubformName].Form![FieldName].SetFocus
  4.  
A bit tedious but something similar happens when you are dealing with multiple subforms in Design mode, you have to click on the parent container before you can select an element within it.
S7
Dec 7 '11 #12
Seth Schrock
2,962 Expert 2GB
@sierra7 I just tried your code and it came back with a "{database name} can't find the field 'sfrmFileTypes' referred to in your expression." error message in the second line of code. I tried putting
Expand|Select|Wrap|Line Numbers
  1. Me!frmCustomer.Form!sfrmFileTypes.SetFocus
but all that did was make the error say "can't find field 'frmCustomer'".

@NeoPa I will try to get you the sample database here shortly. I'll include some sample data as the real data is confidential.
Dec 7 '11 #13
Seth Schrock
2,962 Expert 2GB
Okay... Here is the sample database in .mdb (not .accdb). I tested it after creating the new sample and the problem still exists. You will notice empty combo boxes and tabs, but don't worry about them. I just didn't include the extra forms and tables.
Attached Files
File Type: zip Seths Database Test.zip (85.1 KB, 193 views)
Dec 7 '11 #14
NeoPa
32,173 Expert Mod 16PB
What do I do to get your code to attempt to execute, and where exactly is this code found?

Probably a dumb question, but it's late and I'm tired ;-D
Dec 8 '11 #15
Seth Schrock
2,962 Expert 2GB
The code is found in sfrmEmail in the Email control's OnChange event. To trigger the code, open frmCustomer. In the tabs, select a blank file under Regular 1 (as if you just finished entering the Regular 1 record) and then try typing in an email address. Basically, I'm trying to keep an email address from getting assigned to a blank file.
Dec 8 '11 #16
sierra7
446 Expert 256MB
Hi Seth
You must do this in stages. What I missed was ALL the instructions are coming from the first sub-form so must be routed through the Parent
Expand|Select|Wrap|Line Numbers
  1. Me.Parent.SetFocus
  2. Me.Parent![sfrmFileTypes].SetFocus
  3. Me.Parent![sfrmFileTypes].Form![FileName].SetFocus
The above works
S7
Dec 8 '11 #17
NeoPa
32,173 Expert Mod 16PB
I cannot get the code to fire at all. In Datasheet mode (as it is) it just ignores any changes to the control :-( I tried in other modes too and no joy there either.

It does require a reference that I don't have, and I guess that must be the problem. I replaced it with the closest match I could find on my system, but the OnChange event simply never triggered (and I tried various different options to see if I could make it do so). It also wouldn't allow me to sign the project, so something wasn't quite right with it somewhere (on my system at least).

Sorry.
Dec 8 '11 #18
Seth Schrock
2,962 Expert 2GB
@sierra7 Thanks. It works perfectly.

@NeoPa I'm not sure why you couldn't get it to work. After I created the sample database, I tested it to make sure that the problem still existed and everything triggered like it was supposed to.
Dec 8 '11 #19
NeoPa
32,173 Expert Mod 16PB
I'm sure you did Seth. No criticism implied.

I suspect that the availability of the reference on your PC made it behave differently for you than for me.
Dec 8 '11 #20

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Stuart Wexler | last post: by
17 posts views Thread by Neil Ginsberg | last post: by
reply views Thread by MattB | last post: by
1 post views Thread by Heather Crow | last post: by
1 post views Thread by massimo s. | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.