How do I take the result from a message box? - vb.net

In Visual Basic in Visual Studio 2010, if this is used for giving a message,
MsgBox("hello", 4, "status")
how do I manipulate the result Yes or No from the msgbox?
Like this should happen if the user gives No and this should happen if No.

You need to check whether MsgBox returned vbYes.
For example:
If vbYes = MsgBox("hello", vbYesNo, "status") Then
'Do things
Else
'Don't do things
End If

MsgBox Function (Visual Basic)
Lesson 10: Introduction to VB Built-in Functions
Sample code:
Private Sub Test_Click()
Dim testMsg As Integer
testMsg = MsgBox("Click to test", 1, "Test message")
If testMsg = 1 Then 'User clicked on OK button
Display.Caption = "Test Succeeded"
Else 'User clicked on Cancel button
Display.Caption = "Test failed"
End If
End Sub

Pretty sure you can just do this also:
Dim response As MsgBoxResult = MsgBox("Are You Sure you want to delete this entry?", MsgBoxStyle.DefaultButton2 Or MsgBoxStyle.YesNo Or MsgBoxStyle.Critical, "Warning")
If response = MsgBoxResult.Yes Then

Related

VBA Access Message Box woes

I followed a few simple comments on how to pop a confirmation box before executing a script, but sadly, if I press yes, the script doesn't run.
Private Sub Overwrite_Btn_Click()
If MsgBox("Yes?", vbOKCancel) = ok Then
Me.Product_Quantity = Me.Quantity_Input
Else
Exit Sub
End If
End Sub
I'm trying to set Product_Quantity equaling Quantity_Input, and although it works without the MsgBox command, it doesn't with it.
What am I doing wrong?
Instead of If MsgBox("Yes?", vbOKCancel) = ok Then try: If MsgBox("Yes?", vbOKCancel) = vbOK Then
Typically the interactions with forms will return one constant from a set of several constants. Those are catalogged in enums. In this case you have several constants in the VbMsgBoxResult class, and vbOK is a constant with value 1, which is returned from clicking the ok button.
Actually, If MsgBox("Yes?", vbOKCancel) = 1 Then would work as well, but it is harder to remember that clicking Ok returns 1 then simply stating a constant named vbOK
In object explorer (F2 on the VBE), searching for VbMsgBoxResult will give all possible results that comes from interacting with a message box.
https://www.techonthenet.com/access/constants/msgbox_ret.php
1) Dim a variable as integer.
2) Check for value of integer equal to 6, or check for vbYess
3) ?????
4) Profit
borrowed from link
Dim LResponse As Integer
LResponse = MsgBox("Do you wish to continue?", vbYesNo, "Continue")
If LResponse = vbYes Then
{...statements...}
Else
{...statements...}
End If
Single line:
If MsgBox("Yes?", vbOKCancel) <> vbOk then Exit Sub
'continue code here.
More Information:
MSDN : MsgBox Function (Office/VBA)

Command Button in msgbox vba possible?

si it possible to add a command button in the msgbox window in vba?
For example, i want to add a cancel button that stops the code rather than continuing it. I could create a new userform, but it would be nice if i save some space and use the msgbox that is already here.
VBA has several different types of MessageBoxes with built in command buttons for this very purpose. The type of buttons included in the message box is declared as the second parameter - MsgBox(Prompt, Buttons as)
The types you are probably interested in are:
vbYesNo
vbYesNoCancel
vbAbortRetryIgnore
vbOkCancel
vbRetryCancel
These Buttons return integer values that need to either be stored or used for comparison.
VBA has these integer answers stored as constants (e.g. vbOK = 1, VbCancel = 2, etc.) See Microsoft Developer Network MsgBox Function for more details on that.
Sub mySub()
Dim answer as Integer
answer = MsgBox("Would you like to choose yes?", vbYesNoCancel)
If answer = vbYes Then
'Do whatever you want
ElseIf answer = vbNo Then
'Do whatever
Else
Exit Sub
End If
End Sub
Try this:
If MsgBox("Cancel or Continue, are you sure?", vbOKCancel) = vbOK Then
'continue whatever you want to do.
End if

Excel Macro YesNo message box, different directions for Yes and No

The user has two options in the YesNo Message Box. If No, it performs a certain sequence of filters, but I want to filter another column if the user answers Yes to the message box question. Currently, at "Else", I get an error that reads "Compile error: Function call on left-hand side of assignment must return Variant or Object" If I take out "Else", and the code after it, the macro runs smoothly, but only filters when the user selects No.
If MsgBox("Is This Item Catch Weight?", vbYesNo) = vbNo Then
retval = InputBox("Please Enter PO Cost")
ActiveSheet.Range("$A$1:$CL$293662").AutoFilter Field:=71, Criteria1:="=" & retval
retval = InputBox("Please Enter Net Weight")
ActiveSheet.Range("$A$1:$CL$293662").AutoFilter Field:=41, Criteria1:="=" & retval
Else: MsgBox("Is This Item Catch Weight?", vbYesNo) = vbYes
retval = InputBox("Please Enter PO Cost")
ActiveSheet.Range("$A$1:$CL$293662").AutoFilter Field:=71, Criteria1:="=" & retval
End If
End If
A few things are happening here. : new line character in VBA code so a line like
Else: MsgBox("Is This Item Catch Weight?", vbYesNo) = vbYes
is actually the same as
Else
MsgBox("Is This Item Catch Weight?", vbYesNo) = vbYes
Which is not what you want.
There is also an extra End If at the end. Remove that.
Calling the message box to appear multiple times is probably not what oyu want either. Likely you want to show the message box, get the result then do something with it.
Dim response As VbMsgBoxResult
response = MsgBox("Is This Item Catch Weight?", vbYesNo)
If response = vbNo Then
'do stuff for yes
ElseIf response = vbYes Then
' do stuff for No
End If
I'd also suggest not using ActiveSheet unless you are sure that's what you want.

