You are on page 1of 14

THE TOOLBOX

 The toolbox contains a set of tools we use to embellish a blank form. The tools for working
with a form are found on the menus; the toolbox itself contains the icons representing the
controls we can add to our forms.
 The toolbox is usually located on the far left of the VB screen, but it need not be visible at all
times.
 We can move the toolbox to another location on our screen using ordinary windows drag
and-drop techniques.

The Pointer

The first item on the toolbox is not a control but is used to manipulate controls after we create
them. Click the pointer when we want to select, resize or move an existing control. The pointer is
automatically activated after we place a control on a form.

Command Buttons

When we click a command button, it gives the illusion of being pressed. This optical illusion comes
from the shading used by VB for command buttons, and it is inherited from the windows operating
system.

Image Control

There is one of the two types of standard controls that we can use to display pictures.

Since image controls also recognize the click event, we can use them as graphical replacements for
command buttons. However, ordinary command buttons can also display pictures.

Textboxes

 We use text boxes to display text or to accept user input. Most of the code we write for the
text boxes is to process the information users to enter into them.
 Text boxes can word wrap automatically or display multiple lines of text.
 All the ordinary windows editing tools, such as cutting and pasting, are available when we
enter information in a text box.
 Text boxes can word wrap and may have scroll bars for moving through the text.
 The usual limit for a text box is approximately 32,000 characters.

Labels

 Use tables for information that users shouldn’t be able to change. Labels identify objects and
occasionally we might want to use them to display output.
 Although labels do respond to 12 events, usually they are used passively-for display
purposes only.

Creating controls

 We can always get a control on a form by double clicking on its icon in the toolbox. This gives
us the control in its default size and shape in the middle of the form. This is certainly one
way to proceed.
 We can use a combination of pointing, clicking and dragging to manipulate the toolbox.
 For example: to draw an item from the toolbox on a form at a specific location with a
specific size.
 The pointer control is automatically highlighted when we release the left mouse button as
well.

The name (control name) property:

This property determines the name VB uses for the event procedures we write to make the control
respond to the user.

For example:

Height=500 (or)

Me. height=5000

Then visual basic will always assume we mean to change the height of the current form.

If we wanted to change the height of a command button whose name property is


MyCommandButton, then

MyCommandButton. Height = 500

COMMON FORM PROPERTIES:


1. Caption

 The caption property sets the title of the form.


 The caption is also the title that a Microsoft window uses for the application icon when the
user minimizes the application.
2. Name

 This property is used only in code.


 The default value is, a rather prosaic, form1 and so its value start out the same as the default
value of the caption property.
 This property is mostly used in lager projects.

3. Appearance

 It determines whether the form will have a 3-dimensional look.


 If we leave it at the default value of 1, the form will look 3-dimensional change it to 0, the
forms will appear flat.

4. Border style

 It offers only a small number of choices.


 Set the border style value to 0-none and the application will show no border, therefore no
minimize, maximize or control box buttons. A form created without a border cannot be
moved, resized or reshaped.
 Change this setting to 1-fixed single and the user will no longer be able to resize the window.
All the user will be able to do is maximize or minimize the window.
 The default value, 2-sizable, allows the users to size and shape the form.
 The third setting, 3-fixed double, is not often used for ordinary forms, but it is commonly
used for dialog boxes. It gives a non-sizable border that is twice as thick as normal.
 The forth setting, 4-fixed tool window is not used very often. Under windows 95/98 and NT,
this displays the form with a close button.
 The fifth setting, 5-sizable tool window, work much like the fixed tool window setting. This
will display the form with a close button, and the text from the title bar will appear in a
reduced size.

5. Control box

 Changes to this property go into effect only when a user runs the application.
 As in any Microsoft window application, control boxes are located in the far left corner of
the title bar.
 Clicking the box displays a list of common window tasks, such as window minimizing,
maximizing and closing.
 We have only two choices: we can either have a control box or not.

6. Enabled

 If we set enabled to false, the form cannot respond to any event such as the user clicking on
the form.
 Usually we will use code to toggle this property back and make our form respond
dynamically.
 The properties window id most often used for setting the static properties of our objects and
