'Me' object no longer set

J

Joshua A. Booker

Hi,

The following error ocurrs after I call a public sub on another form, then
try to refer to the calling form using the me keyword.

Runtime Error 2467: 'The expression you entered refers to an object that is
closed or no longer exists'.

For Example:

Private Sub Button_Click()
Me.Textbox1.SetFocus 'this line would not cause error
Form_OtherFormName.PublicSubName ' this code runs fine
Me.Textbox1.SetFocus 'this line would cause error
End Sub

I assume the me object got lost when I called the other form, how do I
prevent this or reset the 'Me' object?

TIA,
Josh
 
E

Ed Jobe

I don't know what form2's sub does. Does it take focus away from form1?
Maybe you need to set focus to the form first. Me refers to the current
form, and perhaps form2 doesnt have a textbox1 control.

Try this.
Private Sub Button_Click()
Me.Textbox1.SetFocus 'this line would not cause error
Form_OtherFormName.PublicSubName ' this code runs fine
Me.SetFocus
Me.Textbox1.SetFocus 'this line would cause error
End Sub
 
J

Joshua A. Booker

Thanks for the suggestion. The problem is not related to setting the focus,
however. I used the setfocus command as an example. In this case, I tried
setting the focus back to the calling form. As far as I know, you can set
the focus on another form without the Me object going out of scope. I think
it should always refer to the form behind which the code is running
regardless of focus.

I think I've read about the Me object going out of scope or something, but I
can't find it. Does anyone know about this problem?

Thanks,
Josh
 
S

Scott McDaniel

"Me" refers to the currently active object (Form, Report, etc) ... in your
case, when you call a sub on Form_OtherFormName, "Me" then refers to the
form named "OtherFormName" , which probably doesn't have a control named
Textbox1 (which your code is trying to find).
 
D

Dan Artuso

Hi,
Are you sure it's the 2nd Me.Textbox1.SetFocus
that's causing the error and not something with calling the public sub?

I tested this out and it runs fine for me. I've never heard of Me going
out of scope when used in this manner.
 
J

Joshua A. Booker

Dan,

I'm sure the error occurs when I return to the procedure that called the
public sub and attempt to use the Me keyword. The error does not occur in
the public sub, but is related to it.

Have you tested calling a public sub in which a third sub is called? I have
experienced strange things with scope when I go three or more levels deep
calling Access class modules.

Is there a limitation?

Also, others are saying that Me refers to the currently active form. Am I
right to say it refers to the form behind which the code is run regardless
of the current form or which has the focus?

TIA,
Josh
 
A

Alick [MSFT]

Hi Joshua,

I did a test, the issue does not occur.

1. Create a new database with two forms.
2. Form1 with one button and text box, and Form2 with one textbox.
3. create a sub in form2

Public Sub test()

Debug.Print "hello"

End Sub

4. In form1 button click event, call the code:

Private Sub Command0_Click()

Me.Text1.SetFocus

Form_Form2.test

Me.Text1.SetFocus

End Sub

There is no error. I am using Access 2002 with Windows XP Professional. If
you have repro steps, please feel free to let me know.



Sincerely,

Alick Ye, MCSD
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.





--------------------
| From: "Joshua A. Booker" <[email protected]>
| Subject: 'Me' object no longer set
| Date: Wed, 13 Aug 2003 12:36:22 -0400
| X-Tomcat-NG: microsoft.public.access.modulesdaovba.ado
|
| Hi,
|
| The following error ocurrs after I call a public sub on another form, then
| try to refer to the calling form using the me keyword.
|
| Runtime Error 2467: 'The expression you entered refers to an object that
is
| closed or no longer exists'.
|
| For Example:
|
| Private Sub Button_Click()
| Me.Textbox1.SetFocus 'this line would not cause error
| Form_OtherFormName.PublicSubName ' this code runs fine
| Me.Textbox1.SetFocus 'this line would cause error
| End Sub
|
| I assume the me object got lost when I called the other form, how do I
| prevent this or reset the 'Me' object?
|
| TIA,
| Josh
|
|
|
 
A

Alick [MSFT]

Hi Joshua,

Thanks for sharing the resolution with us :- )



Sincerely,

Alick Ye, MCSD
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Joshua A. Booker" <[email protected]>
| References: <[email protected]>
<[email protected]>
|
| Hi Everyone,
|
| After further testing I found that the public sub was resetting the source
| object of the sub form from which I called it, so the form closed and
| reopened thus losing the reference to the Me object. I find it suprising
| that Access allows this to happen while code is running behind a form.
|
| Thank you to all who offered suggestions.
|
| Josh
|
| | > Hi Joshua,
| >
| > I did a test, the issue does not occur.
| >
| > 1. Create a new database with two forms.
| > 2. Form1 with one button and text box, and Form2 with one textbox.
| > 3. create a sub in form2
| >
| > Public Sub test()
| >
| > Debug.Print "hello"
| >
| > End Sub
| >
| > 4. In form1 button click event, call the code:
| >
| > Private Sub Command0_Click()
| >
| > Me.Text1.SetFocus
| >
| > Form_Form2.test
| >
| > Me.Text1.SetFocus
| >
| > End Sub
| >
| > There is no error. I am using Access 2002 with Windows XP Professional.
If
| > you have repro steps, please feel free to let me know.
| >
| >
| >
| > Sincerely,
| >
| > Alick Ye, MCSD
| > Microsoft Online Partner Support
| >
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| >
| >
| >
| >
| > --------------------
| > | From: "Joshua A. Booker" <[email protected]>
| > | Subject: 'Me' object no longer set
| > | Date: Wed, 13 Aug 2003 12:36:22 -0400
| > | X-Tomcat-NG: microsoft.public.access.modulesdaovba.ado
| > |
| > | Hi,
| > |
| > | The following error ocurrs after I call a public sub on another form,
| then
| > | try to refer to the calling form using the me keyword.
| > |
| > | Runtime Error 2467: 'The expression you entered refers to an object
that
| > is
| > | closed or no longer exists'.
| > |
| > | For Example:
| > |
| > | Private Sub Button_Click()
| > | Me.Textbox1.SetFocus 'this line would not cause error
| > | Form_OtherFormName.PublicSubName ' this code runs fine
| > | Me.Textbox1.SetFocus 'this line would cause error
| > | End Sub
| > |
| > | I assume the me object got lost when I called the other form, how do I
| > | prevent this or reset the 'Me' object?
| > |
| > | TIA,
| > | Josh
| > |
| > |
| > |
| >
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top