
Function is something in programming that gives us an answer, in other words, returns a value. Functions always have brackets (). Below are common functions you can use, please note some of them as are very important to your project.
Date & Time functions
- Date() Give you today’s date. E.g. 11/30/01
- Day(), Month(), Year() To get Day, Month or Year separately. E.g. Day(Date()) returns 30
- CDow(),CMonth Give name of the day/month. E.g. cdow(date()) to Friday
- Time() Give Current’s Time E.g. 20:19:27
- DateTime() Give both date and time E.g. 11/30/01 08:19:49
- Sec(),Min(), Hour() Get Seconds, Minutes, Hours separately. E.g. Sec(DateTime()) to 49
Text functions
- Upper() Convert the text to capital. E.g. Upper(“Computer”) Result: COMPUTER
- Lower() Convert the text to lower cases. E.g. Lower(“CoMpUTER”) Result: computer
- Proper() Convert the text to Proper case E.g. Proper(“coMPutEr”) Result: Computer
- Alltrim() Take out all spaces in betweens. E.g. Alltrim(“ computer “) Result: “computer”
- Str() Convert Number into Character E.g. Str(12) Result: “12”
- Val() Convert Character into Number E.g. Val(“12”) Result: 12
- Left(), Right() Get a starting letters on the left/end letters on the right in specific no. of letters. E.g. Left(“Computer”,4) to “Comp”, Right(“Computer”,3) Result: “ter”
- SubStr( [String] , [Starting Index] , [No. of letters] ) More effective than left() or Right() Get a specific no. of letters of specific starting position. E.g. SubStr(“Computer”,2,4) Result: “mput”
- IsAlpha(),IsDigit() Check that it is a Number or Character. E.g. IsAlpha(23441) Result: .f.
- IsBlank() Check that the data/text is blank or not E.g. IsBlank(“IT”) Result: .f.
Those functions should be used in the project at all times. Please note those functions carefully.
Why it is so important and why you need to use it?
Many functions allow you to have consistent expectations and set standards both for your program and the database. Fox an example, case functions such as Lower, Upper, and Proper allows the program to set the case to store in the database to make sure they are consistent. This will help when you want to search for the data for that field, since searching by default is case-sensitive.
In the case you keep everything standard in the previous example, like lower cases, when user enters the data, you convert it to lower case, so new record stores as lower cases. When user searches, you convert the user search’s word into lower cases, so it matches the database’s standard.
How can I apply that concept into my project and perform error-checkings?
Below is the example of the program that will ask the user to enter his/her name and display it. Some functions used here are also explained below in Others functions section.
UserName = Space(20)
Err = .T. Set the variable to see that there is an error or not
Do while Err = .T. If there is an error, ask the question again
@ 5,5 say “Please enter your name:”
@ 6,5 get UserName Get Username of the user
Read
do case Error checking:
case IsBlank(UserName) = .T. If user didn’t enter username (left blank)
@ 10,50 say “Please enter your name!” Show an error message
Wait “Press any key to re-enter your name again…” timeout 10
case IsAlpha(UserName) = .F. If user didn’t enter as string (letters)
@ 10,50 say “Please enter your real name! Not a set of numbers!”
wait “Press any key to re-enter your name again…” timeout 10
Otherwise
Err = .F. Else, There is no error
EndCase
Clear Clear the screen
Enddo
Show the username, if no error.
@ 5,5 say “Your name is “ + AllTrim(Proper(UserName))
Click here to download the sourcecode for an above example
Other useful functions
- Pi() Give out the Pi value
- Tan() Calculate Tan E.g. Tan(PI()/4) = 1.00
- Rand() Random the number (From 0 to 1)
- Chr() Give out the character from the ASCII code
- Asc() Give out the ASCII code from the character
- RecNo() Give out the current record no. selected E.g. ? RecNo()
- RecCount Give out the total no. of records in the table. E.g. ? Reccount()
- FCount Give out the total no. of fields in the table
- BOF() / EOF() This will explain under Database programming with FoxPro topic
- LastKey() Return ASCII no. of the key last pressed
Third-party custom functions
|
|
Converts numbers into English words. E.g. ? InWords(130) Result: One Hunred Thirty Dollars Only
|
| |
|
Extended: Messagebox() function
I found out about this function when I was creating this FoxPro Summary. This function let you create a message box. (Like the one pops up when you are closing the document asking you to click Yes | No | Cancel, when you didn’t save your work yet )
Messagebox ( [message as string], [Options as numbers],[Title as string] )
E.g.
- Messagebox (“Hello World!”) This will pops up the message saying Hello World!
- Messagebox (“Hello World!”,”Welcome”) Same as above, but its title is Welcome
You want it to be like having different buttons, like Yes & No. You want it to have Question mark sign or Exclamation sign, and you want to know how you can get the user’s choice. Here will explain how to do all those things.
In [Options], you can give 3 pieces of information: Buttons type, Icons type and Default button. I will skip Default button for now. Those information is classed as numbers. E.g. Question icon is 32, so Messagebox(“Hello!”,32,”Test”) will shows a message with a question mark icon.
Yes & No buttons is identify as no.4. E.g. Messagebox (“Hello!, 4 + 32,”Test”) will give me Question icon and Yes & No buttons. You can see the full list of options in Messagebox function references section below.
Now, How can you get user Input? Messagebox() function returns user’s input as a number. (From 1-7) E.g. No. 6 is “Yes” and No. 7 is “No”. You can use If..Else…EndIf statement to help you. Below is the example:
Answer = 0 To Store the user’s input
Answer = MessageBox(“Are you sure that you want to exit?”,4+32,”Confirm Exit”)
Get the user’s input form MessageBox
Do case
case Answer = 6 then If User answer Yes then
@ 5,5 say “You have chosen to Exit the program”
case Answer = 7 then If User answer No then
@ 5,5 say “You have chosen NOT to Exit the program”
endcase
Messagebox() function references
|
Value
|
Dialog box buttons
|
|
Value
|
Icon
|
|
0
|
button only
|
|
16
|
Stop sign
|
|
1
|
buttons
|
|
32
|
Question mark
|
|
2
|
buttons
|
|
48
|
Exclamation point |
|
3
|
buttons
|
|
64
|
Information icon
|
|
4
|
buttons
|
|
|
|
|
5
|
buttons
|
|
|
|
|
Return value
|
Button
|
|
1
|
OK
|
|
2
|
Cancel
|
|
3
|
Abort
|
|
4
|
Retry
|
|
5
|
Ignore
|
|
6
|
Yes
|
|
7
|
No
|
Use Messagebox() as a module
If your program is going to use a lot of this function then, you might want to use it as a module. This section will tell you how to use Messagebox () Function in a proper way.
Below is the example: (Using the previous example)
Answer = 0 To Store the user’s input
Message = “Are you sure that you want to Exit?” To store the message
Title = “Confirm Exit” To Store the title
Options = 4 + 32 To Store the Options
Answer = MessageBox(Message, Options, Title)
You can even extend this idea further by preparing generic Messageboxes in a module that you either pass in a name or number and that module will show up that particular messagebox. For example, you may want to say information saved, there can be a generic messagebox for that.