MsgBox Yes/No Excel VBA - vba

I have a Yes/No MsgBoxin my VBA script that returns a question for the user to answer. Whenever the "Yes" or "No" buttons are pressed, besides the script running its respective code, another MsgBox with the numbers "6" or "7" pops up. How do I disable this second MsgBox?
Here's my code:
Dim question As Integer
question = MsgBox("Unfortunately, the Database has no sources regarding " & Category & " in " & country & ". Would you like to broaden your search and see all sources regarding " & country & "?", vbYesNo + vbQuestion, "Empty Sheet")
MsgBox question
If question = vbYes Then
Sheets("Results").Range("D6").ClearContents
Sheets("Results").Range("D7").ClearContents
Category = Sheets("Results").Range("D6").Value
Else
Sheets("Results").Range("D5").ClearContents
Sheets("Results").Range("D6").ClearContents
Sheets("Results").Range("D7").ClearContents
Exit Sub
End If

The MsgBox function returns a vbMsgBoxResult value, which is an enum (and should be a Long integer, not an Integer).
You're calling it twice:
Dim question As Integer
question = MsgBox("Unfortunately, the Database has no sources regarding " & Category & " in " & country & ". Would you like to broaden your search and see all sources regarding " & country & "?", vbYesNo + vbQuestion, "Empty Sheet")
MsgBox question
Once to assign question, and once to display question - which at that point is going to contain either vbYes (6) or vbNo (7).
I would declare question As vbMsgBoxResult to avoid ambiguities and get autocomplete/IntelliSense when you later use it. Actually, result or answer would be a better identifier - "question" sounds like the question itself, not the user's response.

just use
question = "Unfortunately, the Database has no sources regarding " & Category & " in " & country & ". Would you like to broaden your search and see all sources regarding " & country & "?."
would be enough.
In addition the if function can be
If Msgbox(Question) = vbYes then
...
End If
Don't call MsgBox twice

Remove MsgBox question. This is unnecessarily creating a second message box populated with the value of question (6 or 7 depending on whether you chose yes or no, as eg vbYes has the return value 6).

Related

Inserting text from VBA UserForm textbox into part of a string

I'm new to VBA and hoping someone could help, if this might even be possible.
A date will be manually added by the user into UserForm TEXTBOX1 which will be placed at a bookmark in the document.
.Bookmarks("BOOKMARK1").Range _
.InsertBefore TEXTBOX1
I have option buttons for the user to select, which will place specific text (depending on the button selected) into the document as follows:
Private Sub OptionButton2_Click()
If Me.OptionButton2.Value = True Then
Set oRng = ActiveDocument.Bookmarks("BOOKMARK2").Range
oRng.Text = "EXAMPLE SENTENCE 1" & Chr(11) & Chr(9) & _
"EXAMPLE SENTENCE 2" & Chr(11) & _
"EXAMPLE SENTENCE 3" & vbNewLine & " "
ActiveDocument.Bookmarks.Add "BOOKMARK2", oRng
End If
End Sub
I am trying to get the date that was entered in TEXTBOX1 to appear at the end of the sentence of EXAMPLE SENTENCE 2 before the & CHR(11) &. Can anybody please help with this? Thank you!
I've tried numerous online searches to find the answer for my problem but haven't come across anything so far unfortunately.
"EXAMPLE SENTENCE 2" & TEXTBOX1.Text & Chr(11)

Duplicate message problem in MS Access VBA code

Still new to coding in MS Access. Need some assistance.
What I want to be able to do is capture a person in my table multiple based off the request date however if the request is the same date I want my message for duplicate record to show. Right now my code is looking at first name and last name only and if I enter the same person in on my form with a different date it thinks its a duplicate. Need to fix this. below is my code. I want to add something in this to look at the date as well as the names. The field for the date is Date_of_DMIPasswordReset
Private Sub cmdSave_Click()
If DCount("*", "Tbl_DMIPasswordResets", "FirstName = '" & Me.txtFirstName & "' AND LastName = '" &
Me.txtLastName & "'") > 0 Then
If MsgBox(Me.txtFirstName & " " & Me.txtLastName & " Is already in the DMI Password Reset table
to be approved" & vbNewLine & _
"Do You want to Delete this record", vbQuestion + vbYesNo, "Duplicate Entry") = vbYes Then
Me.Undo
End If
Else
Form_BeforeUpdate (0)
If blnSaveRecord Then
Me.Dirty = False
MsgBox "DMI Password Reset for this Employee Saved", vbInformation
Me.cboEmpLookup.Requery
End If
End If
End Sub

Prevent data duplication with condition

