Another question on else function! Please help (1 Viewer)

monk_1987

Registered User.
Local time
Today, 05:32
Joined
Oct 5, 2012
Messages
31
Hi

I have a problem which has been bugging me for a while now, so thought I would sign up here to get it sorted! So here it goes.

I have a form with a close button on it. I have written a code for it which is as follows. (It has been condenced though)

if isnull (FirstName) and isnull (LastName) then

if msgbox "Are you sure you want to close? This record will not be saved", vbyesnocancel) = vbyes

docmd.runcommand accmdundo
docmd.close
docmd.openform "F_MainMenu"

else

If not isnull (FirstName) and not isnull (LastName) then

if msgbox ("Do you want close?, vbyesnocancel) = vbyes then
docmd.close
docmd.openform "F_MainMenu"
end if
end if
end if
end if

The else function doesn't work. It regards the else as the 'no' and 'cancel' of the first message box!

Can someone help me please?

This isn't the full code! If you need it, please let me know!

Thanks
Rhys
 

DrallocD

Registered User.
Local time
Today, 00:32
Joined
Jul 16, 2012
Messages
112
Did you consider doing this in the form_unload event so that the same logic fires however the form is closed?

You should post the whole code as the code you posted has too many syntax and logic bugs in it.

Please wrap the code in code tags to make it easier to read.
 

mahenkj2

Registered User.
Local time
Today, 10:02
Joined
Apr 20, 2012
Messages
459
The whole code we do not know, but based on your code shown

If IsNull(firstname) And IsNull(lastname) Then

If MsgBox("Are you sure you want to close? This record will not be saved", vbYesNo) = vbYes Then

DoCmd.RunCommand acCmdUndo
DoCmd.Close
DoCmd.OpenForm "F_MainMenu"

End If

Else

If Not IsNull(firstname) And Not IsNull(lastname) Then

If MsgBox("Do you want close?", vbYesNo) = vbYes Then
DoCmd.Close
DoCmd.OpenForm "F_MainMenu"
End If

End If
 

monk_1987

Registered User.
Local time
Today, 05:32
Joined
Oct 5, 2012
Messages
31
Hi guys

Sorry about the lack of code before. Here is is below though!

On Error GoTo Err_Handler

If IsNull(NameofSchool) Or IsNull(SchoolType) Or IsNull(SchoolAddress) Or IsNull(SchoolTown) _
Or IsNull(SchoolCounty) Or IsNull(SchoolPostalCode) Or IsNull(SchoolPhoneNumber) _
Or IsNull(FirstName) Or IsNull(LastName) Or IsNull(Group) _
Or IsNull(ContractStart) Or IsNull(ContractEnd) Or IsNull(TravelDistance) Or IsNull(Instrument) Or IsNull(ClassSize) Or IsNull(AgeGroup) _
Or IsNull(Kit) Then

If MsgBox("If you close you cannot save this record at this time!" & _
vbCrLf & vbCrLf & "If you want to proceed the current record will be deleted!" & _
vbCrLf & vbCrLf & "Do you want to proceed and close?", vbYesNoCancel, "Drumming Solutions") = vbYes Then
DoCmd.RunCommand acCmdUndo
DoCmd.Close acForm, "F_Educational_Brandnewschool"
DoCmd.OpenForm "F_Educational_MainMenu"

Else

If Not IsNull(NameofSchool) Or Not IsNull(SchoolType) Or Not IsNull(SchoolAddress) Or Not IsNull(SchoolTown) _
Or Not IsNull(SchoolCounty) Or Not IsNull(SchoolPostalCode) Or Not IsNull(SchoolPhoneNumber) _
Or Not IsNull(FirstName) Or Not IsNull(LastName) Or Not IsNull(Group) _
Or Not IsNull(ContractStart) Or Not IsNull(ContractEnd) Or Not IsNull(TravelDistance) Or Not IsNull(Instrument) Or Not IsNull(ClassSize) _
Or Not IsNull(AgeGroup) Or Not IsNull(Kit) Then

If MsgBox("Do you want to proceed and close?", vbYesNoCancel, "Drumming Solutions") = vbYes Then
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close acForm, "F_Educational_Brandnewschool"
DoCmd.OpenForm "F_Educational_MainMenu"
End If
End If
End If
End If

Err_Handler:
If Err.Number = 2467 Then
End If

End Sub

When you click on yes in the first message box it works fine! But if you click no or cancel that is when the else function comes into play!

I hope this makes more sence!! Again thanks for the replies!
 

DrallocD

Registered User.
Local time
Today, 00:32
Joined
Jul 16, 2012
Messages
112
mahenkj2 has the correct answer - you needed to move up one of the endif's. Here is the code which I reformatted to make it clearer to see the logic.

Code:
    Const CloseWarning As String = "If you close you cannot save this record at this time!" & vbCrLf & vbCrLf & _
                                   "If you want to proceed the current record will be deleted!" & vbCrLf & vbCrLf & _
                                   "Do you want to proceed and close?"
    Dim missingValue As Boolean
    missingValue = IsNull(NameofSchool) Or IsNull(SchoolType) Or IsNull(SchoolAddress) Or IsNull(SchoolTown) _
                Or IsNull(SchoolCounty) Or IsNull(SchoolPostalCode) Or IsNull(SchoolPhoneNumber) _
                Or IsNull(FirstName) Or IsNull(LastName) Or IsNull(Group) _
                Or IsNull(ContractStart) Or IsNull(ContractEnd) Or IsNull(TravelDistance) _
                Or IsNull(Instrument) Or IsNull(ClassSize) Or IsNull(AgeGroup) Or IsNull(Kit)
 
    If missingValue Then
        If MsgBox(CloseWarning, vbYesNoCancel, "Drumming Solutions") = vbYes Then
            DoCmd.RunCommand acCmdUndo
            DoCmd.Close acForm, "F_Educational_Brandnewschool"
            DoCmd.OpenForm "F_Educational_MainMenu"
        End If
    Else
        If MsgBox("Do you want to proceed and close?", vbYesNoCancel, "Drumming Solutions") = vbYes Then
            DoCmd.RunCommand acCmdSaveRecord
            DoCmd.Close acForm, "F_Educational_Brandnewschool"
            DoCmd.OpenForm "F_Educational_MainMenu"
        End If
    End If
End Sub
 
Last edited:

monk_1987

Registered User.
Local time
Today, 05:32
Joined
Oct 5, 2012
Messages
31
Thank you so much for that DrallocD. It works an absolute treat! That has been driving me nuts for ages!
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 00:32
Joined
Feb 19, 2002
Messages
43,257
Rather than using DoCmd for these actions, use methods of Me -
Me.Undo
Me.Close

Also, and this is IMPORTANT, your validation code belongs in the Form's BeforeUpdate event. That is the ONLY event that will prevent bad data from being saved. The code were you have it wouldn't trap errors if the user scrolled to a new record or clicked into a subform (if the form had one) since both of those events would force the current record to be saved but wouldn't involve closing the form.

@DrallocD - welcome - I see that you live in Connecticut. Stamford is a little far but you might consider joining us at the CTAUG meetings the second Wednesday (that would be this week) in Farmington at the MS Office. Check out the website CTAUG.ORG (it looks like it still says September but the address is correct) and our group on Yahoo. I'll be speaking this month to demo some of my utility databases.
 

monk_1987

Registered User.
Local time
Today, 05:32
Joined
Oct 5, 2012
Messages
31
Thanks Pat. I will do that!

Guys thanks so much for your help. I can't believe how willing people are to help you on here. Should have joined sooner!
 

Users who are viewing this thread

Top Bottom