Message Box referring to cell contents - vba

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?"

Related

Microsoft Access Search Box not Turning Up Results

I'm trying to create a search box that acts like the navigation search function at the bottom of the page in access. This is so the users of my data base have an easier time navigating the database.
My code currently is
Private Sub cmdSearch_Click()
Dim bkmk As Variant
Dim strField As String
Me.RecordsetClone.MoveFirst
'Find the first record that matches what
'is in the search text box.
Me.RecordsetClone.FindFirst "B3 Like " _
& Chr(34) & Me.txtsearch & "*" & Chr(34) _
& "OR Item Like" _
& Chr(34) & Me.txtsearch & "*" & Chr(34) _
If Me.RecordsetClone.NoMatch Then
MsgBox "No Match"
Else
bkmk = Me.RecordsetClone.Bookmark
Me.Recordset.Bookmark = bkmk
End If
End Sub
When I apply this code to my search function even when I'm testing with an item that is known to be in the database it is returning the "No Match" message box. My end goal is to have a search function that will search multiple fields in the back end table.

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

MS Access VBA Ranged Dates with BETWEEN or single entry with in Text Boxes

I'm looking for some assistance on this Look up code I managed to put together.
If Me.txtStartDate > "" And Me.txtEndDate > "" Then
varWhere = varWhere & "[CompletionDate] BETWEEN #" & Me.txtStartDate & "# AND #" & Me.txtEndDate & "#"
ElseIf Me.txtStartDate > "" And Me.txtEndDate Is Nothing Then
varWhere = varWhere & "[CompletionDate] = """" & Me.txtStartDate & " * "" And ""
ElseIf Me.txtStartDate Is Nothing And Me.txtEndDate > "" Then
MsgBox "Please Input a Start Date", vbOKOnly, Error
End If
I feel like the code itself is self explanatory of my goals. However I'm wanting to allow the user to input into Me.txtStartDate & Me.txtEndDate giving the range. Also, Allowing the user to just input into Me.txtStartDate for a single date. I have attempted combining the two Along with a message box if they input into end date alone.
Its not working for me, I can get either or working.
My Question is "How do I combine these three statements, so they work in a conditional statement way?"
Anything helps.
I'm not sure what you're trying to do. I like to use datepickers.
Private Sub Form_Open(Cancel As Integer)
Me.DTPickerStart.DefaultValue = "Date()"
Me.DTPickerEnd.DefaultValue = "Date()"
Then I'd do something like this.
If (Nz(Me.Me.txtStartDate.Value) = "") Then
MsgBox "Please select a Start Date.", vbCritical + vbOKOnly, "Error"
ElseIf (Nz(Me.txtEndDate.Value) = "") Then
MsgBox "Please select a End Date.", vbCritical + vbOKOnly, "Error"
Else
DoCmd.OpenReport "XYZRpt", acViewPreview
Or you could do..
DoCmd.OpenQuery "Passthroughquery" that links to an SQL procedure. I'm not
sure what you're trying to do though.
End If

MsgBox Yes/No Excel 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).

Interactive Quiz using ppt

I posted a rubbish question about this before and have gone away and done some work on it to re-ask. Basically I've made a ppt quiz that counts how many correct and incorrect answers a person has given. It then feeds this information back to the user at the end. However what I want to happen now is I want the results to be stored so that I can go back in and see how each user has performed in the quiz. Ideally I would like it to work over 6 networked computers storing all the quiz results in one place. But if need be I can just take a file from each of the 6 computers.
My code so far looks like this:
Dim username As String
Dim numberCorrect As Integer
Dim numberWrong As Integer
Sub YourName()
username = InputBox(prompt:="Type your Name")
MsgBox " Get Ready to begin " + username, vbApplicationModal, " Orange 1C Book 7"
End Sub
Sub correct()
numberCorrect = numberCorrect + 1
ActivePresentation.SlideShowWindow.View.Next
End Sub
Sub incorrect()
numberWrong = numberWrong + 1
ActivePresentation.SlideShowWindow.View.Next
End Sub
Sub Start()
numberCorrect = 0
numberWrong = 0
YourName
ActivePresentation.SlideShowWindow.View.Next
End Sub
Sub Results()
MsgBox "Well done " & username & " You got " & numberCorrect & " out of " & numberCorrect + numberWrong, vbApplicationModal, "Orange 1C Book 7"
End Sub'
Any help would be greatly appreciated. Not sure where to begin with the next step.
Here goes one option for you... But some explanation first. This code will create TXT file. Each time someone will reach Results macro it will add results to the file. So, one file will keep all the results until you don't delete them (or the file). Therefore I've added separation line and date/time information for you to easily find appropriate results.
Sub Save_Results_To_Txt()
'set file results location to activepresentation path
'or could be changed to any path string
Dim strWhere As String
strWhere = ActivePresentation.Path
'let's set name of the file separately
Dim strName As String
strName = "\results.txt"
Dim ff As Long
ff = FreeFile
Open strWhere & strName For Append As #ff
Write #ff, Now & vbTab & username
Write #ff, numberCorrect & vbTab & vbTab & numberWrong
Write #ff, String(30, "-")
Close #ff
End Sub
You need to add Save_Results_To_Txt to your Results sub, possibly before MsgBox line.
Your results.txt file will look like:
"2013-04-25 16:11:05 Tom"
"10 11"
"------------------------------"
"2013-04-25 16:11:23 Mark"
"11 10"
"------------------------------"