SetFocus question

buratti

Registered User.
Local time
Today, 16:20
Joined
Jul 8, 2009
Messages
234
I have a form with subform. In the After update event of my Account Number field I set the focus to the first field in the subform like so:

Private Sub Customer_ID_AfterUpdate()

Me!OrderSubform.SetFocus

End Sub

Everything works fine, but here is my problem... the entire form is just a bit longer than the screen or window will allow for all to appear without scroll bars. When it sets focus to the correct field in the subform, it scrolls down so the now focused field is at the very top of the window, making me have to re-scroll up to view the Customer info just entered. With the original view, or when scrolled all the way to the top, the field I want to set the focus to is already completely visable.

Wait a minute... as I was writing that I think i figured out why it was doing that, but still need a solution on to fix it. I'm guessing its doing it because I'm setting focus to the subform only (which itself sets focus to the first field) and not directly the field in the subform. I'm somewhat new to VBA so what would be the correct code/syntax to focus on the the field Card_Type in the subform.

BTW I am assuming that SetFocus is the same as saying "move the cursor to..." if it's not, then that's what I'm trying to do instead.

Thanks for any help here
 
By the way, this:

Me!OrderSubform.SetFocus


does not set focus on the subform, it sets it on the subform control (the control that houses the subform on the main form). If you want to set focus on the subform you need:

Me.OrderSubform.Form.SetFocus

and then to a control

Me.OrderSubform.Form.YourControlNameHere.SetFocus

And OrderSubform needs to be the name of the subform control and not the subform name itself, unless the control is named the same as the subform.
 
Thanks for the suggestions...
I used this:

Private Sub Customer_ID_AfterUpdate()
Me.OrderSubform.Form.Card_Type.SetFocus
End Sub

And it did nothing, and I used this:

Private Sub Customer_ID_AfterUpdate()
Me.OrderSubform.Form.SetFocus
End Sub

And it gave me the error: Run-Time error 2449: There is an invalid Method in an Expression

OrderSubform is the name of the Subform control. The actual subform is Order Subform for Order Details. Card_Type is the name of the field i am trying to bring the cursor to.

What is the difference between (.) and (!) in these type of expressions and when would I use each????
 
Private Sub Customer_ID_AfterUpdate()
Me.OrderSubform.Form.Card_Type.SetFocus
End Sub

And it did nothing, and I used this:

Private Sub Customer_ID_AfterUpdate()
Me.OrderSubform.Form.SetFocus
End Sub

And it gave me the error: Run-Time error 2449: There is an invalid Method in an Expression
1. Is this code on the main form and not the subform?

OrderSubform is the name of the Subform control. The actual subform is Order Subform for Order Details. Card_Type is the name of the field i am trying to bring the cursor to.
2. I might need to have you upload the database for me to see what is actually going on. If you have to, you can delete all of the data out and then compact and repair and then zip the file.

What is the difference between (.) and (!) in these type of expressions and when would I use each????
There is a long answer and a short answer. I'm going to go with the short answer. The Bang (!) is normally used when referring to a collection and sometimes a FIELD. When referring to CONTROLS, you would normally use the Dot (.) and in this case you would be needing to refer to the CONTROL name and not the field name. If Card_Type is the name of the text box on the form, then you should be fine using .Card_Type

If the name of the control has a space in it, you can't substitute an underscore. So, for example, if the control (or field) is actually named
Card Type
then you can't use Card_Type to refer to it. You need to use

.[Card Type]
with the square brackets.
 
1. Is this code on the main form and not the subform?
Yes its in the after Update event of the Customer ID field which is in the main form

If the name of the control has a space in it, you can't substitute an underscore. So, for example, if the control (or field) is actually named
Card Type
then you can't use Card_Type to refer to it. You need to use .[Card Type]
with the square brackets.

Yes the name of the field actualy is Card_Type with no spaces.

Here's the database...
 

Attachments

Well, I'll have to take a more deeper look but if that other method works, then I guess use it. I'm not sure yet why the others are returning errors or not doing anything.
 

Users who are viewing this thread

Back
Top Bottom