Block If without End IF

jjake

Registered User.
Local time
, 18:34
Joined
Oct 8, 2015
Messages
291
I'm sure this is pretty straight forward but i can't figure it out.

Im getting a compile error, Block if without end if on the following code

Code:
Option Compare Database

Private Sub cmdOK_Click()

If Len(Me.cboPermitType & vbNullString) = 0 Then
    MsgBox "Please select a Permit Type"
    
    Else
    
If cboPermitType.Value = 1 Then
DoCmd.OpenForm "subfrmHotWork", , , , acFormAdd
DoCmd.Close acForm, "frmPermitsearch", acSaveNo

Else

If cboPermitType.Value = 2 Then
DoCmd.OpenForm "subfrmLineBreak", , , , acFormAdd
DoCmd.Close acForm, "frmPermitsearch", acSaveNo

Else

If cboPermitType.Value = 3 Then
DoCmd.OpenForm "subfrmConfinedSpace", , , , acFormAdd
DoCmd.Close acForm, "frmPermitsearch", acSaveNo

End If

End Sub
 
You can have multiple Else clauses but the nesting gets deep and you need an End If for each if. If you indent properly you can see where they are needed

Code:
If Len(Me.cboPermitType & vbNullString) = 0 Then
    MsgBox "Please select a Permit Type"
Else
    If cboPermitType.Value = 1 Then
        DoCmd.OpenForm "subfrmHotWork", , , , acFormAdd
        DoCmd.Close acForm, "frmPermitsearch", acSaveNo
    Else
        If cboPermitType.Value = 2 Then
            DoCmd.OpenForm "subfrmLineBreak", , , , acFormAdd
            DoCmd.Close acForm, "frmPermitsearch", acSaveNo
        Else
            If cboPermitType.Value = 3 Then
                DoCmd.OpenForm "subfrmConfinedSpace", , , , acFormAdd
                DoCmd.Close acForm, "frmPermitsearch", acSaveNo
            End If
        End If ' added
    End If ' added
End If  'added

But using ElseIf for this type of structure results in cleaner code.

Code:
If Len(Me.cboPermitType & vbNullString) = 0 Then
    MsgBox "Please select a Permit Type"
ElseIf cboPermitType.Value = 1 Then
    DoCmd.OpenForm "subfrmHotWork", , , , acFormAdd
    DoCmd.Close acForm, "frmPermitsearch", acSaveNo
ElseIf cboPermitType.Value = 2 Then
    DoCmd.OpenForm "subfrmLineBreak", , , , acFormAdd
    DoCmd.Close acForm, "frmPermitsearch", acSaveNo
ElseIf cboPermitType.Value = 3 Then
    DoCmd.OpenForm "subfrmConfinedSpace", , , , acFormAdd
    DoCmd.Close acForm, "frmPermitsearch", acSaveNo
End If
 
Or select case for even cleaner code

Code:
If Len(Me.cboPermitType & vbNullString) = 0 Then
    MsgBox "Please select a Permit Type"
    
Else
    
	select case cboPermitType.Value
	case 1 
		DoCmd.OpenForm "subfrmHotWork", , , , acFormAdd
	case 2
		DoCmd.OpenForm "subfrmLineBreak", , , , acFormAdd
	case 3
		DoCmd.OpenForm "subfrmConfinedSpace", , , , acFormAdd
	end select
	
	select case cboPermitType.Value
	case 1,2,3
		DoCmd.Close acForm, "frmPermitsearch", acSaveNo
	end select

End If
 
I see there are many ways to skin this cat. Thanks all for the input.
 
Here's another way...

Code:
sbfrm = Choose(Nz(Me.cboPermitType, 0), "subfrmHotWork", "subfrmLineBreak", "subfrmConfinedSpace")

If IsNull(sbfrm) Then
    MsgBox "Please select a Permit Type"
Else
    DoCmd.OpenForm sbfrm, , , , acFormAdd
    DoCmd.Close acForm, "frmPermitsearch", acSaveNo
End If
 

Users who are viewing this thread

Back
Top Bottom