If Statements

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

If Statements

Post by D Willett »

Hi
I have a couple of nested IF statements here, all seems to work ok but just wondered if I have my "End If" statements in the correct place.
Just don't want it falling over when I'm on my holidays next week.

Code: Select all

Private Sub cmdNewFolder_Click()

Dim strNewFolder As String
        
      strNewFolder = "L:\MMPDF\ConsoleFiles\" & Me.txtEst & "\"
      
      If Me.txtEst.Text = "" Then
      MsgBox "You Cannot Use This Function Unless You" & vbCrLf & "Specify A Valid Job Number", vbInformation, "Create Folder"
      Exit Sub
      Else
      
        If Dir(strNewFolder, vbDirectory) = "" Then
            MkDir strNewFolder
            
        If MsgBox("New Folder Created For Job Number " & Me.txtEst.Text & vbCrLf & _
        " Do You Want To Open The Folder?", vbYesNo + vbQuestion, " Folder Created") = vbYes Then
           
       'MsgBox "New Folder Created For Job Number " & Me.txtEst & vbCrLf & _
       '     "Your New Folder Will Now Open", vbInformation, "New Folder"
        
        With CmnDialog1
        '~~> Set the Title of the Dialog Box
        .DialogTitle = "Open New Folder"
        '~~> Set the Initial directory
        .InitDir = "L:\MMPDF\ConsoleFiles\" & Me.txtEst.Text & "\"
        '~~> Set the Filter for the files that you want to open
        .FileName = ""
        .Filter = "All Files (*.*)"
        '~~> Displays the Open Dialog Box
        .ShowOpen
    End With
    End If
    
            
        Else
                
        If MsgBox("Folder " & Me.txtEst & " Already Exists " & _
        " Do You Want To View The Folder And It's Contents !!?", vbYesNo + vbQuestion, " Folder Exists") = vbYes Then
                     
        With CmnDialog1
        '~~> Set the Title of the Dialog Box
        .DialogTitle = "Open New Folder"
        '~~> Set the Initial directory
        .InitDir = "L:\MMPDF\ConsoleFiles\" & Me.txtEst.Text & "\"
        '~~> Set the Filter for the files that you want to open
        .FileName = ""
        .Filter = "All Files (*.*)"
        '~~> Displays the Open Dialog Box
        .ShowOpen
        End With
        
        
       
       End If
       End If
       End If
End Sub
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78568
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: If Statements

Post by HansV »

Yes, that looks OK. But it's much easier to check the structure if the code is indented consistently. In the following version, the code itself is exactly the same, but you can quickly see which End If belongs to which If ... Then:

Code: Select all

Private Sub cmdNewFolder_Click()
    Dim strNewFolder As String

    strNewFolder = "L:\MMPDF\ConsoleFiles\" & Me.txtEst & "\"

    If Me.txtEst.Text = "" Then
        MsgBox "You Cannot Use This Function Unless You" & vbCrLf & "Specify A Valid Job Number", vbInformation, "Create Folder"
        Exit Sub
    Else
        If Dir(strNewFolder, vbDirectory) = "" Then
            MkDir strNewFolder
            If MsgBox("New Folder Created For Job Number " & Me.txtEst.Text & vbCrLf & _
                    " Do You Want To Open The Folder?", vbYesNo + vbQuestion, " Folder Created") = vbYes Then
                With CmnDialog1
                    '~~> Set the Title of the Dialog Box
                    .DialogTitle = "Open New Folder"
                    '~~> Set the Initial directory
                    .InitDir = "L:\MMPDF\ConsoleFiles\" & Me.txtEst.Text & "\"
                    '~~> Set the Filter for the files that you want to open
                    .Filename = ""
                    .Filter = "All Files (*.*)"
                    '~~> Displays the Open Dialog Box
                    .ShowOpen
                End With
            End If
        Else
            If MsgBox("Folder " & Me.txtEst & " Already Exists " & _
                    " Do You Want To View The Folder And It's Contents !!?", vbYesNo + vbQuestion, " Folder Exists") = vbYes Then
                With CmnDialog1
                    '~~> Set the Title of the Dialog Box
                    .DialogTitle = "Open New Folder"
                    '~~> Set the Initial directory
                    .InitDir = "L:\MMPDF\ConsoleFiles\" & Me.txtEst.Text & "\"
                    '~~> Set the Filter for the files that you want to open
                    .Filename = ""
                    .Filter = "All Files (*.*)"
                    '~~> Displays the Open Dialog Box
                    .ShowOpen
                End With
            End If
       End If
    End If
End Sub
You can shorten the code slightly by using ElseIf:

Code: Select all

Private Sub cmdNewFolder_Click()
    Dim strNewFolder As String

    strNewFolder = "L:\MMPDF\ConsoleFiles\" & Me.txtEst & "\"

    If Me.txtEst.Text = "" Then
        MsgBox "You Cannot Use This Function Unless You" & vbCrLf & "Specify A Valid Job Number", vbInformation, "Create Folder"
    ElseIf Dir(strNewFolder, vbDirectory) = "" Then
        MkDir strNewFolder
        If MsgBox("New Folder Created For Job Number " & Me.txtEst.Text & vbCrLf & _
                " Do You Want To Open The Folder?", vbYesNo + vbQuestion, " Folder Created") = vbYes Then
            With CmnDialog1
                '~~> Set the Title of the Dialog Box
                .DialogTitle = "Open New Folder"
                '~~> Set the Initial directory
                .InitDir = "L:\MMPDF\ConsoleFiles\" & Me.txtEst.Text & "\"
                '~~> Set the Filter for the files that you want to open
                .Filename = ""
                .Filter = "All Files (*.*)"
                '~~> Displays the Open Dialog Box
                .ShowOpen
            End With
        End If
    ElseIf MsgBox("Folder " & Me.txtEst & " Already Exists " & _
            " Do You Want To View The Folder And It's Contents !!?", vbYesNo + vbQuestion, " Folder Exists") = vbYes Then
        With CmnDialog1
            '~~> Set the Title of the Dialog Box
            .DialogTitle = "Open New Folder"
            '~~> Set the Initial directory
            .InitDir = "L:\MMPDF\ConsoleFiles\" & Me.txtEst.Text & "\"
            '~~> Set the Filter for the files that you want to open
            .Filename = ""
            .Filter = "All Files (*.*)"
            '~~> Displays the Open Dialog Box
            .ShowOpen
        End With
    End If
End Sub
But that is not essential - your code is correct "as is".
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: If Statements

Post by D Willett »

Cheers Hans.
I never got the hang of the correct indentation did I? But my coding does seem to have come along ok though.. :evilgrin:
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78568
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: If Statements

Post by HansV »

Yes, you've come a long way! :clapping:
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: If Statements

Post by D Willett »

That's because it's so addictive ............ :groan:
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78568
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: If Statements

Post by HansV »

Dave the VB junkie... :laugh:
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: If Statements

Post by D Willett »

I know .. !

Sometimes it's :grin:
Occasionally its :sad:
Now and again it's :clapping:
But when I get to the stage of :hairout: I just want to :flee:

But all in all it's very satisfying that help is just a couple of clicks away, so a big :thankyou:

:grin:
Cheers ...

Dave.