Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unable to close form in the OnCurrent Event :(

Status
Not open for further replies.

nimarii

MIS
Jan 26, 2004
213
US
Hello -

I'm trying to close a form thru vba, but I'm unable to do so! This is so frustrating not being able to do such a simple thing.

What I have is that when a user creates a new record, the vba code will immediately change some data, and then prompt the user if they want to save. If they choose no, the changes are rolled back, and the form SHOULD close, but it doesn't.

Here's my code:
Code:
If Me.NewRecord Then
    Me.custid_hidden.SetFocus
    Me.custid_hidden.Text = "29"
    If MsgBox("Do you want to save?", vbYesNo) = vbNo Then
        Me.Undo
        DoCmd.Close acForm, Me.Name, acSaveNo
        Exit Sub
    Else
        DoCmd.RunCommand acCmdSaveRecord
    End If
End If

The code just seems to ignore the docmd.close line. This is in the OnCurrent Event of the form, does this have anything to do with it? Does anyone know what I'm doing wrong?

THanks!!!!
 
Hmmm,

On Current is going to fire before any data has been entered. Therefore, there wouldn't be anything to "undo". But that should not stop the form from closing.

However, Me.Name looks suspect - that's supposed to contain the name of the form. Unless you've turned error reporting off (Setwarnings = false), you should get an error message.



HTH,
Bob [morning]
 
well, even if I substitute the name of the form instead of me.name, it still doesn't work, and no error messages have occurred.

i really don't know why this won't work. I even tried creating a separate sub that is called from within the code that would close the form, but that didn't work either.

I am completely at a loss. I guess I will try to research some more about whether or not it's possible from within the OnCurrent event.
 
I understand your frustration...

But, it just cannot be that complex.

Either the form in question is not the active window when the code is executed, or the name is not what you think it is, or your database is corrupt.

The quickest possibility to eliminate is the name. Try just using DoCmd.Close - no parameters because the default name is the active object. If that works you need to identify the name. Note that you may be prompted to say whether or not you want to save changes.



HTH,
Bob [morning]
 
There are quite a few of the Form_xxx events that will not allow the DoCmd.Close event to fire.

The solution is to find some other event that you can run the code from.

If you can find no other alternative then :
Replace
DoCmd.Close acForm, Me.Name, acSaveNo

with
Me.TimerInterval = 10


And in the On_Timer event you can put
DoCmd.Close

because the OnTimer event does not have the restriction and the 10ms is plenty long enough for the Form_Current to have closed off nicely before the Timer event fires.



'ope-that-'elps.

G LS
spsinkNOJUNK@yahoo.co.uk
Remove the NOJUNK to use.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top