Visual Basic: Message Box Prompt Twice When I Click On Button

I have problem with my message box. This is part of app which check if serial number has already been used. If yes, it shows error message,when click YES program continues and make some functions, NO button end sub. It works well, but it shows me message box twice and I really don't understand why, please help
If File.Exists(pathSN) Then
Dim Findstring = IO.File.ReadAllText(pathSN)
If Findstring.Contains(Lookfor) Then
Dim msg = "Serial number has already been used"
Dim title = "Error"
Dim style = MsgBoxStyle.YesNo Or MsgBoxStyle.DefaultButton2 Or _
MsgBoxStyle.Critical
Dim response = MsgBox(msg, style, title)
MsgBox(msg, style, title)
If response = MsgBoxResult.Yes Then
wrlayout()
openlayout()
Exit Sub
Else
Exit Sub
End If
End If
End If
...
Dim response = MsgBox(msg, style, title)
MsgBox(msg, style, title)
...
you do not need the second call to MsgBox

Nested If / Else

So I have a sub on a button click that does the following:
If the text entry within a combo box (cmbServerInstall.Text) is blank, firstly it will force the user to make a selection before proceeding.
Else, a string (strGameServer) is populated with the text within the combo box (cmbServerInstall.Text).
From here, a MessageBox will then show with a Yes/No option, asking if the user wishes to proceed.
Here is where things are going wrong.
What I want to happen
If the user selects yes, then I want to use another if/else to determine what was stored in the string strGameServer. Depending on what this is set to, it will launch one of two batch files (I understand the file paths are the same at the moment, I plan to update this at a later date).
If the user selects no, I want it to remove the selection from the combobox cmbServerInstall.
What is happening as it stands
Basically the shell command launches the batch file REGARDLESS of whether or not MsgBoxResult is Yes or No.
Could anyone kindly take a look at the code below and point me in the direction of where I am going wrong? Nested IFs seem to be getting the better of me.
Dim strGameServer As String
If cmbServerInstall.Text = "" Then
MessageBox.Show("Please select a game server to install", "No game server selected", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
Else
strGameServer = cmbServerInstall.Text
MessageBox.Show("You have chosen" + " " + strGameServer + "." + " " + "Please confirm you wish to proceed with your selection.", "Confirm game server selection", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
If MsgBoxResult.Yes Then
If strGameServer = "Counter-Strike: Global Offensive" Then
Shell("C:\Users\Damon\Desktop\YorkshaLAN Server Creator\YorkshaLAN Server Setup.bat", AppWinStyle.NormalFocus)
Else : strGameServer = "Team Fortress 2"
Shell("C:\Users\Damon\Desktop\YorkshaLAN Server Creator\YorkshaLAN Server Setup.bat", AppWinStyle.NormalFocus)
End If
Else
cmbServerInstall.Text = ""
End If
cmbServerInstall.Text = ""
cmbServerInstall.Enabled = False
btnServerGoInstall.Enabled = False
End If
End Sub
You need to save the result from MessageBox.Show and then check it, or do so in one line.
Edit of original code:
If cmbServerInstall.Text = "" Then
MessageBox.Show("Please select a game server to install", "No game server selected", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
Else
Dim strGameServer As String = cmbServerInstall.Text ' Moved init to avoid declaration without use '
If MessageBox.Show("You have chosen" & " " & strGameServer & "." & " " & "Please confirm you wish to proceed with your selection.",
"Confirm game server selection",
MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) =MsgBoxResult.Yes Then
If strGameServer = "Counter-Strike: Global Offensive" Then
Shell("C:\Users\Damon\Desktop\YorkshaLAN Server Creator\YorkshaLAN Server Setup.bat", AppWinStyle.NormalFocus)
Else
strGameServer = "Team Fortress 2"
Shell("C:\Users\Damon\Desktop\YorkshaLAN Server Creator\YorkshaLAN Server Setup.bat", AppWinStyle.NormalFocus)
End If
Else
cmbServerInstall.Text = ""
End If
cmbServerInstall.Text = ""
cmbServerInstall.Enabled = False
btnServerGoInstall.Enabled = False
End If
End Sub
You need to get the result of the MessageBox with the question and check the result
Dim result = MessageBox.Show("You have chosen ......")
If result = MsgBoxResult.Yes Then
.....
Actually your code checks the enum MsgBoxResult.Yes and because it is not zero the if is always evaulated as true
Also, if I were you I would try to remove any usage of the old VB6 syntax and enumerations. Actually MessageBox.Show returns a DialogResult enumeration not a MsgBoxResult. This is here just for VB6 compatibility
Dim result = MessageBox.Show("You have chosen ......")
If result = DialogResult.Yes Then