Solved Block If without End If (1 Viewer)

maff811

Registered User.
Local time
Today, 11:53
Joined
May 1, 2014
Messages
45
Hi all,

I have a form with two option buttons (RentCharge and OtherCharge). I have code that clears OtherCharge when RentCharge is selected and vice versa.

When I click the save button on the form, I have code that runs a series of checks - below.

Unfortunately, I get the Block End without End If error and cannot find where I am going wrong.

Any advice would be very much appreciated.

Code:
Private Sub cmdSaveRentCharge_Click()

If RentCharge = True Then

    If IsNull(StartDate) Then
    msgbox "Please enter a start date.", vbOKOnly, "Weekly Rent Charge"
    StartDate.SetFocus

    ElseIf IsNull(FinishDate) Then
    msgbox "Please enter a finish date.", vbOKOnly, "Weekly Rent Charge"
    FinishDate.SetFocus

    ElseIf StartDate > FinishDate Then
    msgbox "Please check the dates - the start date must be on or before the finish date.", vbOKOnly, "Weekly Rent Charge"
    StartDate.SetFocus
    
    ElseIf IsNull(Amount) Then
    msgbox "Please enter an amount.", vbOKOnly, "Weekly Rent Charge"
    Amount.SetFocus

    Me.RentCharge.Enabled = False
    Me.RentCharge.Locked = True
    Me.OtherCharge.Enabled = False
    Me.OtherCharge.Locked = True
    Me.StartDate.Enabled = False
    Me.StartDate.Locked = True
    Me.FinishDate.Enabled = False
    Me.FinishDate.Locked = True
    Me.Amount.Enabled = False
    Me.Amount.Locked = True
    Me.Notes.Enabled = False
    Me.Notes.Locked = True
    Me.cmdEditRentCharge.Enabled = True
    Me.cmdSaveRentCharge.Enabled = False
    Me.cmdClose.Enabled = True

Else

    If IsNull(StartDate) Then
    msgbox "Please enter the date the expense was incurred.", vbOKOnly, "Weekly Rent Charge"
    StartDate.SetFocus

    ElseIf IsNull(Amount) Then
    msgbox "Please enter the expense amount.", vbOKOnly, "Weekly Rent Charge"
    Amount.SetFocus

    ElseIf IsNull(Notes) Then
    msgbox "Please enter an expense description.", vbOKOnly, "Weekly Rent Charge"
    Notes.SetFocus
    
    Me.RentCharge.Enabled = False
    Me.RentCharge.Locked = True
    Me.OtherCharge.Enabled = False
    Me.OtherCharge.Locked = True
    Me.StartDate.Enabled = False
    Me.StartDate.Locked = True
    Me.FinishDate.Enabled = False
    Me.FinishDate.Locked = True
    Me.Amount.Enabled = False
    Me.Amount.Locked = True
    Me.Notes.Enabled = False
    Me.Notes.Locked = True
    Me.cmdEditRentCharge.Enabled = True
    Me.cmdSaveRentCharge.Enabled = False
    Me.cmdClose.Enabled = True

    End If

End If

End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 20:53
Joined
Oct 29, 2018
Messages
21,453
Hi. There should be an End If before the Else line.
 

Minty

AWF VIP
Local time
Today, 04:53
Joined
Jul 26, 2013
Messages
10,366
No disrespect to the OP here, it's simply a rainy Saturday observation;
I know it means slightly more code, but am I the only one here that prefers the use of a repeated simple If Then (single Else if required) End If and actively dislikes the use of the ElseIf construct as confusing and unhelpful?
 

maff811

Registered User.
Local time
Today, 11:53
Joined
May 1, 2014
Messages
45
Thanks theDBguy.

I also found that I needed to add Else after the two ElseIf statements.

All working now!
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 20:53
Joined
Oct 29, 2018
Messages
21,453
Thanks theDBguy.

I also found that I needed to add Else after the two ElseIf statements.

All working now!
Hi. Glad to hear you got it sorted out. Good luck with your project.
 

maff811

