VBScript set focus on "No" button on UI window - vbscript

I'm writing a VBScript script for use with VanDyke's CRT. Is there a way to set the focus on the "No" button on a MsgBox automatically, instead of the default "Yes"?
val = MsgBox("Do you want to remove specials?",VBYesNo, "Purchasing Automation")
'6 == yes, 7 == no
If (val = 6) Then
'we received a yes
MsgBox("got a yes")
Else
'no is inferred
MsgBox("got a no")
End If

Replace vbYesNo with vbYesNo OR vbDefaultButton2.

Related

vbscript X and Cancel not doing what they should

im writing a code in vbscript where it will ask the user for input and then run certain files according to the input and i have the else so that it will redo the if else sequence when you type something that isnt an option but when i try to press cancel or the red 'X' it acts as if i have put in an invalid input and goes over the else sequence.
Dim sInput
sInput = InputBox("input")
If sInput = "input1" or sInput = "input2" Then
set shell=createobject("wscript.shell")
shell.run "file.bat"
elseif sInput = "exit" or sInput = "Exit" Then
WScript.Quit
else
name=msgbox (" That is not a valid response",0+16,"ERROR")
set shell=createobject("wscript.shell")
shell.run "input.vbs"
end if
Don't try to restart the script.
Use a loop instead. End the loop when the user entered a valid option, or quit the entire program if requested.
Option Explicit
Dim Shell, input, button
Set Shell = CreateObject("WScript.Shell")
Do
input = InputBox("input")
If IsEmpty(input) Or LCase(input) = "exit" Then WScript.Quit
input = LCase(Trim(input))
If input = "input1" Or input = "input2" Then
Shell.Run "file.bat"
Exit Do
Else
button = MsgBox("That is not a valid response.", vbExclamation + vbRetryCancel, "ERROR")
If button = vbCancel Then Exit Do
End If
Loop
Notes:
Option Explicit makes variable declaration mandatory. It's a good idea to always have this enabled.
IsEmpty() is true when the user pressed the Cancel button (or the Esc key) in the InputBox - but this will work only before the response is manipulated in any way, such as LCase or Trim. Supporting the Cancel button is more intuitive than having a special "exit" keyword, so maybe you should get rid of that.
The various constants you can use with MsgBox are described on ss64.com and in more detal in the official VBScript language reference.
You can change what Enter and Esc do in each MsgBox by using the vbDefaultButton1 or vbDefaultButton2 constants.
The Do loop without any conditions (Do/Loop While ... or Do/Loop Until ...) will run forever - be sure not to forget using Exit Do or WScript.Quit(). (If you do, killing the Script with the Task Manager will get you out of it.)

If Then Else won't initiate vbNo

This code is supposed to open a script to simulate a timer when "Yes" is pressed or open a script to simulate an alarm when "No" is pressed. When "Yes" is pressed it works as intended, but when "No" is pressed, it still opens the timer script and not the alarm one.
Pick = MsgBox("Do you want a Timer?", 4+32, "Choose one")
If vbYes Then
CreateObject("WScript.Shell").Run("""C:\Users\Username\Documents\Applications\Reminder\Reminder Script.vbs""")
WScript.Quit
Else
CreateObject("WScript.Shell").Run("""C:\Users\Username\Documents\Applications\Alarm\Alarm Script.vbs""")
WScript.Quit
End If
AFAIK you can do this two ways (See below - please excuse the syntax). The If...Then...Else method is answered by GSerg and pinkfloydx33 in the comments to your question...
set wshell = createobject("wscript.shell")
'**If...Then...Else**
pick = msgbox("Do you want a timer?", 4+32, "Choose one")
if pick = vbyes then
'wshell.run("""c:\users\username\documents\applications\reminder\reminder script.vbs""")
wscript.echo "Reminder"
else
'whsell.run("""c:\users\username\documents\applications\alarm\alarm script.vbs""")
wscript.echo "Alarm"
end if
'**Select...Case**
pick = msgbox("Do you want a timer?", 4+32, "Choose one")
select case pick
case vbyes
'wshell.run("""c:\users\username\documents\applications\reminder\reminder script.vbs""")
wscript.echo "Reminder"
wscript.quit
case vbno
'whsell.run("""c:\users\username\documents\applications\alarm\alarm script.vbs""")
wscript.echo "Alarm"
wscript.quit
end select
Hopefully this helps.
Furthermore the MSDN VBScript User guide may help better explain variables etc.

VBS - Not Working Script

