Block If without End IF (1 Viewer)

jjake

Registered User.
Local time
Today, 11:43
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
 

sneuberg

AWF VIP
Local time
Today, 09:43
Joined
Oct 17, 2014
Messages
3,506
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
 

static

Registered User.
Local time
Today, 17:43
Joined
Nov 2, 2015
Messages
823
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
 

jjake

Registered User.
Local time
Today, 11:43
Joined
Oct 8, 2015
Messages
291
I see there are many ways to skin this cat. Thanks all for the input.
 

stopher

AWF VIP
Local time
Today, 17:43
Joined
Feb 1, 2006
Messages
2,396
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

Top Bottom