How do I check if a button is pressed in a VBScript 'MsgBox'? [duplicate] - vbscript

This question already has answers here:
How do you display a message using VBScript (VBS), without causing an error?
(2 answers)
Closed 5 months ago.
How do I check if I click on a button in a MsgBox?
x = msgbox("Test", 0+16, "Test")
I meant check, not how to make a message box!

Use:
x = msgbox("Test", 0+16, "Test")
if x = number then
msgbox("Hello, World!")
end if
instead of the "number" in the if statement, write the number corresponding with the button that you want to check if pressed. Here are the numbers that you can write:
1 - ok; 2 - cancel; 3 - abort; 4 - retry; 5 - ignore; 6 - yes; 7 - no

You should learn VBScript programming from the ground up.
VBScript Basics, Part 1 | Message Box - Numbers (MsgBox)
VBScript Basics, Part 2 | Message Box - Constants (MsgBox)
VBScript Basics, Part 3 | If - ElseIf - Else - Then Statements
All Vbscript Tutorial Playlist
Here is an example:
Dim AnswerQuestion, Msg, Title
Title = "Answer the question?"
Msg = "Do you like cookies??" & Vbcr &_
"If yes, then click the [YES] button " & Vbcr &_
"If not, then click the [NO] button"
AnswerQuestion = MsgBox(Msg, VbYesNo + VbQuestion, Title)
If AnswerQuestion = VbYes Then
MsgBox "You clicked on the OK button. Good, you're off the hook!", vbInformation, Title
Else
MsgBox "You clicked on the No button! and you're going to jail!", VbCritical, Title
End if

Related

How to Keep Script running for Auto Print and then Delete [duplicate]

I have written a .vbs script which presently is run manually by users. How can I make this script schedule itself in Task Scheduler (to run at a fixed time each day automatically) on Windows XP and Windows 7 the first time it is executed manually?
EDIT
Option Explicit
Dim oShell : Set oShell = WScript.CreateObject ("WScript.Shell")
Dim FSO : set FSO = CreateObject("Scripting.FileSystemObject")
Dim StartTime,Elapsed
'msgBox(oShell.CurrentDirectory)
'MsgBox(FSO.GetFile(Wscript.ScriptFullName).ParentFolder )
oShell.CurrentDirectory = FSO.GetFile(Wscript.ScriptFullName).ParentFolder
StartTime = Timer
oShell.run "ParentChildLinkFinal.vbs", 1, True
oShell.run "Parent_Child_Merge_final.vbs", 1, True
oShell.run "CycleTime.vbs", 1, True
oShell.run "Baddata.vbs", 1, True
oShell.run "Matrixrefresh.vbs", 1, True
Elapsed = Timer - StartTime
MsgBox("Total time taken to finish this task:" & Elapsed & "in Seconds")
Thanks,
Create a scheduled task to run the following command:
c:\windows\system32\cscript.exe PATH_TO_YOUR_VBS
You can certainly make a vbs file a scheduled task.
The caveat, though, is you want NOTHING to prompt the user...no inputs, no message boxes, nothing. Make sure you've handled and logged your exceptions or you could find yourself with a task that never completes (or worse, operations angrily calling you because a message box popped up at 3am and halted a production process)
on Windows 8: windows tasks that are triggering VBScripts having message boxes will only display the message boxes if the windows task is run under the same user who really is logged on the machine and the if the windows task is configured to "run only if the user is logged on"
I think this changed on windows 8, as on windows 7 I did not had to configure the task in that way
try this
X=MsgBox("Take the Quiz!")
name=inputbox("Name")
q1=inputbox("Question 1 - How many days are there in a leap Year?")
q2=inputbox("Question 2 - How many Suns does Neptune have?")
q3=inputbox("Question 3 - What is your name?")
q4=inputbox("Question 4 - What did one computer say to the other?")
q5=inputbox("Question 5 - Why did the chicken cross the road?")
msgbox("Answers!")
msgbox("Q1 - How many days are there in a leap Year?, you answered ") + q1 + (" ,the correct answer is 366")
msgbox("Q2 - How many Suns does Neptune have?, you answered ") + q2 + (" ,the correct answere is one, our sun.")
msgbox("Q3 - What is your name?, you answered ") + q3 + (", the correct answer is ") + name + (" or is it?")
msgbox("Q4 - What did one computer say to the other?, you answered ") + q4 + (", the correct answer is, 011100110111010101110000 (Binary code for sup)")
msgbox("Q5 - Why did the chicken cross the road?, you answered ") + q5 + (", the correct answer is To get to the other side")
msgbox("Well done, ") + name + (" you have completed the quiz")

can .vbs file be a scheduled script?