I want to setup a data duplication check on a text box which is used to input serial numbers.
If the entered serial number is already found in the database, it should call a MsgBox to alert the user before clearing the value in the text box.
However, if the entered serial number contains "RW", the check should be disabled.
Private Sub Serial_Number_AfterUpdate()
Dim NewSerialNumber As String
Dim stLinkCriteria As String
NewSerialNumber = Me.Serial_Number.Value
stLinkCriteria = "[Serial_Number] = " & "'" & NewSerialNumber & "'"
If Me.Serial_Number = DLookup("[Serial_Number]", "Esagon_End", stLinkCriteria) Then
MsgBox "This serial number, " & NewSerialNumber & ", has already been entered into the database." _
& vbCr & vbCr & "Please check the serial number again.", vbI, "Duplicate information"
Me.Undo
End If
End Sub
If this cannot be done with VBA I'm open to other methods like queries. Thank you.
Looking at what you have asked, I think this is what you are looking for. If it isn't then leave a comment and I'll try to update my answer.
Private Sub Serial_Number_AfterUpdate()
'If it doesn't contain "RW"
If InStr(Me.Serial_Number, "RW") = 0 Then
'If serial number not in the database
If DCount("*", "Esagon_End", "Serial_Number = '" & Me.Serial_Number & "'") > 0 Then
'Alert user and blank the text box
Call MsgBox("The serial number " & Me.Serial_Number & " is already in the database." _
& vbCrLf & vbCrLf & "Please check the serial number you are entering.", _
vbInformation, "Duplicate Serial")
Me.Serial_Number = ""
End If
End If
End Sub

Message Box referring to cell contents

I cant get the syntax right for a msgbox. I want the box to say:
You have indicated that"
"employee name" (a range reference to a cell the worksheet)
has worked for "hours" (a range reference to a cell the worksheet) doing "job" (a range reference to a cell the worksheet)
Is this information correct?
This is what I have (shortened slightly):
Public confirmation_yes_no()
Dim yesornoanswertomessagebox As String
Dim questiontomessagebox As String
questiontomessagebox = "You have indicated that" & worksheets("dept 1 input").range("g12"),"worked at" & worksheets("dept 1 input").range("g16"), "for" & worksheets("dept 1 input").range("g16"), vbinformation, "Are you sure that this data is correct?"
yesornoanswertomessagebox = MsgBox(questiontomessagebox, vbYesNo, "FlightPlan for Profits PRO")
If yesornoanswertomessagebox = vbNo Then
MsgBox "Return to Data Input to correct error(s)", 0, "FlightPlan for Profits PRO"
Else
MsgBox "Great! Press Enter", 0, "FlightPlan for Profits PRO"
End If
End Sub
I am assuming, of course, that this is possible.
Couple of things with your code,
The opening line of your sub, Public confirmation_yes_no(), what is it, is it a sub, function or what, the way it's written right now it is a global variable declaration.
When combining elements into one like with your string, always use & but be sure to manually put spaces around it, otherwise it is not recognized. &var1 <> & var1
Be cautious when setting the arguments in a variable to be used later and definitely don't set them twice.
If you use a qualifier a lot, like Worksheets("dept 1 input"), consider using a With statement like below, this saves you from having to type the bit on the With statement over and over. Please note that to make use of the with statement, you write . in front of the code..Range(... points to the sheet which is set by the With statement.Range(... points to the sheet which Excel considers to be active.
When combining variables with text, take into account that the variables most likely do not have leading and trailing spaces, and that you'll have to compensate for this in the string bits.
for readability you can add an _ to your code to indicate it continues on the next line instead of having a very, very long line.
You can use a message box directly in an If statement.
Corrected code
Public Sub confirmation_yes_no()
Dim questiontomessagebox As String
With ThisWorkbook.Worksheets("dept 1 input")
questiontomessagebox = "You have indicated that " & .Range("G12") & " worked at " _
& .Range("G16") & " for " & .Range("G16") & "." _
& vbCr & vbCr _
& "Are you sure that this data is correct?"
End With
If MsgBox(questiontomessagebox, vbYesNo, "FlightPlan for Profits PRO") = vbNo Then
MsgBox "Return to Data Input to correct error(s)", 0, "FlightPlan for Profits PRO"
Else
MsgBox "Great! Press Enter", 0, "FlightPlan for Profits PRO"
End If
End Sub
Hi you missed the "&" signs. So i Correct it for you.
questiontomessagebox = ("You have indicated that " & Worksheets("dept 1 input").Range("g12") & " ,worked at " _
& Worksheets("dept 1 input").Range("g16") & " for " & Worksheets("dept 1 input").Range("g16")) & Chr(32) & _
vbInformation & vbNewLine & " Are you sure that this data is correct?"

MSG Box display workbook

I have an easy question but for whatever reason canĀ“t find it online. I want a msgbox to return me specific cells that apply to a set of conditions. For each cell I want it to say what workbook the cell is in.
MsgBox ("Error: " & cl.Address & " is " & cl.Value & " " & thisworkbook.Name)
is what I have in my code. What its returning is the name of the reporting workbook and not where the actual error is located.
Please help
In your situation you should go this way:
MsgBox ("Error: " & cl.Address & " is " & cl.Value & " " & cl.Parent.Parent.Name)
the final part will use hierarchy of Excel Object model- from Cell >> to Sheet >> to Workbook...