Solved Is there another way? (1 Viewer)

mloucel

Member
Local time
Today, 14:17
Joined
Aug 5, 2020
Messages
181
This code works perfectly, but I tried to simplify and somehow I got lost..

I tried to simplify the code without the use of so many [ ] but I am getting lost
The Commented Line:
'Me!PatientAdmitanceF!PatientNotesSF.Form!Pnote.SetFocus
was my futile effort, is there a better way?

This is a FORM that contains a subform, once the fields on the form are verified to be correct, I make the SubForm visible.
Now I want to SetFocus to the field called Pnote
the code works but I would really want to simplify it as much as possible [ I AM LEARNING ]
does anyone have any Ideas..

I have checked this:

No luck..
MVPS_Website_Link

Anyone with a better Idea???

Code:
' Set the focus to the Note
            [Forms]![PatientAdmitanceF].[PatientNotesSF].SetFocus
            [Forms]![PatientAdmitanceF].[PatientNotesSF].[Form].[Pnote].SetFocus
            'Me!PatientAdmitanceF!PatientNotesSF.Form!Pnote.SetFocus
 

June7

AWF VIP
Local time
Today, 13:17
Joined
Mar 9, 2014
Messages
5,492
Brackets are not needed unless naming includes spaces or punctuation/special characters.

Code is behind main form?
Main form is named PatientAdmitance and subform container is named PatientNotesSF?
Me.PatientNotesSF.Form.Pnote.Setfocus
 

Edgar_

Active member
Local time
Today, 16:17
Joined
Jul 8, 2023
Messages
438
You don't need to set focus to the subform first, like you seem to be trying to do here:
Code:
[Forms]![PatientAdmitanceF].[PatientNotesSF].SetFocus
[Forms]![PatientAdmitanceF].[PatientNotesSF].[Form].[Pnote].SetFocus

You can simply use the second line:
[Forms]![PatientAdmitanceF].[PatientNotesSF].[Form].[Pnote].SetFocus

You also don't need the brackets, as June7 says, so the reference could look like any of these variants as well as others:
Forms.PatientAdmitanceF.Form.PatientNotesSF.Form.Pnote.SetFocus
Forms("PatientAdmitanceF").Form.PatientNotesSF.Form.Pnote.SetFocus
Forms!PatientAdmitanceF.Form!PatientNotesSF.Form!Pnote.SetFocus

Now, if by "simplify" you meant "shorter code", then there is one thing you can do: If PatientNotesSF does not have a module, assign one to it first from the "Other" tab and just use Form_PatientNotesSF.Pnote.SetFocus
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 22:17
Joined
Jul 9, 2003
Messages
16,363
You could also use a with block like this:-

Code:
' Set the focus to the Note
With [Forms]![PatientAdmitanceF].[PatientNotesSF]
    .SetFocus
    .Form!Pnote.SetFocus
End With
 

Gasman

Enthusiastic Amateur
Local time
Today, 22:17
Joined
Sep 21, 2011
Messages
14,447
You don't need to set focus to the subform first, like you seem to be trying to do here:
Code:
[Forms]![PatientAdmitanceF].[PatientNotesSF].SetFocus
[Forms]![PatientAdmitanceF].[PatientNotesSF].[Form].[Pnote].SetFocus

You can simply use the second line:
[Forms]![PatientAdmitanceF].[PatientNotesSF].[Form].[Pnote].SetFocus

You also don't need the brackets, as June7 says, so the reference could look like any of these variants as well as others:
Forms.PatientAdmitanceF.Form.PatientNotesSF.Form.Pnote.SetFocus
Forms("PatientAdmitanceF").Form.PatientNotesSF.Form.Pnote.SetFocus
Forms!PatientAdmitanceF.Form!PatientNotesSF.Form!Pnote.SetFocus

Now, if by "simplify" you meant "shorter code", then there is one thing you can do: If PatientNotesSF does not have a module, assign one to it first from the "Other" tab and just use Form_PatientNotesSF.Pnote.SetFocus
That is not what MS state?