I have written a .vbs script which presently is run manually by users. How can I make this script schedule itself in Task Scheduler (to run at a fixed time each day automatically) on Windows XP and Windows 7 the first time it is executed manually?
EDIT
Option Explicit
Dim oShell : Set oShell = WScript.CreateObject ("WScript.Shell")
Dim FSO : set FSO = CreateObject("Scripting.FileSystemObject")
Dim StartTime,Elapsed
'msgBox(oShell.CurrentDirectory)
'MsgBox(FSO.GetFile(Wscript.ScriptFullName).ParentFolder )
oShell.CurrentDirectory = FSO.GetFile(Wscript.ScriptFullName).ParentFolder
StartTime = Timer
oShell.run "ParentChildLinkFinal.vbs", 1, True
oShell.run "Parent_Child_Merge_final.vbs", 1, True
oShell.run "CycleTime.vbs", 1, True
oShell.run "Baddata.vbs", 1, True
oShell.run "Matrixrefresh.vbs", 1, True
Elapsed = Timer - StartTime
MsgBox("Total time taken to finish this task:" & Elapsed & "in Seconds")
Thanks,
Create a scheduled task to run the following command:
c:\windows\system32\cscript.exe PATH_TO_YOUR_VBS
You can certainly make a vbs file a scheduled task.
The caveat, though, is you want NOTHING to prompt the user...no inputs, no message boxes, nothing. Make sure you've handled and logged your exceptions or you could find yourself with a task that never completes (or worse, operations angrily calling you because a message box popped up at 3am and halted a production process)
on Windows 8: windows tasks that are triggering VBScripts having message boxes will only display the message boxes if the windows task is run under the same user who really is logged on the machine and the if the windows task is configured to "run only if the user is logged on"
I think this changed on windows 8, as on windows 7 I did not had to configure the task in that way
try this
X=MsgBox("Take the Quiz!")
name=inputbox("Name")
q1=inputbox("Question 1 - How many days are there in a leap Year?")
q2=inputbox("Question 2 - How many Suns does Neptune have?")
q3=inputbox("Question 3 - What is your name?")
q4=inputbox("Question 4 - What did one computer say to the other?")
q5=inputbox("Question 5 - Why did the chicken cross the road?")
msgbox("Answers!")
msgbox("Q1 - How many days are there in a leap Year?, you answered ") + q1 + (" ,the correct answer is 366")
msgbox("Q2 - How many Suns does Neptune have?, you answered ") + q2 + (" ,the correct answere is one, our sun.")
msgbox("Q3 - What is your name?, you answered ") + q3 + (", the correct answer is ") + name + (" or is it?")
msgbox("Q4 - What did one computer say to the other?, you answered ") + q4 + (", the correct answer is, 011100110111010101110000 (Binary code for sup)")
msgbox("Q5 - Why did the chicken cross the road?, you answered ") + q5 + (", the correct answer is To get to the other side")
msgbox("Well done, ") + name + (" you have completed the quiz")

get value of Checkbox in datagrid

I am working with windows application.
I have a datagrid in vb.net. Its first column is a checkbox. I want to know which checkboxes are checked and which are not.
My code is :
Dim dr As DataGridViewRow
For i = 0 To gdStudInfo.RowCount - 1
dr = gdStudInfo.Rows(i)
att = dr.Cells(0).Value.ToString()
If att.Equals("Present") Then
qry = "insert into Stu_Att_Detail values(" & id & "," & gdStudInfo.Rows(i).Cells(1).Value.ToString() & ",'" & dr.Cells(0).Value.ToString() & "')"
con.MyQuery(qry)
End If
Next
I am getting correct values for all checked check box, but it gets error when the checkbox is not checked.
What if you try this?
If Not String.IsNullOrEmpty(dr.Cells(0).Value) Then
'do stuff here
End If

QTP 10 - A function return deifferent results for same data in run and debug modes

