Trying to close a form

Eljefegeneo

Still trying to learn
Local time
Today, 11:13
Joined
Jan 10, 2011
Messages
902
I have a code that works up to a point. If a {Day} is filled in and there is no corresponding [Time], then a message box pops up, telling the user to insert a time. However, if no time is known and wants to just save the [Day] data, the form should close.

If Not IsNull(Day) And (IsNull(Time) Or Time = "") Then
MsgBox "Please make a selection for Time", vbOKCancel, "Time Selection Not Complete!"
If vbOK Then
DoCmd.CancelEvent
Me.Time.SetFocus
Else
DoCmd.Close acform, "frm1", acSave
End If
End If


I can't get the second part, the else part to work. The form won't close, the focus goes to [Time]. What am I doing wrong?
 
You didn't post the entire event so we don't know which one you have used. But it is the Before Update event you should be using and then you don't use DoCmd.CancelEvent but you use

Cancel = True


Also, a couple of other things. Your field names are using Access Reserved Words (Day, Time) so that can also be causing you grief. It is best to not use Access Reserved Words as object names or field names.

Also, I don't think you know what acSave actually means. That has nothing to do with records. That has to do with saving DESIGN CHANGES to the form. If this is in the Before Update event you do not need to save as it will only be fired if something happens to try to save a record and then if it passes your validation you can just let it go.
 
The event was in the OnUnload and the [Day] and [Time] are actually five different controls, Day1, Day2, etc. and Time1, Time2, etc.

I used the Form OnUnload since nothing would work on the Onclose event.
 
Rewritten with the Access Reserved words changed to something else would be:

Code:
    If Len(Me.txtDay & vbNullString) > 0 And Len(Me.txtTime & vbNullString) = 0 Then
        If MsgBox("Please make a selection for Time", vbOKCancel, "Time Selection Not Complete!") = vbOK Then
            Cancel = True
            Me.txtTime.SetFocus
        Else
            Cancel = True
            Me.Undo
            DoCmd.Close acForm, "frm1", acSaveNo
        End If
    End If
 
The event was in the OnUnload and the [Day] and [Time] are actually five different controls, Day1, Day2, etc. and Time1, Time2, etc.

I used the Form OnUnload since nothing would work on the Onclose event.
You want the Before Update event. If you use the On Unload event the record has already saved.
 
I tried it in the form Before Update event and it doesn't work. Thanks for trying.
 
I tried it in the form Before Update event and it doesn't work. Thanks for trying.

First off, we need to get it to work in the Before Update event because that's where it goes. It isn't a matter of "thanks for trying." If you can't get it to work then something is not right and we need to fix it.

So, when you say it doesn't work, what actually does that mean? Have you set a breakpoint and stepped through, watching the values of things and where it actually steps through which can yield results.
 
One more thing -

you can make this more generic:

DoCmd.Close acForm, "frm1", acSaveNo

By using

DoCmd.Close acForm, Me.Name, acSaveNo

if that code is on the form that is needing to be closed, you don't need to type the name. Just use Me.Name.
 
Didn't mean to say that it couldn't be done. Sorry, Attached is a sample of what I am trying to do.

In essence, if a DayX is filled in and a TimeX is not, then a message box should pop up saying fill in the time. If "Cancelled", the form should close saving the DayX and not demanding a TimeX.
 

Attachments

...I tried it in the form Before Update event and it doesn't work. Thanks for trying...
Just to reiterate what Bob has said, this belongs in the Form_BeforeUpdate event...period! This is not just Bob's opinion, this is a fact!

As he said, we need to figure out why it's not working for you like you want it to.

Am I correct in thinking that you want the Form to save, even if no time has been entered? In other words, you want a message to remind the user that the time control is empty, and give them a chance to correct this, but you want to save it whether or not they correct the omission? (I can't open this file version)

Linq ;0)>
 
Yes, that is correct. Save it even though no corresponding Time is entered.
And I now know to use the Event that you say.
Thanks.
 
Okay; Bob and I were confused, I think, because the usual scenario for this kind of thing is to not allow the Record to be saved if it is not complete!

So, building on Bob's code, with the new Control names of txtDay and txtTime, this should meet your requirement:

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

If Len(Me.txtDay & vbNullString) > 0 And Len(Me.txtTime & vbNullString) = 0 Then
  
  If MsgBox("A Time Has Not Been Entered; Would You Like to Enter it Now?", vbYesNo, "Time Selection Not Complete!") = vbYes Then
    Cancel = True
    Me.txtTime.SetFocus
  Else
    'Do nothing; let the Record be saved
  End If

End If

End Sub
Linq ;0)>
 
Thank you. I do appreciate you taking the time time to help me.
 

Users who are viewing this thread

Back
Top Bottom