0

How can I use an if in another if in Visual Basic 6?

For example, in C++ I can start my statements with { and finish them with }, and I can use another if in the first statement.

For example:

if m>3
{       .....
     if a>5
      {
       ......
      }
     else {....}
} else ........

… but in Visual Basic there is no { } or something similar it

i want to writ a program that recive a time from user and Reduce 4:30 from it ! for example when user runs it , it asks : what time is it in Tehran ? and user enter time (for example 16:30) and then program Reduces 4 hour and 30 minutes from it ! and print : GMT time is : 12:00 ! i'll updat my post and put my code ! please read it !

Option Explicit

Private Sub Command1_Click()
    Cls
    Dim h As Integer, m As Integer, hm As Integer, mm As Integer, t As Integer
    Dim fh As String, fm As String, fhm As String, fmm As String

    t = InputBox("Enter time")
    h = t \ 100
    m = t Mod 100

    Select Case h

    Case 0 To 24

        If m < 60 Then
            If m >= 30 Then

                hm = h - 4
                mm = m - 30

                If h < 10 Then fh = Format(h, String(2, "0")) Else fh = h
                If m < 10 Then fm = Format(m, String(2, "0")) Else fm = m
                If hm < 10 Then fhm = Format(hm, String(2, "0")) Else fhm = hm
                If mm < 10 Then fmm = Format(mm, String(2, "0")) Else fmm = mm

                Print
                Print "      Tehran  Time is  :   "; fh; ":"; fm
                Print
                Print "      GMT       Time is  :   "; fhm; ":"; 
            Else
                hm = h - 5
                mm = m + 30

                If h < 10 Then fh = Format(h, String(2, "0")) Else fh = h
                If m < 10 Then fm = Format(m, String(2, "0")) Else fm = m
                If hm < 10 Then fhm = Format(hm, String(2, "0")) Else fhm = hm
                If mm < 10 Then fmm = Format(mm, String(2, "0")) Else fmm = mm

                Print
                Print "      Tehran  Time is  :   "; fh; ":"; fm
                Print
                Print "      GMT       Time is  :   "; fhm; ":"; fmm
            End If
        End If

    Case Else

        Beep
        Print
        Print "   Time is wrong !!!  Try again "

    End Select

End Sub

Private Sub Command2_Click()
        End
End Sub

Private Sub Form_Load()

End Sub
1
  • 1
    Just begin the second if statement under the first if statement. then set an else condition for the second if statement then end the first if statement. Since you didn't bother posting valid VB 6 I won't give an exact solution. Commented Mar 29, 2013 at 18:00

2 Answers 2

13

If clauses in VB are ended with "End If" (case insensitive). No brackets whatsoever.

You may nest as many if clauses as you want, as long as you close them:

If ... Then
    *Do stuff*

    If ... Then
        *Do more stuff*
    End If
End If
7

Single-line If statements can also be nested:

If (a) Then [a_stuff] : If (b) Then [a_and_b_stuff] Else [a_and_notb_stuff] Else [nota_stuff]

Note that no more [a_stuff] can appear after [a_and_notb_stuff], since there's no way to tell where b's Else-statements end, so nothing can appear in a clause after a nested If statement.

Any of the *_stuff parts can be empty. But you may have some difficulty with an empty [a_and_notb_stuff], since the VB6 IDE seems to be a bit temperamental here, and will try inserting an invalid third 'Else' statement there. To work around this you can put a ':' in place of [a_and_notb_stuff]. The colon will be removed by the IDE, but it won't then insert another Else statement.

Addendum: I should point out that this whole post should be treated as information, and not as a recommendation. Nesting single-line If statements like this makes for very unreadable and bug-prone code.

4
  • What is it you want an example of? A simple If-Else statement would be If n > 10 Then MsgBox "n >= 10" Else MsgBox "n <= 10". A further If-Else could be dropped in after or instead of a MsgBox statement. But to be honest, it's a bad idea to nest single-line If statements. I've updated my post to point this out.
    – mwfearnley
    Commented Mar 30, 2013 at 5:48
  • my firnd ! i want to writ a program that recive a time from user and Reduce 4:30 from it ! for example when user runs it , it asks : what time is it in Tehran ? and user enter time (for example 16:30) and then program Reduces 4 hour and 30 minutes from it ! and print : GMT time is : 12:00 ! i'll updat my post and put my code ! please read it !
    – SoheilYou
    Commented Mar 30, 2013 at 10:44
  • Hi. I've suggested an edit to fix the formatting of your code, but the problem is outside the scope of the original question, so I think you should take it out and post it separately.
    – mwfearnley
    Commented Mar 31, 2013 at 2:08
  • This method also allows for semi short curcuiting in VB6: If Person Is Not Nothing Then If Person.Age >= 17 Then Call Person.Drive(Car)
    – Deanna
    Commented Apr 2, 2013 at 11:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.