Once a string has been initialized, one of the operations you can perform on it consists of reversing it. To do this, you can call the StrReverse() function. Its syntax is: Function StrReverse(ByVal Expression As String) As String This function takes as argument the string that needs to be reversed. After performing its operation, the function returns a new string made of characters in reverse order. Here is an example: Sub Exercise() Dim StrValue As String Dim StrRev As String StrValue = "République d'Afrique du Sud" StrRev = StrReverse(StrValue) ActiveCell = StrValue & vbCrLf & StrRev End Sub Because the StrReverse() function returns a string, you can write it as StrReverse$. The simplest string is probably one that you declared and initialized. In some other cases, you may work with a string that you must first examine. For example, for some reason, a string may contain an empty space to its left or to its right. If you simply start performing a certain operation on it, the operation may fail. One of the first actions you can take on a string would consist of deleting the empty space(s), if any on its sides. To remove all empty spaces from the left side of a string, you can call the LTrim() function. Its syntax is: Function LTrim(ByVal str As String) As String To remove all empty spaces from the right side of a string, you can call the RTrim() function. Its syntax is: Function RTrim(ByVal str As String) As String To remove the empty spaces from both sides of a string, you can call the Trim() function. Its syntax is: Function Trim(ByVal str As String) As String
If you want to create a string made of one or more empty spaces, you can call the Space() function. Its syntax is: Function Space(ByVal Number As Integer) As String This function is the programmatic equivalent to pressing the Space bar when typing a string to insert an empty space between two characters.
A message box is a special dialog box used to display a piece of information to the user. The user cannot type anything in the message box. There are usually two kinds of message boxes you will create: one that simply displays information and one that expects the user to make a decision. A message box is created using the MsgBox function. Its syntax is: Function MsgBox(Prompt[, Buttons] [, Title] [, Helpfile, Context]) As String The MsgBox() function takes five arguments and only the first one is required.
The Prompt argument is the string that the user will see displaying on the message box. As a string, you can display it in double quotes, like this "Your credentials have been checked.". Here is an example: Sub Exercise() MsgBox ("Your credentials have been checked.") End Sub This would produce: You can also create the message from other pieces of strings. The Prompt argument can be made of up to 1024 characters. To display the Prompt on multiple lines, you can use either the constant vbCrLf or the combination Chr(10) & Chr(13) between any two strings. Here is an example: Sub Exercise() MsgBox ("Your logon credentials have been checked." & _ vbCrLf & "To complete your application, please " & _ "fill out the following survey") End Sub This would produce: If you call the MsgBox() function with only the first argument, it is referred to as a method (a method is a member function of a class; the class in this case is the Application on which you are working). If you want to use the other arguments, you must treat MsgBox as a function. That is, you must assign it to a variable or to an object.
The Buttons argument specifies what button(s) should display on the message box. There are different kinds of buttons available and the VBA language. Each button uses a constant integer as follows:
When calling the MsgBox() function and specifying the button, you can use one of the above constant numeric values. Here is an example that displays the Yes and the No buttons on the message box: Sub Exercise() ActiveCell = MsgBox("Your logon credentials have been checked " & _ "and your application has been approved: " & _ "Congratulations!" & vbCrLf & _ "Before leaving, would you like " & _ "to take our survey survey now?", vbYesNo) End Sub This would produce:
Besides the buttons, to enhance your message box, you can display an icon in the left section of the message box. To display an icon, you can use or add a member of the MsgBoxStyle enumeration. The members that are meant to display an icon are:
To use one of these icons, you must combine the value of the button to the desired value of the icon. To perform this combination, you use the OR operator. Here is an example: Sub Exercise() Dim iAnswer As Integer iAnswer = MsgBox("Your logon credentials have been checked " & _ "and your application has been approved: Congratulations!" & _ vbCrLf & "Before leaving, would you like " & _ "to take our survey survey now?", vbYesNo Or vbQuestion) End Sub This would produce: When calling the MsgBox() function, if you want to show one or more buttons and to show an icon, you can use either two members of the MsgBoxStyle enumeration using the OR operator, or you can add one of the constant values of the buttons to another contant values for an icon. For example, 3 + 48 = 51 would result in displaying the buttons Yes, Ne, and Cancel, and the exclamation icon.
If you create a message box with more than one button, the most left button usually has a thick border, indicating that it is the default. If the user presses Enter after viewing the button, the effect would be the same as if he had clicked the default button. If you want, you can designate another button as the default. To do this, you can use or add another member of the MsgBoxStyle enumeration. The members used to specify the default button are:
Once again, to specify a default value, use the OR operator to combine a Default Button Constant with any other combination. Here is an example: Sub Exercise ActiveCell = MsgBox("Your logon credentials have been checked " & _ "and your application has been approved: Congratulations!" & _ vbCrLf & "Before leaving, would you like " & _ "to take our survey survey now?", _ vbYesNo Or _ vbQuestion Or vbDefaultButton2) End Sub This would produce: These additional buttons can be used to further control what the user can do:
The Title argument is the caption that would display on the title bar of the message box. It is a string whose word or words you can enclose between parentheses or that you can get from a created string. The Title argument is optional. As you have seen so far, if you omit, the message box would display the name of the application on the title bar. Otherwise, if you want a custom title, you can provide it as the third argument to the MsgBox() function. The caption can be a simple string. Here is an example: Sub Exercise() ActiveCell = MsgBox("Your logon credentials have been checked " & _ "and your application has been approved: Congratulations!" & _ vbCrLf & "Before leaving, would you like " & _ "to take our survey survey now?", _ vbYesNo Or vbQuestion, _ "Crofton Circle of Friends - Membership Application") End Sub This would produce: Notice that the caption is now customized instead of the name of the application. The caption can also be a string created from an expression or emanating from a variable or value.
The MsgBox() function can be used to return a value. This value corresponds to the button the user clicked on the message box. Depending on the buttons the message box is displaying, after the user has clicked, the MsgBox() function can return a value. The value can be a member of the MsgBoxResult enumeration or a constant numeric value recognized by the Visual Basic language. The value returned can be one of the following values:
The Visual Basic language provides a function that allows you to request information from the user who can type it in a text field of a dialog box. The function used to accomplish this is called InputBox and its basic syntax is: InputBox(prompt[, title] [, default] [, xpos] [, ypos] [, helpfile, context])
The most basic piece of information you can provide to the InputBox() function is referred to as the prompt. It should be a string that the user will read and know what you are expecting. Here is an example: Sub Exercise() InputBox("Enter your date of birth as mm/dd/yyyy") End Sub This would produce Upon reading the message on the input box, the user is asked to enter a piece of information. The type of information the user is supposed to provide depends on you, the programmer. Therefore, there are two important things you should always do. First you should let the user know what type of information is requested. Is it a number (what type of number)? Is it a string (such as the name of a country or a customer's name)? Is it the location of a file (such as C:\Program Files\Homework)? Are you expecting a Yes/No True/False type of answer (if so, how should the user provide it)? Is it a date (if it is a date, what format is the user supposed to enter)? These questions mean that you should state a clear request to the user and specify what kind of value you are expecting. A solution, also explicit enough, consists of providing an example to the user.
The second argument to the InputBox() function allows you to optionally specify the title of the input box. This is the string that would appear on the title bar. Since this is an optional argument, if you don't pass it, the input box would display the name of the application. Otherwise, to display your own title bar, pass the Title argument. The title is passed as a string. Here is an example: Sub Exercise() ActiveCell = InputBox("Please enter your date of birth as mm/dd/yyyy", _ "Student Registration") End Sub This would produce: Notice that the caption is now customized instead of the name of the application. The caption can also be a string created from an expression or emanating from a variable or value.
Sometimes, even if you provide an explicit request, the user might not provide a new value but click OK. The problem is that you would still need to get the value of the text box and you might want to involve it in an expression. You can solve this problem and that of providing an example to the user by filling the text box with a default value. To support this, the InputBox() function provides the third argument. To present an example or default value to the user, pass a third argument to the InputBox() function. If you want to use this argument to provide an example the user can follow, provide it with the right format. Here is an example: Sub Exercise() ActiveCell = InputBox("Enter Student Name:", _ "Student Registration", "John Doe") End Sub Here is an example of running the program: Notice that, when the input box displays with a default value, the value is in the text box and the value is selected. Therefore, if the value is fine, the user can accept it and click OK. Another way you can use the default value is to provide a value the user can accept; that is, the most common or most likely value the user would enter. Here is an example: Sub Exercise() ActiveCell = InputBox("Enter Birth State:", _ "Student Registration", "VA") End Sub Here is an example of running the program: Once again, notice that the user can just accept the value and click OK or press Enter.
By default, when the input box comes up, it displays in the middle of the screen. If you want, you can specify where the input box should be positioned when it comes up. To assist you with this, the InputBox() function is equipped with a fourth and a fifth arguments. The fourth argument specifies the x coordinate of the input box; that is, the distance from its left border to the left border of the monitor. The fifth argument specifies the distance from the top border of the input box to the top border of the monitor.
When the input box displays, after typing a value, the user would click one of the buttons: OK or Cancel. If the user clicks OK, you should retrieve the value the user would have typed. It is also your responsibility to find out whether the user typed a valid value. Because the InputBox() function can return any type of value, it has no mechanism of validating the user's entry. To retrieve the value of the input box dialog when the user clicks OK, you can get the returned value of the InputBox() function. After being used, the InputBox() function returns a string. Here is an example of getting it: Sub Exercise() Dim StudentName As String StudentName = InputBox("Enter Student Name:", _ "Student Registration") MsgBox ("Student Name: " & StudentName) End Sub You can also get any type of value from an input box. That is, when the InputBox() function exits, thanks to the flexibility of the Visual Basic language, the compiler can directly cast the returned value for you. Here is an example: Sub Exercise() Dim DateOfBirth As Date DateOfBirth = InputBox("Please enter your date of birth as mm/dd/yyyy", _ "Student Registration") MsgBox("Date of Birth: " & DateOfBirth) End Sub |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|