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
Related
The error message continues with:
"*The expression may not result in the name of a macro, the name of a user-defined function, or [Event Procedure]."
"*There may have been an error evaluating the function, event, or macro."
This database was written by an employee who retired years ago and unfortunately I am now stuck with it. It was working last week and of course when it stopped working today. I am not sure where to even start looking or how to debug in VBA. When a user enter data in the fields and click on Print button, I believe the data gets populated in the data source and then it will print a report/receipt. The print button call the cmdPrintReceipt_Click() procedure. Can someone give me some pointers or how to debug in VBA? Much appreciated and happy holidays!
Pasting the code here:
Option Compare Database
Option Explicit
Dim OkPass As Integer, message As String, Response As Integer
Dim nAmount As Integer
Dim Conn As New ADODB.Connection
Dim RS2 As ADODB.Recordset
Dim Total As Long 'Hold receiptNumber for ReceiptLine
Private Sub cmdNoAdd_Click() 'close window without adding a new reacord
'Use the msgbox function to see if user wants to exit this form w/o adding new receipt
Dim Msg, Style, Title, respons
Msg = "Close this window without saving this receipt?"
Style = vbYesNo + vbQuestion + vbDefaultButton2 'Define buttons of message box
Title = "Exit without adding a receipt?" 'Title of the message box
respons = MsgBox(Msg, Style, Title)
If respons = vbYes Then 'User chose Yes DO NOT ADD the Record
DoCmd.Close
Else 'User chose No close the message box and leave frmReceipt Open
End If
End Sub
Private Sub Form_Load()
'Clear Total
Total = 0
End Sub
'****************************************************************************************
' Main procedure for the form, check first all required fields filled in
' then Add new records and print receipts.
'****************************************************************************************
Private Sub cmdPrintReceipt_Click()
'Check if all filled in, then
'Just call the AddReceipt Function
DoNotClose 'function
If OkPass <> 9 Then
Response = MsgBox(message, vbOKOnly, "Information Missing")
'return focus to the frmReceipt
txtFirstName.SetFocus
Exit Sub
Else
' Add the new Receipt record
Me.lblNoAdd.Visible = False
Me.cmdNoAdd.Visible = False 'you can't escape after printing receipt.
AddAReceipt ' call AddReceipt function
MsgBox "Please confirm the information you entered is correct", vbOKOnly
ShowPrint3Button
'cmdPrint3Button print 3 copies of receipt
End If
Exit Sub
End Sub
If it worked for years without errors before, I doubt debugging will help.
The db might be corrupted. Try the following steps:
see if VBA compiles
compact and repair the db
if not successfull, create a new blank db, and imort all objects from the old one.
Also, when you get the error message, you can press Ctrl+Break and arrive on the exact line that causes the error. Good luck ;-)
I have a problem in VBA(so I'm new).
I have a error with this code
Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "Do you want to continue ?" ' Define message.
Style = vbYesNo + vbCritical + vbDefaultButton2 ' Define buttons.
Title = "MsgBox Demonstration" ' Define title.
Help = "DEMO.HLP" ' Define Help file.
Ctxt = 1000 ' Define topic context.
' Display message.
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbYes Then ' User chose Yes.
MyString = "Yes" ' Perform some action.
Else ' User chose No.
MyString = "No" ' Perform some action.
End If
Error: Invalid outside procedure
Can you help me :))?
Write Sub Test() before the code... And ... End Sub after the code and then run it.
You can refer to Sub statement
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)
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.
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