PDA

View Full Version : Solved: Multi line Msgbox



austenr
03-07-2010, 12:30 PM
I have written three If statements that warn users they have not filled it text boxes, each having a msgbox that pops up. While this works, is there a way to display the warnings in one message box?

Zack helped me do this a few years ago but I can't find the thread.

lucas
03-07-2010, 01:11 PM
Are you validating on a button click or on txtbox exit?

Are they different msg's or the same?

austenr
03-07-2010, 01:13 PM
' Determine if name has been entered
If txtName.Text = "" Then
MsgBox("You must enter your name.", , "Input Error")
End If
' Determine if Hours entered are between 5 and 60
If txtHoursWorked.Text < 5 Or txtHoursWorked.Text > 60 Then
HoursResponse = MsgBox("Hours worked must be at least 5 and no more than 60.", , "Input Error")
txtHoursWorked.Text = ""
End If
' determine if payrate entered is within 8.00 and 40.00
If txtPayPerHour.Text < 8.0 Or txtPayPerHour.Text > 40.0 Then
HourlyPayResponse = MsgBox("Pay rate must be between 8.00 amd 40.00.", , "Input Error")
txtPayPerHour.Text = ""
End If

mikerickson
03-07-2010, 01:24 PM
Dim MsgPrompt as String

MsgPrompt = vbNullString

If txtName.Text = vbNullString then
MsgPrompt = "You must enter your Name."
End If
If Val(txtHoursWorked.Text) < 5 Or 60 < Val(txtHoursWorked.Text) Then
MsgPrompt = MsgPrompt & vbCr & "Hours worked must be between 5 and 60."
End If
If Val(txtPayPerHour.Text) < 8 Or 40 < Val(txtPayPerHour.Text) Then
MsgPrompt = MsgPrompt & vbCr & "Pay rate must be between 8 and 40."
End If

If MsgPrompt <> vbNullString Then
MsgBox MsgPrompt
End If

lucas
03-07-2010, 01:47 PM
elegant solution Mike. I worked with the code austenr posted and went a different route.

I'll post it for comparison with how efficient Mike's code is.


Private Sub CommandButton1_Click()
Dim Nameresponse As String
Dim HoursResponse As String
Dim HourlyPayResponse As String
If txtName.Text = "" Then
Nameresponse = MsgBox("You must enter your name.", , "Input Error")
txtName.SetFocus
Exit Sub
End If
' Determine if Hours entered are between 5 and 60
If txtHoursWorked.Value = "" Or txtHoursWorked.Value < 5 Or txtHoursWorked.Value > 60 Then
HoursResponse = MsgBox("Hours worked must be at least 5 and no more than 60.", , "Input Error")
txtHoursWorked.Text = ""
txtHoursWorked.SetFocus
Exit Sub
End If
' determine if payrate entered is within 8.00 and 40.00
If txtPayPerHour.Value = "" Or txtPayPerHour.Value < 8# Or txtPayPerHour.Value > 40# Then
HourlyPayResponse = MsgBox("Pay rate must be between 8.00 amd 40.00.", , "Input Error")
txtPayPerHour.Text = ""
txtPayPerHour.SetFocus
Exit Sub
End If
End Sub

austenr
03-07-2010, 01:54 PM
Thanks guys, both solutions are great. Not sure which one I will use yet. Thanks to you both.