MsgBox placing

limhockmeng

New Member
Joined
May 3, 2012
Messages
2
Hi folks,

I have the following coding that display a message should one of the mandatory fields be empty (validation). Now, my problem is that the placing of the variables in the MsgBox is hardocded, i.e. if all fields are filled except Postal code, Postal code will appear in the last line instead of first line of the msgbox. How can i make the code more versatile as in it will auto shift the position of the variable up.

Please advise.

Many thanks

Current placing order:
LastName
FirstName
EmployeeNumber
Address 1
Country
Postal Code


'Coding
---------------
Private Sub Page2Next_Click()
Dim Page2MsgLastName As String
Dim Page2MsgFirstName As String
Dim Page2MsgEmployeeNumber As String
Dim Page2MsgAddressLineBox1 As String
Dim Page2MsgCountryBox As String
Dim Page2MsgPostalCodeBox As String
If LastNameBox.Text = "" Then
Page2MsgLastName = "Last Name"
End If
If FirstNameBox.Text = "" Then
Page2MsgFirstName = "First Name"
End If
If EmployeeNumberBox.Text = "" Then
Page2MsgEmployeeNumber = "Employee Number"
End If
If AddressButtonYes = True Then
If AddressLineBox1.Text = "" Then
Page2MsgAddressLineBox1 = "Address Line 1"
End If

If CountryBox.Text = "" Then
Page2MsgCountryBox = "Country"
End If
If PostalCodeBox.Text = "" Then
Page2MsgPostalCodeBox = "Postal Code"
End If
End If
If AddressButtonNo = True Then
If LastNameBox.Text = "" Or FirstNameBox.Text = "" Or EmployeeNumberBox.Text = "" Then
MsgBox "Please enter the following fields:" & vbCr & vbCr & Page2MsgLastName & vbCr & Page2MsgFirstName & vbCr & Page2MsgEmployeeNumber, vbCritical, "Blank Fields Detected"
Else
'Advance by one page
iPageNo = MultiPage1.Value + 1
MultiPage1.Pages(iPageNo).Visible = True
MultiPage1.Value = iPageNo
End If
End If
If AddressButtonYes = True Then
If LastNameBox.Text = "" Or FirstNameBox.Text = "" Or EmployeeNumberBox.Text = "" Or AddressLineBox1.Text = "" Or CountryBox.Text = "" Or PostalCodeBox.Text = "" Then
MsgBox "Please enter the following fields:" & vbCr & vbCr & Page2MsgLastName & vbCr & Page2MsgFirstName & vbCr & Page2MsgEmployeeNumber & vbCr & Page2MsgAddressLineBox1 & vbCr & Page2MsgCountryBox & vbCr & Page2MsgPostalCodeBox, vbCritical, "Blank Field Detected"
Else
'Advance by one page
iPageNo = MultiPage1.Value + 1
MultiPage1.Pages(iPageNo).Visible = True
MultiPage1.Value = iPageNo
End If
End If
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Maybe you can use something like

Dim ErrorMessage as string

If LastNameBox.Text = "" Then ErrorMessage = ErrorMessage & "Last Name" & vbCr

If FirstNameBox.Text = "" Then ErrorMessage = ErrorMessage & "First Name" & vbCr

If EmployeeNumberBox.Text = "" Then ErrorMessage = ErrorMessage & "Employee Number" & vbCr

....
....

If ErrorMessage <> "" Then MsgBox "Please enter the following fields:" & vbCr & _
ErrorMessage, vbCritical, "Blank Fields Detected"


M.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top