I have this VBScript that doesn't work. When I press "Yes" on the pop-up it says error. Please help. Thanks!
Code:
result = MsgBox ("Yes or No?", vbYesNo, "Start Web")
Select Case result
Case vbYes
shell.CurrentDirectory = "My Directory"
shell.Run "startweb.bat"
Case vbNo
End Select
When I Press "Yes" A .Bat file called: startweb
When I press "Yes" on the pop-up it says error.
What error?
Microsoft VBScript runtime error: Object required, or
The system cannot find the file specified, or
something else?
Solution:
Case ˙Object required`:
Set Shell = WScript.CreateObject("WScript.Shell") ''' add this line
result = MsgBox ("Yes or No?", vbYesNo, "Start Web")
Select Case result
Case vbYes
shell.CurrentDirectory = "My Directory"
shell.Run "startweb.bat"
Case vbNo
End Select
Case file missing: use fully qualified path to "My Directory", e.g. "c:\tests\My Directory"
Case Else: unsolvable; edit your question and add more info.

GoTo "Expected Statement"

I am attempting to use the GoTo command (I do not know any alternatives and it works fine in batch). Whenever I attempt to load the program, I get this error:
Here is basically where the error is (line 11 column 3)
top:
input = InputBox("Enter normal text:", "Message Encrypt Style 2", "Text goes here")
If input = "" Then
Y = MsgBox("You inputed nothing!", vbRetryCancel+64, "Huh?")
If Y = 2 Then
WScript.Quit
Else
If Y = 4 Then
GoTo top
Else
If input = 2 Then
WScript.Quit
VBScript doesn't have a Goto statement, and there's a cleaner approach anyway.
Do
input = InputBox(...)
If IsEmpty(input) Or input = "2" Then
WScript.Quit
ElseIf input = "" Then
MsgBox "No input."
End If
Loop Until input <> ""
try this
Option Explicit
Dim Input ' as string
Dim Y ' as msgbox response
Input = ""
Do Until Input <> ""
Input= InputBox("Enter normal text:", "Message Encrypt Style 2", "Text goes here")
If Input = "" Then
Y = Msgbox ("You input nothing", vbRetryCancel, "Huh?")
If Y = vbCancel Then
WScript.Quit
End If
ElseIf Input = "2" Then
WScript.Quit
End If
Loop
' Proceed here if input is valid
Vbscript is a structured programming language and one of the main goals of structured programming is to eliminate the goto statement as it's considered harmful. Vbscript does have a goto for exceptions, but these are only meant for resource cleanup prior to a program exit.

InputBox Cancel

I have created an Inputbox to get the username entered but stuck with the cancel button
Private Sub Form_Load()
fsUserName = UCase(InputBox("Please Enter your name.", "User Name", _
"dc"))
If fsUserName = "" Then
MsgBox "No name Entered." & Chr(13) & Chr(13) & _
"You must enter a name.", vbExclamation, "ERROR"
Form_Load
ElseIf VarType(fsUserName) = 0 Then 'If cancel clicked
cmdQuit_Click
End If
Also is there a way that when the X button on the form is clicked it executes cmdQuit_Click so that if the userclicks the command button Quit or X ,the Quit script is run.In the quit script there are message boxes and cleanup.
You can use StrPtr() to detect if cancel was clicked;
Dim fsUserName As String
fsUserName = InputBox("Please Enter your name.", "User Name", "dc")
If (StrPtr(fsUserName) = 0&) Then
MsgBox "Cancelled or X clicked"
ElseIf fsUserName = "" Then
MsgBox "No name Entered." & vbCr & "You must enter a name.", vbExclamation, "ERROR"
Else
fsUserName = UCase$(fsUserName)
MsgBox fsUserName
End If
If you want to do something when the form unloads you can use the Form_Unload event or better the Form_QueryUnload event which fires before the actual unload & allows you to cancel it.
It will also tell you why the form is unloading (UnloadMode will be 0 if the red X is clicked)
Using "Unload Me" will raise both of the events.
Edit: Calling Form_Load in Form_Load like that will eventually fill up the stack, better to use a loop to look for a missing username.
Alex's answer using StrPtr, which I assume works as advertised, is good as far as it goes, but the better advice (IMO) is to avoid InputBox altogether. You can easily create your own using a dialog-style form, a textbox, and a couple of buttons, and maybe an icon if you like.
Rolling your own gives you complete flexibility and predictability, and once you have it you can use it in any future projects.
Excel InputBox and Application.InputBox are different functions.
Sub GetValue2()
Dim Monthly As Variant
Monthly = Application.InputBox _
(Prompt:="Podaj wysokość miesięcznej wypłaty:", _
Type:=1)
If Monthly = False Then Exit Sub
MsgBox "Zarobki rocznie: " & Monthly * 12
End Sub
Try this...
Name = Application.InputBox("Please enter Name")
If Name = "False" Then
MsgBox " Your message here"
Exit Sub
End If

Resources