I would extremely appreciate if anyone can suggest a solution for this.
I have a simple function that is is expecting for a browser to be opened on a page containing a web list that each value of it represents an account. When an account is selected it's products (if any) are displayed.
The functions goal is to retrieve an index of an account with products (the first to be found) or -1 if there are none.
The problem, which I can't figure out what is causing it, is that the function will return the correct result when I'm debugging it - meaning running the code step by step using F10, but will return a wrong result if I'll run regularly (F5). This behavior is consistent and the function retrieves the same result each time for each type of runs, meaning it's not a bug that just makes the function return a random answer.
This is the function:
' #return: a random account index with products if one exists
' otherwise returns -1
Public Function getRandomAccountWithProducts()
On Error Resume Next
Set Page1 = Browser("micclass:=browser").Page("micclass:=Page")
Set br = Browser("micclass:=Browser")
originalURL = br.GetROProperty("URL")
br.Navigate Environment.Value("SOME URL") & "REST OF URL"
br.Sync
Page1.WebList("name:=accountId").Select "#1"
br.Sync
' Display only products
Page1.WebRadioGroup("name:=name0").Click
Page1.WebList("name:=name1").Select "Display None"
Page1.WebList("name:=name2").Select "Display None"
Page1.WebButton("value:=Apply","visible:=True").Click
' Init
numOfAccounts = Page1.WebList("name:=accountId").GetROProperty("items count") - 1
If numOfAccounts < 1 Then
getRandomAccountWithProducts = -1
Reporter.ReportEvent micFail, "Number of accounts","There are no accounts. No account with products exists"
Exit Function
End If
hasProducts = false
accountIndex = 1
' Get account with products
While ((Not hasProducts) AND (accountIndex =< numOfAccounts))
' Return account if has products
If Page1.WebList("name:=webListName","index:=0","micclass:=WebList","visible:=True").Exist(5) Then
hasProducts = true
End If
If (Not hasProducts) Then
accountIndex = accountIndex + 1
Page1.WebList("name:=accountId").Select "#" & accountIndex
End If
Wend
br.Navigate originalURL
Set Page1= Nothing
Set br = Nothing
' If no account has products, report and exit, else return selected account index
If Not hasProducts Then
Reporter.ReportEvent micFail,"Accounts","No account has products."
getRandomAccountWithProducts = -1
Else
getRandomAccountWithProducts = accountIndex
End If
If Err<>0 Then
errorMessage = "Error number: " & Err.Number & vbNewLine & "Error description: " & Err.Description & vbNewLine & "Error source: " & Err.Source
Reporter.ReportEvent micFail,"Run Time Error",errorMessage
Err.Clear
End If
On Error GoTo 0
End Function
I'm running on Pentium 4, 3.2 GHZ, 2 GB RAM, Win XP, SP 3,IE 7, QTP 10.0 Build 513
Thanks!
Have you considered using the all items property?
AllItems = Page1.WebList("name:=accountId").GetROProperty("all items")
SplitItems = Split(AllItems, ";")
Found = False
For i = 0 To UBound(AllItems)
If AllItems(i) = "<product>" Then
Found = True
Exit For
End If
Next
Solution was found thanks to Jonty,
The problem was in the following section:
' Get account with products
While ((Not hasProducts) AND (accountIndex =< numOfAccounts))
' Return account if has products
If Page1.WebList("name:=webListName","index:=0","micclass:=WebList","visible:=True").Exist(5) Then
hasProducts = true
End If
If (Not hasProducts) Then
accountIndex = accountIndex + 1
Page1.WebList("name:=accountId").Select "#" & accountIndex
End If
Wend
The first time entered to the loop, the account really didn't have any products, so obviously none was recognized. So accountIndex was increased by one and the corresponding account was selected in the web list.
No here lies the problem. The select method caused a refresh in the page and the condition Page1.WebList("name:=webListName","index:=0","micclass:=WebList","visible:=True").Exist(5)
was evaluated before the web list was loaded thus, returning false.
I considered that option, but I thought (wrongly apparently) that the Exist(5) should do the trick, but it seems that it works differently than expected.
Thanks,
Alon

Using multiple Message Boxes in "if then" statement with VB

I am very new with programming and I have come across an issue in Visual Basic that I cannot figure out. Many forums and YouTube videos later I still do not have an answer.
I am using a nested selection structure and within it is two Message boxes. I cannot figure out how to get the second dialog result to trigger the elseif statement. It just skips over it. I believe since I have one variable declared for a dialog result it is checking both of them, but in this case I don't know how to declare only the second dialog result.
Here is the code so far.
Dim dblTotal As Double = 12
Dim strResponse As DialogResult
' Dialog box asking about a coupon and $2 coupon.
If MessageBox.Show("Does customer have a coupon?", "Coupon", MessageBoxButtons.YesNo) = vbYes AndAlso
MessageBox.Show("Does customer have a $2 coupon?", "Coupon", MessageBoxButtons.YesNo) = vbNo Then
lblTotal.Text = Convert.ToString(dblTotal - 4)
' Meant to be ran if statement is false. I dont Understand
' why it is skipping over and not executing.
' Is "dlgResult" reading the first one as well? How do I correct?
ElseIf strResponse = vbYes Then
lblTotal.Text = Convert.ToString(dblTotal - 2)
Else
lblTotal.Text = Convert.ToString(dblTotal)
End If
End Sub
I understand it would be easier to to code if the first message = vbNo, but I was trying to see if this way would work.
Thank you!!
Is this how you wanted it?
Dim dialog1 As DialogResult
Dim dialog2 As DialogResult
Dim dblTotal As Double = 12
dialog1 = MessageBox.Show("Does customer have a coupon?", "Coupon", MessageBoxButtons.YesNo)
dialog2 = MessageBox.Show("Does customer have a $2 coupon?", "Coupon", MessageBoxButtons.YesNo)
If dialog1 = DialogResult.OK Then
dblTotal = dblTotal - 2
End If
If dialog2 = DialogResult.OK Then
dblTotal = dblTotal - 2
End If
lblTotal.Text = Convert.ToString(dblTotal - 2)

Resources