Also mentioned here?
 

Edgar_

Active member
Local time
Today, 16:17
Joined
Jul 8, 2023
Messages
438
That is not what MS state?
Technically, the focus is set to the control regardless, you just can't see it because the subform has no focus yet.

However, it's true that in most scenarios, simply setting the focus to the control won't immediately allow you to work with the recently focused control. So, I stand corrected. Although it's not entirely inaccurate because the focus does get applied and there might be situations where you won't work with the control right away, but you still want the focus there once you focus the subform later.

But yes, if OP wanted to work with the control right after setting focus to it, they must set focus to the subform too, before or after.
 

Gasman

Enthusiastic Amateur
Local time
Today, 22:17
Joined
Sep 21, 2011
Messages
14,447
Yes, one article stated you could set focus in any particular order? :unsure:
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 17:17
Joined
Feb 19, 2002
Messages
43,484
When you use "Me", you do not include the name of the parent form in the expression because the code is running IN the parent form.

Me.PatientNotesSF.Form!Pnote.SetFocus


When you use "Forms!", that is the way to refer to a form externally so you must include the entire path. We don't use "Forms!" to reference objects on the current form or on a child of the current form, we use "Me." for that. WHY? VBA is an interpreted language. You don't want it to have to spend any longer than necessary to resolve references. "Me" says "current object". "Forms" says "go find this object and load it into memory if it isn't already there".

You probably don't need code at all if you fix the tab order on the subform so that the correct control gets the event when the subform gets the focus.
 
Last edited:

CJ_London

Super Moderator
Staff member
Local time
Today, 22:17
Joined
Feb 19, 2013
Messages
16,668
And in that situation (code is on main form) you don’t need the me. Although it helps with intellisense
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 17:17
Joined
Feb 19, 2002
Messages
43,484
The point of using Me in addition to getting intellisense is to tell the interpreter that the variable is defined in the current object and there is no need to search for it.

Unlike COBOL where I could actually see the assembler language code generated as part of the process of compiling my COBOL code (and alter my source code for efficiency if necessary), we don't get to see the different code that is generated if we use Me or omit it. So we can only go by the fact that MS gave us "Me" in the first place so it is most likely for a reason. Occasionally in one of those thousand page tomes we used to see written about Access, we would get insights into the inner workings of Access.
 

mloucel

Member
Local time
Today, 14:17
Joined
Aug 5, 2020
Messages
181
Brackets are not needed unless naming includes spaces or punctuation/special characters.

Code is behind main form?
Main form is named PatientAdmitance and subform container is named PatientNotesSF?
Me.PatientNotesSF.Form.Pnote.Setfocus
Thanks I was wondering, but I had so many failures and finally that one worked..
 

mloucel

Member
Local time
Today, 14:17
Joined
Aug 5, 2020
Messages
181
You don't need to set focus to the subform first, like you seem to be trying to do here:
Code:
[Forms]![PatientAdmitanceF].[PatientNotesSF].SetFocus
[Forms]![PatientAdmitanceF].[PatientNotesSF].[Form].[Pnote].SetFocus

You can simply use the second line:
[Forms]![PatientAdmitanceF].[PatientNotesSF].[Form].[Pnote].SetFocus

You also don't need the brackets, as June7 says, so the reference could look like any of these variants as well as others:
Forms.PatientAdmitanceF.Form.PatientNotesSF.Form.Pnote.SetFocus
Forms("PatientAdmitanceF").Form.PatientNotesSF.Form.Pnote.SetFocus
Forms!PatientAdmitanceF.Form!PatientNotesSF.Form!Pnote.SetFocus

Now, if by "simplify" you meant "shorter code", then there is one thing you can do: If PatientNotesSF does not have a module, assign one to it first from the "Other" tab and just use Form_PatientNotesSF.Pnote.SetFocus
Thanks
the reason why I did it like that it is because I found an example and the author said it was needed to first set the focus on the subform, then set the focus on the object.
It is here in this forums, I just lost the reference, but if I find it again I will post it.