Registered User.
Local time
Today, 11:53
Joined
May 1, 2014
Messages
45
No disrespect to the OP here, it's simply a rainy Saturday observation;
I know it means slightly more code, but am I the only one here that prefers the use of a repeated simple If Then (single Else if required) End If and actively dislikes the use of the ElseIf construct as confusing and unhelpful?

Hi Minty,

I'm very much an amateur coder, so use what makes most sense to me. I might google your suggestion to satisfy my own curiosity and see if that will serve my needs into the future.

Cheers
 

Gasman

Enthusiastic Amateur
Local time
Today, 04:53
Joined
Sep 21, 2011
Messages
14,231
No disrespect to the OP here, it's simply a rainy Saturday observation;
I know it means slightly more code, but am I the only one here that prefers the use of a repeated simple If Then (single Else if required) End If and actively dislikes the use of the ElseIf construct as confusing and unhelpful?
I am pretty sure it is not something | have ever used either.

I would have also indented the code properly as well, so anything like that would jump out a lot easier?

I use this now in my Access/Excel code https://www.add-ins.com/macro-produ...to-indent-vba-code/how-to-indent-vba-code.htm Comes in handy when copying other people's code.
Code:
Private Sub cmdSaveRentCharge_Click()

    If RentCharge = True Then

        If IsNull(StartDate) Then
            MsgBox "Please enter a start date.", vbOKOnly, "Weekly Rent Charge"
            StartDate.SetFocus

        ElseIf IsNull(FinishDate) Then
            MsgBox "Please enter a finish date.", vbOKOnly, "Weekly Rent Charge"
            FinishDate.SetFocus

        ElseIf StartDate > FinishDate Then
            MsgBox "Please check the dates - the start date must be on or before the finish date.", vbOKOnly, "Weekly Rent Charge"
            StartDate.SetFocus

        ElseIf IsNull(Amount) Then
            MsgBox "Please enter an amount.", vbOKOnly, "Weekly Rent Charge"
            Amount.SetFocus

            Me.RentCharge.Enabled = False
            Me.RentCharge.Locked = True
            Me.OtherCharge.Enabled = False
            Me.OtherCharge.Locked = True
            Me.StartDate.Enabled = False
            Me.StartDate.Locked = True
            Me.FinishDate.Enabled = False
            Me.FinishDate.Locked = True
            Me.Amount.Enabled = False
            Me.Amount.Locked = True
            Me.Notes.Enabled = False
            Me.Notes.Locked = True
            Me.cmdEditRentCharge.Enabled = True
            Me.cmdSaveRentCharge.Enabled = False
            Me.cmdClose.Enabled = True

        Else

            If IsNull(StartDate) Then
                MsgBox "Please enter the date the expense was incurred.", vbOKOnly, "Weekly Rent Charge"
                StartDate.SetFocus

            ElseIf IsNull(Amount) Then
                MsgBox "Please enter the expense amount.", vbOKOnly, "Weekly Rent Charge"
                Amount.SetFocus

            ElseIf IsNull(Notes) Then
                MsgBox "Please enter an expense description.", vbOKOnly, "Weekly Rent Charge"
                Notes.SetFocus

                Me.RentCharge.Enabled = False
                Me.RentCharge.Locked = True
                Me.OtherCharge.Enabled = False
                Me.OtherCharge.Locked = True
                Me.StartDate.Enabled = False
                Me.StartDate.Locked = True
                Me.FinishDate.Enabled = False
                Me.FinishDate.Locked = True
                Me.Amount.Enabled = False
                Me.Amount.Locked = True
                Me.Notes.Enabled = False
                Me.Notes.Locked = True
                Me.cmdEditRentCharge.Enabled = True
                Me.cmdSaveRentCharge.Enabled = False
                Me.cmdClose.Enabled = True

            End If

        End If

    End Sub
 

Minty

AWF VIP
Local time
Today, 04:53
Joined
Jul 26, 2013
Messages
10,366
I appreciate your response and it's not a bad way to do it. As I said - it's more of a personal bugbear, I will always use a select case statement if I need to handle multiple possibilities of a test as it simply reads better IMVHO.

