Error when closing form

  • Thread starter Thread starter PaleRider
  • Start date Start date
P

PaleRider

Hi,

I have code that will close my "GlassWare" form after 14 days have passed.
For some reason when I try to close the form with code I get a different
error depending on which module the code is located OnOpen, OnLoad, ect. The
line of code that's causing the errors is the one that closes the form. If I
remove that one line, everything is fine.

Here's the code:

Private Sub Form_Open(Cancel As Integer)

Dim myDate As String

myDate = #6/2/2009#

If DateDiff("d", myDate, Date) >= 14 Then
MsgBox "Your 14 day evaluation period has expired."
End If

' This line of code causes the error
DoCmd.Close acForm, "GlassWare", acSaveNo

End Sub
 
Instead of closing the form, cancel its Open event:

Private Sub Form_Open(Cancel As Integer)
Dim myDate As Date
myDate = #6/2/2009#

If DateDiff("d", myDate, Date) >= 14 Then
MsgBox "expired"
Cancel = True
End If
End Sub
 
Hi,

I have code that will close my "GlassWare" form after 14 days have passed.
For some reason when I try to close the form with code I get a different
error depending on which module the code is located OnOpen, OnLoad, ect. The
line of code that's causing the errors is the one that closes the form. If I
remove that one line, everything is fine.

Here's the code:

Private Sub Form_Open(Cancel As Integer)

Dim myDate As String

myDate = #6/2/2009#

If DateDiff("d", myDate, Date) >= 14 Then
MsgBox "Your 14 day evaluation period has expired."
End If

' This line of code causes the error
DoCmd.Close acForm, "GlassWare", acSaveNo

End Sub

What actual error are you getting?

Note that dates aren't strings: try Dim myDate As Date instead (though that
shouldn't be causing an error with the Close).
 
Allen, John,

OK, it kind of works. I put in the "Cancel = true" part and I don't get
anymore errors but the form is still open in FormView mode. Is there a way
to just make the form disappear completely after the OnLoad event is
cancelled?

What I tried was to open a dummy form just before the cancel=true line, then
in the dummy form I put in a line to close the GlassWare form then close
itself but it won't close the GlassWare form.

Here's the code in dummy form:

(frmDummy)
Private Sub Form_Open(Cancel As Integer)
DoCmd.Close acForm, "GlassWare", acSaveNo
DoCmd.Close
End Sub


-PR
 
I don't understand that question.

The On Load event cannot be cancelled.

The On Open event runs before the form is displayed, and if you cancel it
the form never does appear.
 
Allen,

Your absolutely right. I should have closed the form completely first.
Once I closed the form and ran the test the form didn't open at all which is
what I wanted. Thank you so much for your help on this. Thank you too John
for your response as well.

-PR
 
Back
Top