not dynamic properties.
7. Text

 We can change the font style and size of the text given in the text box to display information
on the form.

8. Height, Width

 Height and width can be set two ways.


 Microsoft windows twips scale to measure the sizes o the objects involved.
 There are 1,440 twips to an inch.
 The term actually stands for one twentieth of a point.

9. Icon

 This property determines the icon our application will display when it is minimized on the
tool bar or turned into a standalone application on the windows desktop.

10. Left, top

 These properties determine the distance between the left or top of the form and the screen.
 Set the value of the top property to 0, the form designing is flush with the top.
 Set the value of the left property to 0, and it will be flush with the left side of the screen.
 Using the form layout window is, another way to control these properties.

11. Mouse pointer, Mouse Icon

 Mouse pointer is a useful property that lets us set the shape of the mouse pointer.
 The default value is 0, as the pull-down list indicates, there are 17 other values.
 Using a value of 11, hourglass changes the mouse pointer to the usual hour glass.
 Set the mouse pointer property to a value of 99 and we will be able to use a custom icon.
 This is most commonly done using code when the cursor is over a specific control.

12. Start-up Position

 This property gives us another way to decide on the initial position of our form at run time.
 It is generally more precise than using the form layout window.
 We have 4 choices; the most common is a setting of 2, which let us to centre the form on the
screen.

13. Visible

 Set the value to false, and the form will no longer be visible.
 We usually will want to make a form invisible only when we are designing an application
with multiple forms.
 We will often want to hide one or more of the form by using the visible property.
 Often, we will reset this property by using code and not at the time we design the
application.
14. Window State

 This property determines how the form will look at run time.
 There are 3 possible settings.
 A setting of 1 reduces the form to an icon, and a setting of 2 maximizes the form. A setting of
0 is the normal default setting.
 This property is most often changed in code.

PROPERTIES OF COMMAND BUTTON:


1. Caption

 The caption property of a form determines the name that shows in the title bar.
 The caption property on the command button determines what the user sees on the face of
the button.
 Any text we use for the caption on a command button is automatically centered within the
button.
 Command buttons always start out with caption like command 1, command 2, reflecting the
default value of the name property.
 The number indicates the order in which the button were created.
 Double-click on the command button icon to create the button in the center of the screen.
 The property window should be visible.

Eg:

Command1. Caption= click

2. Visible

 This property determines whether the command button is visible or not.


 It’s quite common to have our code alternately make a command button visible and
invisible, depending on the situation.
 Like the visible property for forms, this property can only be set to true or false.

3. Enabled

 This property determines whether the button can respond to any event.
 If we set property to false, VB will not respond to any event concerning that button.
 Unlike the visible property, the button remains on the form but is inert.
 The enabled property is more often temporarily toggled on or off via code than set in the
property window, thus maintaining flexibility in our program.

4. Font

 This property controls which font is used for the caption of the button.
 We can only use font at a time.
 When we open up the font dialog box, all the font characteristics- Bold, Italic, font name and
so on can be set independently for each command button.

5. Height, Width

 These properties define the Height and width of the command button.
 Microsoft windows twips scale to measure the sizes o the objects involved.
 There are 1,440 twips to an inch.
 The term actually stands for one twentieth of a point.
 We can change the settings for these properties window or by using the sizing handles.

6. Left, top

 These properties determine the distance between the command button and left edge and
top of the container.
 We can also change them by dragging and as with forms; the values are displayed to the
right on the toolbar.

7. Mouse Pointer

 Setting the mouse pointer to something different than the usual arrow is a good way to give
a user feedback that he or she has moved the focus to the command button.
 The same 17 settings that are available for the mouse pointer on the form are available for a
command button.
 8. Disabled Picture, Down Picture, Picture Style
 VB gives command buttons the ability to display graphics.
 In addition to giving a command button a picture in its normal state, we can set a special
picture when the control is disabled or when it is clicked.
 The command button won’t resize itself to fit the image, we will need to do that by hand.
 To make the command button display a picture, we first need to set the style property to 1.
 After that, we need only specify the picture to be used as the value of the other three
properties.