Also I have NEVER used the option MODULE on other tab, I didn't even know it was there, I will try to find out more about it.
Thanks.
 
Last edited:

mloucel

Member
Local time
Today, 14:17
Joined
Aug 5, 2020
Messages
181
You could also use a with block like this:-

Code:
' Set the focus to the Note
With [Forms]![PatientAdmitanceF].[PatientNotesSF]
    .SetFocus
    .Form!Pnote.SetFocus
End With
Thanks Uncle Gizmo..
Love it..
I set it up, and it worked like a charm, I just got rid of the brackets, very elegant and I learned something new.
I will try to study a bit more about the use of WITH, seems very nice.
 

mloucel

Member
Local time
Today, 14:17
Joined
Aug 5, 2020
Messages
181
Thanks
I just followed an example I found here in the forums, unfortunately I lost the reference but I will try to look it up again, said that I need to reference the subform then the subform plus the object and since it worked I let it go, thou I found more refcerences online as well, none work, and when I tried to get rid of the brackets it made it worse, then I made the question here.
thank you.
 

mloucel

Member
Local time
Today, 14:17
Joined
Aug 5, 2020
Messages
181
When you use "Me", you do not include the name of the parent form in the expression because the code is running IN the parent form.

Me.PatientNotesSF.Form!Pnote.SetFocus


When you use "Forms!", that is the way to refer to a form externally so you must include the entire path. We don't use "Forms!" to reference objects on the current form or on a child of the current form, we use "Me." for that. WHY? VBA is an interpreted language. You don't want it to have to spend any longer than necessary to resolve references. "Me" says "current object". "Forms" says "go find this object and load it into memory if it isn't already there".

You probably don't need code at all if you fix the tab order on the subform so that the correct control gets the event when the subform gets the focus.
I tried Pat and just got worst.
then I tried this:
- Verify all data is there and the User Ok
- Set the focus on the subform
[ I did previously set the tab order on the subform so that the field I need is the ONLY one available and with TAB }
- when I run the code the focus goes to the subform, but the pointer does not go to the field unless I add the second part where I specifically set the focus on that field.
 

mloucel

Member
Local time
Today, 14:17
Joined
Aug 5, 2020
Messages
181
The point of using Me in addition to getting intellisense is to tell the interpreter that the variable is defined in the current object and there is no need to search for it.

Unlike COBOL where I could actually see the assembler language code generated as part of the process of compiling my COBOL code (and alter my source code for efficiency if necessary), we don't get to see the different code that is generated if we use Me or omit it. So we can only go by the fact that MS gave us "Me" in the first place so it is most likely for a reason. Occasionally in one of those thousand page tomes we used to see written about Access, we would get insights into the inner workings of Access.
Pat, I have no Idea what intellisense is, I honestly have no idea, so when you guys refer to 'ME" i am clueless, I do understand that me refers to the form, and replaces the need to write the name of the form, that is as far as I understand.
 

June7

AWF VIP
Local time
Today, 13:17
Joined
Mar 9, 2014
Messages
5,492
Intellisense is popup tips VBA provides as you type. Also, in query designer and expression builder.
 

Gasman

Enthusiastic Amateur
Local time
Today, 22:17
Joined
Sep 21, 2011
Messages
14,447
Intellisense is... when you start to type code the editor shows you the parameters required to complete the command.
So you should be able to put the parameters in the correct location for that command.
So as you type DoCmd.OpenForm you get first the prompt for the name of the form?

I am not sure if one can switch intellisense off, or any reason why one would do, even if they could.
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 17:17
Joined
Feb 19, 2002
Messages
43,484
when I run the code the focus goes to the subform, but the pointer does not go to the field unless I add the second part where I specifically set the focus on that field.

When you try something new and it doesn't work, just telling us "it" doesn't work is pretty useless since we don't know what "it" is. Please get used to always posting the code/query you tried to run and the result you expected if it isn't obvious and the result you got.
 

Users who are viewing this thread

Top Bottom