Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need help with a vbs code!

Status
Not open for further replies.

LeuxD

Programmer
Dec 8, 2018
1
PT
So, i'm learning vbs and i'm playing with code to learn more, but there's an error:

Code:
Option Explicit
Dim x
Dim lol
Dim lol2
Dim lol3
Dim lol4

x=msgbox("oof" ,vbYesNo+vbSystemModal, "only oof")
if x = vbYes Then
   lol=msgbox("more oofs then",vbCritical+vbOKCancel+vbDefaultButton1, "hehe")
ElseIf x = vbNo Then
   lol2=msgBox("oh, ok then...",vbExclamation+vbOKCancel+vbDefaultButton1, "oh...")
ElseIf lol = vbOK Then
   lol3=msgBox("ok",vbExclamation+vbOKCancel, "ok") 
ElseIf lol2 = vbOK Then
   lol4=msgBox("sad",vbExclamation+vbOKCancel, "...") 
End If

when we clicked in lol's 'ok', it was suppose to appear lol3, and if we clicked in lol2's 'ok', it was suppose to appear lol4, but lol3 and lol4 don't appear any more!

please,someone help! i really want to learn vbs, but i'm stuck with this code and i want to know what was my mistake, and no, Option Explicit didn't told me the mistake.
 
Hi LeuxD,
Your mistake is improper use of IF..ElSEIF...
Use nested IFs instead, e.g.:
Code:
x=msgbox("oof" ,vbYesNo+vbSystemModal, "only oof")
if x = vbYes Then
   lol=msgbox("more oofs then",vbCritical+vbOKCancel+vbDefaultButton1, "hehe")
   If lol = vbOK Then
      lol3=msgBox("ok",vbExclamation+vbOKCancel, "ok")
   End If
ElseIf x = vbNo Then
   lol2=msgBox("oh, ok then...",vbExclamation+vbOKCancel+vbDefaultButton1, "oh...")
   If lol2 = vbOK Then
      lol4=msgBox("sad",vbExclamation+vbOKCancel, "...")
   End If
End If
 
I’d prefer Select Case...End Select
Code:
Select Case x
   Case vbYes 
      lol = msgbox("more oofs then",vbCritical+vbOKCancel+vbDefaultButton1, "hehe")
    Case vbNo 
      lol2=msgBox("oh, ok then...",vbExclamation+vbOKCancel+vbDefaultButton1, "oh...")
    Case vbOK 
      lol3=msgBox("ok",vbExclamation+vbOKCancel, "ok") 
    Case Else 
      lol4=msgBox("sad",vbExclamation+vbOKCancel, "...") 
End Select


Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Not sure the Case solution provides the results indicated in the OP, needs some minor modification, e.g.

Code:
[blue]    x = MsgBox("oof", vbYesNo + vbSystemModal, "only oof")
    Select Case x
        Case vbYes
            lol = MsgBox("more oofs then", vbCritical + vbOKCancel + vbDefaultButton1, "hehe")
            [green]'lol1 = lol ' in case we are capturing these for future use[/green]
        Case vbNo
            lol = MsgBox("oh, ok then...", vbExclamation + vbOKCancel + vbDefaultButton1, "oh...")
            [green]'lol2 = lol ' in case we are capturing these for future use[/green]
    End Select
    Select Case lol
        Case vbOK
            lol3 = MsgBox("ok", vbExclamation + vbOKCancel, "ok")
        Case Else
            lol4 = MsgBox("sad", vbExclamation + vbOKCancel, "...")
    End Select[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top