COMMON PROPERTIS OF TEXT BOXES:


 Text boxes are the primary method for accepting input and displaying output in VB.
 The icon in the tool box for a text box shows the letter ab.
 There are around 50 properties for text boxes.
 As before, the name property is used only for the code we write.

1. Height and Width, Left and Top

 As with command button the height, width, left and top properties use the scale determined
by the surrounding container.

2. Back color, fore color

 Unlike command button, we can set both the back color and fore color properties for a text
box.
 The fore color property affects the color of the text that is displayed.
 Back color affects the rest of the text box.
 Both of these can be set independently of the surrounding container.

3. Enabled

 The enabled property affects whether the text box will respond to events.
 If a text box is disabled, the user cannot enter the text inside it.
 When a text box is disabled it is greyed.

4. Visible

 It is quite common to toggle the visible property between true and false with code, in order
to make a text box appear and disappear.

5. Mouse pointer

 The mouse pointer property has the same 17 possible settings as for form.
 We can often change this property to dramatize that the focus is now within the control.

Special properties for text boxes:

 There are a few properties for text boxes and the border style property works differently
than for forms.

6. Text

 The text property in text boxes is the analog of the caption property for a command button
or a form; it controls the text the user sees.
 When we create a text box, the default value for this property is set to text 1, text 2 and so
on.
 If we want a text box to be empty when the application starts, select the text property and
blank out the original setting. Eg: Text1. Text=a

7. Alignment
 This property controls how text is displayed.
 The default value is 0, which leaves the text left-alignment.
 Use a value of 1 and text is right aligned.
 Use a value of 2 and text is centered.

8. Multiline

 This property determines whether a text box can accept more than one line of text when the
user runs the application and it is usually combined with resetting the value of the scroll bars
property.
 If we set this to true, a user can always use the standard methods in windows to move
through the text box: the arrow keys, home, ctrl+home, end and ctrl+end.
 VB automatically word-wraps when a user types more than one line of information into a
multiline text box, unless we have added horizontal scroll bar to the text box.
 If we have a default command button, the user has to press ctrl+enter to break lines.
 Since forms can only display a limited amount of text and they do not scroll, multiline text
boxes are the usual method for displaying large amounts of text in VB.
 The limit for a multiline text box is approximately 32,000 characters.

9. Scroll Bars

 This property determines whether a text box has horizontal or vertical scroll bars.
 VB allows us to accept long or multiple lines of data from a single text box, roughly 32,000
characters is the limit for a multiline text box.

10. Border Style

 As with the image control, there are only two possible setting for the border style property
for a text box.
 The default value is 1, which gives you a single-width border called a fixed single.
 If we change the value of this property to 0, the border disappears.

11. Max length

 This property determines the maximum number of characters the text box will accept.
 The default value is 0, which means there is no maximum other than the 32,000 characters
limit for multiline text box.
 Any setting other than 0 will limit the user’s ability to enter data into that text box to that
number of character.

12. Password Char

 The password char property lets we limit what the text box displays.
 The convention is to use an asterisk (*) for the password character.
 Once we set this property, all the user sees is a row of asterisks.
 This property is often combined with the max length property to add a password feature to
our program.

13. Locked

 This true/false property lets us prevent users from changing the contents of the text box.
 Users can scroll and highlight text but won’t be able to change it.
 Its advantage over setting the enabled property to false is that the box isn’t greyed.

COMMON PROPERTIES OF LABEL:


 Most common use for label is to identify a text box or other control by describing its
content.
 Another common use is to display help information.
 Labels have 34 possible properties.

1. Caption

 Labels have a caption property that determines what they display, the font being set via the
font property.
 The caption property is originally set to be the same as the default name property.
 At design time we can have one line to text, at most, as the caption for a label.
 We can add blank lines of text to a caption.

2. Height and Width, Left and Top

 As with command buttons, the height, width, left and top properties use the scale
determined by the surrounding container.

3. Font Color

 We can set the back color and fore color properties for a label.
 The fore color property affects the color of the text that displayed.
 Back color affects the rest of the label.
 Both of these can be set independently of the surrounding container.

4. Enabled

 The enabled property is not often used for labels.


 Its primary role is to determine whether the user can move the focus to the control that
follows the label in tab order.

5. Visible property
 It is quite common to have code toggle the visible property between true and false to make