You are testing for something that doesn't lend itself well to a select case statement, however, I would possibly do want you wanted more like the code below, as currently if you have missed multiple entries you'll get a message for each one separately - this combines it all into one and avoids the duplication (It is air code - untested but you should get the idea)
Code:
Private Sub cmdSaveRentCharge_Click()

Dim sMsg as String
Dim sTitle as String

    If RentCharge = True Then

        If IsNull(StartDate) Then     sMsg = "Start date" & vbCrLf
        If IsNull(FinishDate) Then sMsg = sMsg & "Finish date" & vbCrLf
        If IsNull(Amount) Then sMsg = sMsg & "Weekly Rent Amount" & vbCrLf
        IF StartDate > FinishDate Then sMsg =  sMsg & "Please check the dates - the start date must be on or before the finish date." 
        sTitle = "Weekly Rent Charge"
    Else   
        If IsNull(StartDate) Then  sMsg = "Expense date" & vbCrLf
        If IsNull(Amount) Then sMsg = sMsg & "Expense Amount" & vbCrLf
        If IsNull(Notes) Then sMsg = sMsg & "Expense Discription" & vbCrLf
        sTitle = "Expense Entry"
    End If
    
    If Len(sMsg)> 0 Then 
        msgbox "The following Items are missing;" & vbCrLf & sMsg, vbOKOnly, sTitle
    End If
    
    Me.RentCharge.Enabled = False
    Me.RentCharge.Locked = True
    Me.OtherCharge.Enabled = False
    Me.OtherCharge.Locked = True
    Me.StartDate.Enabled = False
    Me.StartDate.Locked = True
    Me.FinishDate.Enabled = False
    Me.FinishDate.Locked = True
    Me.Amount.Enabled = False
    Me.Amount.Locked = True
    Me.Notes.Enabled = False
    Me.Notes.Locked = True
    Me.cmdEditRentCharge.Enabled = True
    Me.cmdSaveRentCharge.Enabled = False
    Me.cmdClose.Enabled = True

End Sub
 
Last edited:

maff811

Registered User.
Local time
Today, 11:53
Joined
May 1, 2014
Messages
45
I appreciate your response and it's not a bad way to do it. As I said - it's more of a personal bugbear, I will always use a select case statement if I need to handle multiple possibilities of a test as it simply reads better IMVHO.

You are testing for something that doesn't lend itself well to a select case statement, however, I would possibly do want you wanted more like the code below, as currently if you have missed multiple entries you'll get a message for each one separately - this combines it all into one and avoids the duplication (It is air code - untested but you should get the idea)
Code:
Private Sub cmdSaveRentCharge_Click()

Dim sMsg as String
Dim sTitle as String

    If RentCharge = True Then

        If IsNull(StartDate) Then     sMsg = "Start date" & vbCrLf
        If IsNull(FinishDate) Then sMsg = sMsg & "Finish date" & vbCrLf
        If IsNull(Amount) Then sMsg = sMsg & "Weekly Rent Amount" & vbCrLf
        IF StartDate > FinishDate Then sMsg =  sMsg & "Please check the dates - the start date must be on or before the finish date."
        sTitle = "Weekly Rent Charge"
    Else  
        If IsNull(StartDate) Then  sMsg = "Expense date" & vbCrLf
        If IsNull(Amount) Then sMsg = sMsg & "Expense Amount" & vbCrLf
        If IsNull(Notes) Then sMsg = sMsg & "Expense Discription" & vbCrLf
        sTitle = "Expense Entry"
    End If
   
    If Len(sMsg)> 0 Then
        msgbox "The following Items are missing;" & vbCrLf & sMsg, vbOKOnly, sTitle
    End If
   
    Me.RentCharge.Enabled = False
    Me.RentCharge.Locked = True
    Me.OtherCharge.Enabled = False
    Me.OtherCharge.Locked = True
    Me.StartDate.Enabled = False
    Me.StartDate.Locked = True
    Me.FinishDate.Enabled = False
    Me.FinishDate.Locked = True
    Me.Amount.Enabled = False
    Me.Amount.Locked = True
    Me.Notes.Enabled = False
    Me.Notes.Locked = True
    Me.cmdEditRentCharge.Enabled = True
    Me.cmdSaveRentCharge.Enabled = False
    Me.cmdClose.Enabled = True

