Built-In Functions |
|
|
|
String-Based Functions |
Introduction |
A string-based function is one that deals with functions; either it manipulates them or returns them. Microsoft Visual Basic allows you to be specific about the return value you are expecting. Some of the functions you will be using can be configured to return exactly a string. Such functions use the $ suffix that states it clearly. |
Message Boxes |
A message box is a special form used to display a piece of information to the user. As opposed to a regular form, the user cannot type anything on the box. There are usually two kinds of dialog boxes you will create: one that simply displays information and one that expects the user to make a decision. To create a message box, you can use the MsgBox function. There are two techniques to use it. To display a simple message with just an OK button, use the MsgBox method whose syntax is MsgBox Message The parameter, Message, is the string to present to the user. As a normal, it should be passed in double-quotes. Here is an example: Private Sub Form_Load() MsgBox "Welcome to the wonderful world of Microsoft Visual Basic" End Sub When the above version of the the MsgBox function is used, a rectangular form (we will learn later on that this type of form is called a dialog box) is presented to the user, display a string message and an OK button: Another version of the MsgBox function allows you to present a message that asks a question to the user, expecting a decision. This version displays a more informative prompt with more than one button. The user makes a decision by clicking one of the presented buttons. After the user has clicked a button, you can then retrieve the result and use it as you see fit. The syntax of this version is: MsgBox Message, [Buttons], [Title], [HelpFile], [Context] The Message argument is the string that the user will see displaying on the message box. As a string, you can display it in double quotes. You can also create it from other pieces of strings. The Message argument can be made of up to 1024 characters. To display the Message on multiple lines, you can use either the constant vbCrLf or the combination Chr(10) & Chr(13) between any two strings. Besides the Message parameter, this version allows you to display more than one button. If you don't need to, you don't have to specify the buttons. If you don't, the message box would appear with only an OK button. Otherwise, you can specify what buttons to display. This is done using the Buttons argument. There are different kinds of buttons available and Visual Basic recognizes them by a numeric value assigned to each. The buttons are
Here is an example of a message box that display a Yes and a No buttons: Private Sub Form_Load() MsgBox "Are you ready to rumbleeeeeeeeeee", vbYesNo End Sub This would produce: When a message box displays more than one button, one of the buttons usually has a thick button. That button is also called the default button. If the user presses Enter upon reading the message, the compiler would behave as if the default button was clicked. There are some buttons that are set automatically as default when you create the message box. If you don't like the set button to be the default, you can specify which one you prefer as default. To do that combine a second value with one of the above values for the buttons. You can set the default argument using the following table
To combine one of these values with one of the buttons, use the OR operator between them. Here is an example: Private Sub Form_Load() MsgBox "Are you ready to rumbleeeeeeeeeee", vbYesNo Or vbDefaultButton2 End Sub This would produce: These additional buttons can be used to further control what the user can do:
Besides the message and the button(s), you can also display a friendly icon on the message box. To do that, combine the button value with one of the following:
Here is an example: Private Sub Form_Load() MsgBox "Are you ready to rumbleeeeeeeeeee", vbYesNo Or vbQuestion End Sub This would produce: As you can see on the message boxes we have used so
far, by default, a message box displays the name of the application it
belongs to in its title bar. If you want, you can display your own title.
This is done using the Title argument which is also called the caption
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. Private Sub Form_Load() MsgBox("Are you ready to rumbleeeeeeeeeee", vbYesNo Or vbQuestion) End Sub When treated as a function, MsgBox returns a value. This value corresponds to the button the user clicks on the message box. Depending on the buttons the message box is displaying, after the user has clicked, the MsgBox function can return one of the following values:
|
Practical Learning: Using Message Boxes |
|
The Input Box |
Like a message box, an input box is a (relatively) small form (in reality, it is a dialog box) that displays a message to the user. Unlike a message box, an input box presents a small text box that expects the user to enter a value. After using it, the user can either send the form with the new value or dismiss it without any change. To create an input box, you can use the InputBox function procedure prompts the user to enter some information in a message box, and the function will return the content of that box. |
The Character To ASCII Conversion |
The Chr function is used to associate an entered character with its ASCII character equivalent. It could be used to convert a number to a character. It could also be used to break a line in a long expression. The syntax of the Chr function is: Chr(Number) A combination of Chr(13) and Chr(10) would break a line in an expression.
|
Case Conversion |
If you are presented with a string or an expression whose cases must be the same, you can convert all of its characters in either uppercase or lowercase. To convert a character, a string or an expression to uppercase, you can call the UCase or the UCase$ function. These functions take one argument as the string or expression to be considered. The syntaxes are: Function UCase(Letter As Char) As Char Function UCase(Expression As String) As String The first version receives one character as argument. If the character is already in uppercase, it would be return the same. If the character is not a readable character, no conversion would happen and the function would return it. If the character is in lowercase, it would be converted to uppercase and the function would then return the uppercase equivalent. The second version considers the argument supplied as a string. Any letter that is in lowercase in the string would be converted to uppercase. Any letter that is in uppercase would be preserved and would not be changed. Any non-alphabetic character in the string would be kept "as is". |
Logical Functions |
Is it Empty? |
A logical function is one that checks whether an expression is true or false and then return a Boolean value. The IsEmpty function check whether a field is empty. Its syntax is: IsEmpty(Expression) In this case, the Expression argument will be checked. If it is empty, the IsEmpty function returns True. If the expression or field is not empty, that is, if it contains something, the function returns False.
|
Is it Null? |
Another problem you may encounter when involving an operation or the contents of a control is whether it has never contained a value. This operation is sometimes confused with that of checking whether a field is empty. Here is the difference (it is important to understand this because it is used in many other environments):
To check whether an expression or the value of a control is null, you can call the IsNull() function. Its syntax is: IsNull(Expression) Also used on fields, the IsNull() function checks the state of a field (remember, this functions does not check whether a field is empty or not; it checks if the field has ever contained a value). If the field it null, this function returns True. If the field is not null, this function returns False.
|
Date and Time Functions |
Current Data and Time |
Microsoft Visual Basic provides various functions to perform date and time related operations. These functions allow you to add dates or times, find the difference between dates or times, or add constant values to dates or times. The current date is represented by a function called Date. The Date() function is used to get the system date of the computer. You can use it to display today's date, provided your computer has the correct date. The current time of the computer is represented by a function called Time. The Time() function is used to get the system time of the computer. The Date() and Time() functions can be combined and are represented by a function called Now. |
Practical Learning: Using Date and Time |
|
Day - Month - Year |
The Day function is used to get the numeric value that represents a day in the month. It ranges from 1 to 31 included. The formula of the Day function is Day(DateValue) The Month function displays the numeric month of a date. It ranges from 1 to 12 included. The formula of the Month function is Month(DateValue) The Year function returns the numerical year of a date. The formula is the Year function is Year(DateValue) |
Practical Learning: Using Day, Month, and Year Values |
|
Adding a Date |
The DateAdd function is used to add a date value to another date. It can be used to add a number of days, weeks, months, or years to another date. The formula for the DateAdd function is DateAdd(Interval, Number, date) Required, the Interval argument specifies the kind of value you want as a result. This argument will be enclosed between double quotes and can have one of the following values:
Required also, the Number argument specifies the number of units you want to add. If you set it as positive, its value will be added. On the other hand, if you want to subtract, make it negative. The number represents the units of the Interval argument you want to add. The date argument is the date to which you want to add the number. |
Subtracting a Date |
The DateDiff function is used to find the difference between two date or time values. It allows you to find the number of seconds, minutes, hours, days, weeks, months, or years from two valid date or time values. The DateDiff function takes 5 arguments, 3 are required and 2 are optional. The formula of the function is DateDiff(Interval, Date1, Date2, Option1, Option2) Required, the Interval argument specifies what kind of value you want as a result. This argument will be enclosed between double quotes and can have one of the following values:
Required also, the Date1 and Date2 argument specify the date or time values that will be used when performing the operation. By default, the days of a week are counted starting on Sunday. If you want to start counting those days on another day, supply the Option1 argument using one of the following values: vbSunday, vbMonday, vbTuesday, vbWednesday, vbThursday, vbFriday, vbSaturday. There are other variances to that argument. If your calculation involves weeks or finding the number of weeks, by default, the weeks are counted starting January 1st. If you want to count your weeks starting at a different date, use the Option2 argument to specify where the program should start. |
Practical Learning: Subtracting Dates |
|
|
||
Previous | Copyright © 2001-2004-2014 FunctionX, Inc. | Next |
|