a label appear and disappear.

6. Mouse Pointer

 The mouse pointer property uses the same 17 possible settings.


 This is rarely changed for labels, but one possibility is to change the icon when the user
moves from the label to the control that is being labelled.

Useful properties for label:

 There are five especially useful properties for labels, auto size and word-wrap, works slightly
differently than it does for text boxes.
 We can only use the word wrap property after we set the auto size property to true.
 Also, the border style property has one neat use that can give more polish to our application.

7. Alignment

 The alignment property for a label has three possible settings.


 The usual value is 0, which means the text in the text in the label is left-justified.
 Set the value of this property to 1, and the text inside the label will be right justified.
 Set the value to 2, and the text is centered.

8. Border style, Back style

 The border style property has the same two possible values as text boxes.
 The default value is 0, label do no start out with a border.
 Set the value to 1, the label resembles a text box.
 Using labels with a border style property a value 1 displays output.
 The back style property determines whether the label is transparent or opaque.

9. Auto size, Word wrap

 Unlike command buttons, labels can be made to grow automatically in a horizontal direction
to encompass the text we place in them.
 This is a function of the auto size property.
 The default value is false; we need to change it to true to take advantage of this neat
feature.
 If we also set the word wrap property to true, the label will grow in the vertical direction to
encompass its content, but the horizontal size will stay the same.

The On Error Statement


The heart of error handling in VBA is the On Error statement. This statement
instructs VBA what to do when an run time error is encountered. The On Error
statement takes three forms.

On Error Goto 0
On Error Resume Next
On Error Goto <label>:

The first form, On Error Goto 0, is the default mode in VBA. This indicates that
when a run time error occurs VBA should display its standard run time error
message box, allowing you to enter the code in debug mode or to terminate the
program. When On Error Goto 0 is in effect, it is the same as having no enabled
error handler.

The second form, On Error Resume Next , is the most commonly used and
misused form. It instructs to ignore the error and resume execution on the next line
of code.. It simply instructs VBA to continue as if no error occured. However, the
error may have side effects, such as uninitialized variables or objects set to
Nothing. You do this by testing the value of Err.Number and if it is not zero
execute appropriate code. For example,

On Error Resume Next


N = 1 / 0 ' cause an error
If Err.Number <> 0 Then
N = 1
End If

This statement tests the value of Err.Number and assigns some other number to
N.

The third form On Error of is On Error Goto <label>:which tells VBA to


transfer execution to the line following the specified line label. Whenever an error
occurs, code execution immediately goes to the line following the line label. None of
the code between the error and the label is executed, including any loop control
statements.

On Error Goto ErrHandler:


N = 1 / 0 ' cause an error
'
' more code
'
Exit Sub
ErrHandler:
' error handling code
Resume Next
End Sub

The Resume Statement


The Resume statement instructs VBA to resume execution at a specified point in the
code. The Resume statement takes three syntactic form:

Resume
Resume Next
Resume <label>

The following code attempts to activate a worksheet that does not exist. This causes
an error (9 - Subscript Out Of Range), and the code jumps to the error handling
block which creates the sheet, correcting the problem, and resumes execution at the
line of code that caused the error.

On Error GoTo ErrHandler:


Worksheets("NewSheet").Activate
Exit Sub

ErrHandler:
If Err.Number = 9 Then
' sheet does not exist, so create it
Worksheets.Add.Name = "NewSheet"
' go back to the line of code that caused the problem
Resume
End If

The second form of Resume is Resume Next . This causes code execution to
resume at the line immediately following the line which caused the error.

On Error GoTo ErrHandler:


N = 1 / 0
Debug.Print N
Exit Sub

ErrHandler:
N = 1
' go back to the line following the error
Resume Next

The third form of Resume is Resume <label>: . This causes code execution to
resume at a line label. This allows you to skip a section of code if an error occurs.
For example,

On Error GoTo ErrHandler:


N = 1 / 0
'
' code that is skipped if an error occurs
'
Label1:
'
' more code to execute
'
Exit Sub

ErrHandler:
' go back to the line at Label1:
Resume Label1:

All forms of the Resume clear or reset the Err object.

You might also like