End Sub

Thanks Minty, I see your point. That would obviously be the more advanced way of coding it, as opposed to my beginner approach! I might have a play with that once I get the rest of it working and see how that goes.
 

maff811

Registered User.
Local time
Today, 11:53
Joined
May 1, 2014
Messages
45
I am pretty sure it is not something | have ever used either.

I would have also indented the code properly as well, so anything like that would jump out a lot easier?

I use this now in my Access/Excel code https://www.add-ins.com/macro-produ...to-indent-vba-code/how-to-indent-vba-code.htm Comes in handy when copying other people's code.
Code:
Private Sub cmdSaveRentCharge_Click()

    If RentCharge = True Then

        If IsNull(StartDate) Then
            MsgBox "Please enter a start date.", vbOKOnly, "Weekly Rent Charge"
            StartDate.SetFocus

        ElseIf IsNull(FinishDate) Then
            MsgBox "Please enter a finish date.", vbOKOnly, "Weekly Rent Charge"
            FinishDate.SetFocus

        ElseIf StartDate > FinishDate Then
            MsgBox "Please check the dates - the start date must be on or before the finish date.", vbOKOnly, "Weekly Rent Charge"
            StartDate.SetFocus

        ElseIf IsNull(Amount) Then
            MsgBox "Please enter an amount.", vbOKOnly, "Weekly Rent Charge"
            Amount.SetFocus

            Me.RentCharge.Enabled = False
            Me.RentCharge.Locked = True
            Me.OtherCharge.Enabled = False
            Me.OtherCharge.Locked = True
            Me.StartDate.Enabled = False
            Me.StartDate.Locked = True
            Me.FinishDate.Enabled = False
            Me.FinishDate.Locked = True
            Me.Amount.Enabled = False
            Me.Amount.Locked = True
            Me.Notes.Enabled = False
            Me.Notes.Locked = True
            Me.cmdEditRentCharge.Enabled = True
            Me.cmdSaveRentCharge.Enabled = False
            Me.cmdClose.Enabled = True

        Else

            If IsNull(StartDate) Then
                MsgBox "Please enter the date the expense was incurred.", vbOKOnly, "Weekly Rent Charge"
                StartDate.SetFocus

            ElseIf IsNull(Amount) Then
                MsgBox "Please enter the expense amount.", vbOKOnly, "Weekly Rent Charge"
                Amount.SetFocus

            ElseIf IsNull(Notes) Then
                MsgBox "Please enter an expense description.", vbOKOnly, "Weekly Rent Charge"
                Notes.SetFocus

                Me.RentCharge.Enabled = False
                Me.RentCharge.Locked = True
                Me.OtherCharge.Enabled = False
                Me.OtherCharge.Locked = True
                Me.StartDate.Enabled = False
                Me.StartDate.Locked = True
                Me.FinishDate.Enabled = False
                Me.FinishDate.Locked = True
                Me.Amount.Enabled = False
                Me.Amount.Locked = True
                Me.Notes.Enabled = False
                Me.Notes.Locked = True
                Me.cmdEditRentCharge.Enabled = True
                Me.cmdSaveRentCharge.Enabled = False
                Me.cmdClose.Enabled = True

            End If

        End If

    End Sub


Thanks Gasman. I'll check that link out.
 

Micron

AWF VIP
Local time
Yesterday, 23:53
Joined
Oct 20, 2018
Messages
3,478
am I the only one here that prefers the use of a repeated simple
Yer not alone. I'll use 1 Else if I can only see one other possible result other than the first test. I also try to code so that the test results in execution of code or exits a block if not the whole procedure. Sometimes that requires If Not rather than just If.
 

Minty

AWF VIP
Local time
Today, 04:53
Joined
Jul 26, 2013
Messages
10,366
Thanks Minty, I see your point. That would obviously be the more advanced way of coding it, as opposed to my beginner approach! I might have a play with that once I get the rest of it working and see how that goes.

Good luck with your project.
One other point that I missed yesterday, if a control is disabled, there is no point locking it as well. That would also save your typing fingers... ;)
 

Users who are viewing this thread

Top Bottom