Consulting

Results 1 to 6 of 6

Thread: Solved: Multi line Msgbox

  1. #1
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location

    Solved: Multi line Msgbox

    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.
    Peace of mind is found in some of the strangest places.

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Are you validating on a button click or on txtbox exit?

    Are they different msg's or the same?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    [VBA]' Determine if name has been entered
    If txtName.Text = ""Then
    MsgBox("You must enter your name.", , "Input Error")
    EndIf
    ' 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 =
    ""
    EndIf
    ' 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 =
    ""
    EndIf
    [/VBA]
    Peace of mind is found in some of the strangest places.

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    [VBA]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[/VBA]

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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.


    [VBA]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[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Thanks guys, both solutions are great. Not sure which one I will use yet. Thanks to you both.
    Peace of mind is found in some of the strangest places.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •