Documente Academic
Documente Profesional
Documente Cultură
In our first exercise we will construct a simple interface to get used to the Visual
Basic Language and Interface. In this program we will create program to Add,
Subtract, Multiply and Divide any two numbers.
Creating a form
To create a ‘form’ the user interface
or ‘front end’ of your program select:
Insert Userform1.
This will add a form item to your
Project manager window.
Should you ever slip out of form design mode you can get back to it by right clicking
Userform1 and selecting View Object.
Changing properties
We will now change some properties to make the program more self-explanatory /
user friendly. To do this we will change the Title top margin to ‘Basic Calculator:
Surname’, change the name of each button to convey the mathematical operation
performed. Add the word Exit to the 5th button. Enlarge to the font size in the used.
During development we will temporarily add values to the first two text boxes to save
having to type values in every time we want to test the program. These will ultimately
be removed.
1
Changing the form title
To change the form Heading or Title of the form
Select the ‘Userform1’.
This can be selected in the form design area or can
be selected from the drop down in the properties
section.
The specify Basic Calculator for the caption property.
Summary
So far all we have is the GUI or Graphical user interface. The main step to this
process ware, (a) creating a form, (b) adding objects to the form and (c) the design
time modification of object properties.
2
Programming your calculator
We will now add the appropriate programming or code to make the program function
as intended. The main steps to this process are:
• Taking values form the text boxes
• Performing the necessary mathematical operation
• Placing the result in another text box.
You will notice that the numbers have just been stuck
together; they have not been added ‘mathematically’.
This is because VB is treating the numbers as regular ‘text’
or a ‘string’. Strings are usually represented by characters
inside inverted commas e.g. “ABCDE”. Numbers in
inverted commas are “12345” is just treated as a string or
a collection of characters which no mathematical meaning.
The “” are not visible but in inferred by the type of variable.
3
A text box as the name suggests can accept text of any type, both letters or
numbers. Therefore the contents / output of the text box is treated as text e.g.
“ABCDE” or “12345”.
While you can return to the form and double click each
button in turn, you can also select the object from the drop
down menu. To minimize typing ‘copy and paste’ the text
from one button to the other.
Next save the Excel file. To save an Excel file which contain a VBA program you
must save it as a Macro Enable file.