You are on page 1of 33

Siliguri Institute of Technology

Department of Computer Applications

Lab Manual

MCA-E592B
Windows Programming Lab

Prepared By:
Ammlan Ghosh
Assistant Professor
Department of Computer Application

Dept. of Computer Application, SIT 0 | Page


Laboratory Manual MCA-E592B
Quick Primer on Saving Visual Basic Applications
 When saving Visual Basic applications, you need to be concerned with
saving both the forms (.FRM) and modules (.BAS) and the project file
(.VBP).
 In either case, make sure you are saving in the desired directory.
 The current directory is always displayed in the Save window. Use
standard Windows techniques to change the current directory.
 The easiest way to save a new project is to click the ‘Save Project’ button (it
looks like a floppy disk) on the Visual Basic toolbar.
o First, you will be asked where you want to save your forms and
modules, then where you want to save your project file.
o Once you’ve done this, subsequent clicking on the Save Project
toolbar button will automatically save your forms, modules, and
project file in their specified locations.
 To open a saved project, simply click the ‘Open Project’ button (looks like a
file folder). There is a corresponding Open command under the File menu
to open project files (File Open).
 If you prefer to save without the toolbar, save your Project from Menu File
 Save Project.
o This action saves all forms and modules in the current project using
their current names and also saves the project file.
 Or you may use File Save Project As.
o This option is like Save Project, however you have the option to
change file names.
o When you choose this option, if you have not saved your forms or
modules, you will also be prompted to save those files.
 Students are advised to name their forms prefixed by ‘frm’ and a project
should be prefixed by ‘prj’. For example, if you write a program for
checking a string a prlindrome or not, the form name may be
‘frmPalindrome’ and project name may be ‘prjPalindrome’. Note that how
the file name is prefixed with ‘frm’ and the project is prefixed with ‘prj’.
 Save your project by creating a appropriated directory by adding your Roll
Number under C:\MCA-E592B folder.

Dept. of Computer Application, SIT 1 | Page


Laboratory Manual MCA-E592B
Experiment Set-I

Experiment 1-1. Beep Problem


Build an application with a command button. When the button is clicked, make the
computer beep (use the Beep function).

Experiment 1-1. Caption Problem


Build an application with a command button. When the button is clicked, change the
button’s Caption property. This allows a command button to be used for multiple purposes.
If you want to change the button caption back when you click again, you’ll need an If
statement. We’ll discuss this statement in the next class, but, if you’re adventurous, look in
on-line help to try it.

Experiment 1-3. Enabled Problem


Build an application with two command buttons. When you click one button, make it
disabled (Enabled= False) and make the other button enabled (Enabled= True).

Experiment 1-4. Date Problem


Build an application with a command button. When the button is clicked, have the
computer display the current date in a label control.

Dept. of Computer Application, SIT 2 | Page


Laboratory Manual MCA-E592B
Experiment Set-II
Saving Account
Start a new project. The idea of this project is to determine how much you save by making
monthly deposits into a savings account. For those interested, the mathematical formula
used is:
F = D [ (1 + I)M- 1] / I Where F - Final amount D - Monthly deposit amount
I - Monthly interest rate M - Number of months
A. Form Design as Follow:

B. Set the Properties of the form and each object:


Object Properties Values Object Properties Values
Name frmSavings Name txtInterest
Savings Text 2
Form Caption Text [Blank]
Account
BorderStyle 1-Fixed Single Name txtMonths
Label 1 Monthly Text 3
Caption Text [Blank]
Deposit
Label 2 Caption Yearly Interest Name txtFinal
Label 3 Number of Text 4
Caption Text [Blank]
Months
Label4 Caption Final Balance Name cmdCalculate
Command 1
Name txtDeposit Caption &Calculate
Text1
Text [Blank] Name cmdEXit
Command 2
Caption E&xit

C. Write the appropriate code.

Dept. of Computer Application, SIT 3 | Page


Laboratory Manual MCA-E592B
Experiment Set-III

A. Creating User Interface


In this lab you will create the reservation application for Hotel RAJDARBAR in
Visual Basic. You will add controls and work with control arrays and their properties.

Figure: Hotel Rajdarbar Reservation Form

Exercise 1: Creating the User Interface


In this exercise you will create the user interface by adding controls to the Hotel
Rajdarbar reservation form. Some of these controls will be implemented as control arrays.

 To create a new Visual Basic Project:


1. Open a new Standard EXE Project. Define the following project and form properties.

Object Properties Value


Project1 Name HotelReservation
Name frmReservation
Form1 BorderStyle 1-Fixed Single
Caption Hotel Reservation

Dept. of Computer Application, SIT 4 | Page


Laboratory Manual MCA-E592B
2. Add the following controls to the form.
Object Control Type Properties Value
Name txtFistName
Text1 Text Box
Text <blank>
Name txtLastName
Text2 Text Box
Text <blank>
Name txtAddress
Text3 Text Box Text <blank>
MultiLine True
Name txtPhone
Text4 Text Box
Text <blank>
Name txtNumPeople
Text5 Text Box
Text <blank>
Name txtNumDays
Text6 Text Box
Text <blank>
Name txtCheckOut
Text7 Text Box
Text <blank>
Name txtRoomNumber
Text8 Text Box
Text <blank>
Name txtRate
Text9 Text Box
Text <blank>
Name FraPmtType
Frame Frame
Caption Payment Type
Name CmdDone
Command1 Command Button
Caption &Done

Note: Use each control’s TabIndex property to contrl the order in which the user will navigate
through the form when pressing the TAB key.

 To Create control Arrays


1. On frmReservation, add the following three optionButton controls to the Frame control
by clicking OptionButton in the tool box, and then the drawing the controls inside of
fraPmtType.

Object Control Type Properties Value


Name grpPmtType
Option1 OptionButton Index 0
Caption Credit Card
Name grpPmtType
Option2 OptionButton Index 1
Caption Cheque
Name grpPmtType
Option3 OptionButton Index 2
Caption Cash

2. Add the appropriate labels for the textboxes on the hotel reservation form, as shown in
Figure.

Dept. of Computer Application, SIT 5 | Page


Laboratory Manual MCA-E592B
Excercise2: Adding Custom controls to the Form.
In this exercise you will add the Masked Edit and Status Bar Custom controls to the Hotel
Rajdarbar reservation form. You will modify the properties of these controls at design time
in the Properties Page and programmatically at run time. The form with status bar is
illustrated in the figure.

 To add ActiveX controls to your project and form.


1. On the Project Menu, click Components (or Press Ctrl+T).
2. Select the Microsoft Windows Common Controls 6.0 and the Microsoft Masked
Edit Control 6.0 check boxes.
3. Click OK to close the Component dialog box.
(The new controls appear in the Tool box)
4. Add the following control to the form and set their properties:

Object Control Type Properties Value


MaskEdBox1 MaskedEdBox Name MskCheckIn
Mask ##-##-####
StatusBar1 Status Bar Name staAddtionalInfo

5. Add a Label Controls with the Caption property set to Check-in-Date. Position the
label to the left of the MaskEdBox control.

 To modify an ActiveX control’s Properties.


1. Add StatusBar control to the form.
2. Right Click on the status bar and click Properties. The Control’s Properties pages
appear
3. Click the Panels tab in the StatusBar Property Pages dialog box and set the following
properties for index 1:

Index 1 Property Value


ToolTip Text The Current Time
Key Time
Style 5-sbrTime
Bevel 0-sbrNoBevel

4. On the Panels tab, click the Insert Panel button.


5. Change the Index Property to 2 by clicking the arrow to the right of the Index text
box and setting the following properties:

Index 2 Property Value


ToolTip Text The Current Date
Key Date
Style 6-sbrDate
Bevel 2-sbrRaised

6. On the Panels tab, click the Insert Panel button.

Dept. of Computer Application, SIT 6 | Page


Laboratory Manual MCA-E592B
7. Change the Index property to 3 by clicking the arrow to the right of the Index text
box and setting the following properties:

Index 3 Property Value


ToolTip Text Additional Information
Key addInfo
Style 0-sbrText
Bevel 1-sbrSpring
AutoSize 1-sbrSpring

8. Click OK

 To add text to the StatusBar Panel.


1. Double-click one of the option buttons in the Payment Type frame. The following
procedure appears:

Private Sub grpPmtType_Click(Index As Integer)

End Sub

2. Add the follwing code to the grpPmtType_Click event procedure:

Private Sub grpPmtType_Click(Index As Integer)


Select Case Index
Case 0 'Credit Card
staAdditionalInfo.Panels("addinfo").Text = _
"Visa, M<aster Card or Ameriacal Express
accepted. "
Case 1 'Cheque
staAdditionalInfo.Panels("addinfo").Text = _
"Picture ID and cheque guarantee card
required."
Case 2
staAdditionalInfo.Panels("addinfor").Text = _
"Hotel's do not carry a log to extra change."
End Select
End Sub

3. Save your Application to the folder D:\VBPracticeFolder\<YourName>. Save the


project as HotelRes.vbp. Save the form as FrmRsvn.frm
4. Run and text the application.

Dept. of Computer Application, SIT 7 | Page


Laboratory Manual MCA-E592B
Experiment Set-IV
(Continuation from Experiment Set- III)

Adding a Menu Bar.


In this exercise, you will use the Menu Editor to create a menu bar for the Hotel
Rajdarbar Reservation form. You will add code to the click event of the Exit menu item.

 To add a menu to the project

1. Click the frmReservation form.


2. On the Tools menu, click Menu Editor, or press Ctrl+E button from key board
3. In the Caption text box enter &File. (The ampersand (&) provides an access key to
the menu)
4. In the Name text box enter mnuFile. This is the name that you will use to refer to the
menu control in code.
5. Repeat steps 3 and 4 to create the following menu structure:

Menu Item Property Value


Caption &File
File
Name mnuFile
Caption E&xit
Exit
Name mnuFileExit
Caption &Guest
Guest
Name mnuGuest
Caption &Reservation
Reservation
Name mnuGuestReservation
Caption &Add
Add
Name mnuGuestReservationAdd

Dept. of Computer Application, SIT 8 | Page


Laboratory Manual MCA-E592B
Caption Check-&In
Check-In
Name mnuGuestReservationCheckIn
Caption Check-&Out
Check-Out
Name mnuGuestReservationCheckOut
Caption &Cancel Reservation
Cancel Reservation
Name mnuGuestReservationCancel
Caption &Search
Search
Name MnuGuestSearch
Menu Item Property Value
Caption &Help
Help
Name MnuHelp
Caption &Contents……
Contents
Name MnuHelpContents
Caption &About
About
Name mnuHelpAbout

6. Click OK to close the menu Editor.

 To add code to menu Items:


1. On the Forms menu, click File and then click Exit. The mnuFileExit_Click event
appears.
2. Add the Unload method to the mnuFileExit_Click event.

Private Sub mnuFileExit_Click


Unload Me
End Sub
3. Save and test the application

Note: In the next assignment example you have to continue from this project. So do the
project carefully. If you have not changed the properties of the control, as it is stated, you
will face problem.

Dept. of Computer Application, SIT 9 | Page


Laboratory Manual MCA-E592B
Experiment Set-V
(Continuation from Experiment Set- IV)

Adding Field and Form Level Validation.

In this lab you will add form and field level validation to the Hotel Rajdarbar
Reservation system. You will modify control properties and add code to control events to
validate data entered into the form. In subsequent labs, you will continue to add to the
application and explore each of the tasks you try here in more detail. You can continue to
work with the files you create in Assignment Example one.

A. Controlling User Navigation.


In this exercise, you will create two procedures that control user navigation by
enabling and disabling the form’s controls. These procedures will be called from Form and
Control Events.

Open the Hotel Rajdarbar Reservation Project you have saved in the Assignment
Example one.

 To create sub procedures that enables and disables controls:


1. Open frmReservation ‘s code module. On the Visual Basic Tools Menu click
AddProcedure….
2. Using the Add Procedure dialog box, create two private Sub Procedures named
DisableControls and EnableControls.
The following two procedures are displayed in the form’s module.

Private Sub DisableControls()

End Sub

Private Sub EnableControls()

End Sub.

3. The DisableControls procedures code will loop through the controls collection using
the For Each … Statement. It will disable all but he Munu and Label contgrls,a dn set
the MaskEdBox and the Text box control’s BackColor property to gray. Add the
following code to the DisableControls procedure.

(Continued in next Page…..)

Dept. of Computer Application, SIT 10 | P a g e


Laboratory Manual MCA-E592B
Private Sub DisableControls()
Dim ctl As Control
For Each ctl In Controls
If TypeOf ctl Is Menu Or TypeOf ctl Is Label Then
ctl.Enabled = True
Else
ctl.Enabled = False
End If
If TypeOf ctl Is TextBox Or TypeOf ctl Is MaskEdBox _
Then ctl.BackColor = "&H8000000F"
Next ctl
mnuGuestReservationCheckIn.Enabled = False
mnuGuestReservationCheckout.Enabled = False
mnuGuestReservationCancel.Enabled = False
End Sub

Note: At the end of the Disable controls procedure those three lines will disable Check-In,
Check-Out and the Cancel Reservation menu Items.

4. The EnableControls procedure will enable all of the controls, including the menu
items, and returns the TextBox and MaskEdBox control’s Back Color to White. The
finished code should look like the following.

Private Sub EnableControls()


Dim ctl As Control
For Each ctl In Controls

ctl.Enabled = True

If TypeOf ctl Is TextBox Or TypeOf ctl Is


MaskEdBox _
Then ctl.BackColor = "&H80000005"
Next ctl
mnuGuestReservationCheckIn.Enabled = True
mnuGuestReservationCheckout.Enabled = True
mnuGuestReservationCancel.Enabled = True
End Sub

 To Call the DisableControls and EnableControls procedures.


1. Call the DisableControl procedure from the Form_Load event and the
cmdDone_Click event. (Below it is shown only the form load event)

Private Sub Form_Load()


DisableControls
End Sub

Dept. of Computer Application, SIT 11 | P a g e


Laboratory Manual MCA-E592B
2. From the mnuGuestReservationAdd_Click event, call the EnableControls procedure
and set the focus to the txtFirstName Text Box Control. The finished code should
look like the following:

Private Sub mnuGuestReservationAdd_Click()


EnableControls
txtFirstName.SetFocus
End Sub
3. Save and Test the application.

B. Field-Level Validation
In this exercise, you will use the IsNumeric function and the KeyPress event to
validate user input on the reservation form.

 Validating data entry on the Reservation Form.


1. In the KeyPress event of the FirstName, Last Name and Address text boxes, add
code that convers the text to uppercase. For example

Private Sub txtFirstName_KeyPress(KeyAscii As Integer)


KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub

Private Sub txtLastName_KeyPress(KeyAscii As Integer)


KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub

Private Sub txtAddress_KeyPress(KeyAscii As Integer)


KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub

2. Use the KeyPress evnet of the Phone Number text box to accept only numbers,
parenthesis(), hypens(-) and the Backspace characters. If incorrect values are
entered, beep and then display a message to the addinfo panel of the StatusBar
Control. The finished code should look like the following:

Private Sub txtPhone_KeyPress(KeyAscii As Integer)


'exit sub for allowable characters
If Chr(KeyAscii) = vbBack _
Or Chr(KeyAscii) = "-" _
Or Chr(KeyAscii) = "(" _
Or Chr(KeyAscii) = ")" Then Exit Sub
'check for alpha characters
If Not IsNumeric(Chr(KeyAscii)) Then
Beep
KeyAscii = 0
staAdditionalInfo.Panels("addinfo").Text = "Phone” _ &
“number must be numeric."
End If
End Sub

Dept. of Computer Application, SIT 12 | P a g e


Laboratory Manual MCA-E592B
3. On the txtNumPeople & txtNumDays text boxes, set the MaxLength property to 3.
(The max length property specifies the maximum number of characters that can be
entered. This value limits the users to a three-digit number.)

4. Using the KeyPress events of the txtNumPeople, txtNumDays and txtRate text
boxes, verify tha a numeric value is type. Be sure to allow the user to press the
BackSpace key. This example shows the completed txtNumPeople_KeyPress event.

Private Sub txtNumPeople_KeyPress(KeyAscii As Integer)


If Chr(KeyAscii) = vbBack Then Exit Sub
If Not IsNumeric(Chr(KeyAscii)) Then
Beep
KeyAscii = 0
staAdditionalInfo.Panels("addinfo").Text = "Value must be numeric."
End If
End Sub
Add similar code to the txtNumDays_KeyPress and txtRate_KeyPress event.

5. Save and test the application.

C. Using the Validate and LostFocus Events


In this exercise, you will use the Validation and LostFocus events to validate the data
entered into the reservation form.
 To use the Validation and LostFocus events
1. In the mskCheckIn_Validate event, add code to verify that a valid date has been
entered into the control. For example.

Private Sub mskCheckIn_Validate(Cancel As Boolean)


If Not IsDate(mskCheckIn.Text) Then
staAdditionalInfo.Panels("addinfo").Text = "Not a valid“ & _
date format (ex. '07-23-2000')"
Cancel = True
End If
End Sub

2. In the txtNumDays_LostFocus event, add the following code to calculate and format
the guest’s checkout date:

Private Sub txtNumDays_LostFocus()


If mskCheckIn.ClipText <> "" And txtNumDays.Text <> "" Then
txtCheckOut.Text = Format(DateAdd("d", _
Val(txtNumDays.Text), mskCheckIn.Text), "mm-dd-yyyy")
End If

End Sub

3. Set the Locked property of the Checkout date text box to true.
4. Save and test the application.

Dept. of Computer Application, SIT 13 | P a g e


Laboratory Manual MCA-E592B
D. Form-Level Validation

In this exercise, you will check that the required values were entered in to the form
before letting the user continue.

 To implement Form Level checking


1. Add code to the Done button so that it loops through all the controls and displays a
message box if the required fields do not have values. The following is the
completed code for the cmdDone_Click envent:
(Remember there is one line code you have entered in this event before. There you called
procedure DisableControls)

Private Sub cmdDone_Click()


Dim ctl As Control

For Each ctl In Controls


If TypeOf ctl Is TextBox Then
If ctl.Text = "" Then
MsgBox "All fields must be entered."
Exit Sub
End If
ElseIf TypeOf ctl Is MaskEdBox Then
If ctl.ClipText = "" Then
MsgBox "All fields must be entered."
Exit Sub
End If
ElseIf TypeOf ctl Is OptionButton Then
If ctl.Value = "" Then
MsgBox "Payment type is required."
Exit Sub
End If
End If
Next ctl
DisableControls
End Sub

2. Save and Text you application.

Note: After successfully completing these two examples you will be able to do different
types of validations, creating menus, adding ActiveX controls and others. In next lab
session you will get different exercise that has been covered in these two examples.

Dept. of Computer Application, SIT 14 | P a g e


Laboratory Manual MCA-E592B
Experiment Set-VI
(Continuation from Experiment Set- V)

Using the ADO Data Control


In this lab, you will add navigation capabilities to frmReservation. You will add the
ADO data control to frmReservation and connect to Microsoft Access’ Reservation
Database. The database’s Reservation table will be used as the data source for theADO
Data Control. You will bind controls on the frmReservation to continue to work with the
files you created in the previous lab.

Exercise 1: Connecting to a data source.

In this exercise, you will connect to a database using the ADO Data control’s
property pages.

 Creating Microsoft Access Database ‘Rsvn.mdb’


1. Open Microsoft Access
2. Select Black Database
3. Give name of the Database ‘Rsvn.mdb’ and save it into your VBPractice Folder.
4. From the Object List Click on Table and Double Click on the option “Create
Table in Design View”. It will open a new window where you have to create the
Table Reservation.

Figure: Creating Access Database.

Dept. of Computer Application, SIT 15 | P a g e


Laboratory Manual MCA-E592B
5. Now create the Table, Give the Field name and Data type according to the
diagram.

6. Now Save the File Give the Table Name Reservation.

Dept. of Computer Application, SIT 16 | P a g e


Laboratory Manual MCA-E592B
7. Now Double click on the table Reservation to enter some fictitious Data. (Sample
data is given in the Table)

8. Save the Table. Close the Database.

9. Or you can collect the database from the faculty in the lab and save it into your
VBPractice Folder.

10. Proceed to the next exercise if your database is ready. Database


Name is Rsvb.mdb and table name is Reservation.

Dept. of Computer Application, SIT 17 | P a g e


Laboratory Manual MCA-E592B
Reservation
PaymentTy
GuestID FirstName LastName Address Phone NumberOfPeople CheckInDate NumberOfDays Status RoomNumber Rate
pe
1 Sean Chai 507 - 20th Ave. E. (206) 555-9857 CREDIT 1 05-26-1999 5 PENDING A1 34.00
Apt. 2A CARD
2 Bruce Oberg 908 W. Capital Way (206) 555-9482 CHEQUE 2 04-08-1999 3 PENDING C22 55.00
3 Brian Fleming 722 Moss Bay Blvd. (206) 555-3412 CREDIT 2 05-25-1999 2 PENDING B8 34.00
CARD
4 David Campbell 4110 Old Redmond (206) 555-8122 CREDIT 2 01-08-2000 4 PENDING A7 55.00
Rd. CARD
5 Susan M. Tjarnberg 14 Garrett Hill (71) 555-4848 CHECK 3 11-20-1999 2 PENDING C3 55.00
6 Tom Johnston Coventry House (71) 555-7773 CREDIT 1 03-08-1999 1 PENDING C5 34.00
Miner Rd. CARD
7 John Tippett Edgeham Hollow (71) 555-5598 CASH 4 04-18-1999 4 PENDING B9 55.00
Winchester Way
8 Paul Borm 4726 - 11th Ave. (206) 555-1189 CREDIT 2 07-22-1999 3 PENDING A3 55.00
N.E. CARD
9 Scott Fallon 7 Houndstooth Rd. (71) 555-4444 CREDIT 2 09-18-1999 5 PENDING C10 55.00
CARD

Sample Data in the database

Dept. of Computer Application, SIT 18 | P a g e


Laboratory Manual MCA-E592B
 To add an ADO Data Control to frmReservation (Open you project HotelRes.VBP
from the folder D:\VBPracticeFolder)

1. Open the Components Dialog Box (press Ctrl+T) to add the Microsoft ADO Data
Control 6.0 (OLE DB) component to the toolbox.
2. Place an ADO Data Control on frmReservation as illustrate in the figure.

Figure: ADO Data Control placed on frmReservation

Dept. of Computer Application, SIT 19 | P a g e


Laboratory Manual MCA-E592B
 To Connect the ADO Data control to a data source

1. Set the ADO Data Control’s Name property to adcReservation, and the Caption
property to Reservation.

2. Use the ADO Data Control’s property pages (right click on the Adodc and click on
ADODC Property) to create a connection string using the Microsoft Jet OLE DB
provider to connect to the Rsvn.mdb database located in your VBPractice Folder.

Dept. of Computer Application, SIT 20 | P a g e


Laboratory Manual MCA-E592B
3. Click on ‘Build’ button. And from Data Link Property window Select Proverder as
‘Microsoft Jet 4.0 OLE DB Provider’ and Click on next.

4. Select the database Rsvn.mdb from your VBPractice folder.

Dept. of Computer Application, SIT 21 | P a g e


Laboratory Manual MCA-E592B
Dept. of Computer Application, SIT 22 | P a g e
Laboratory Manual MCA-E592B
5. Click on Advance Tab and select ReadWrite Check box. Click on OK and come out of
the Data Link Properties.

6. Set the RecodrdSource property of the ADO Data Control to use a command Type of
2-adCmdTable, and use Reservation as the Table or Stored Procedure Name, as
illustrated in Figure.

Dept. of Computer Application, SIT 23 | P a g e


Laboratory Manual MCA-E592B
Figure Setting RecordSoruce Property of adcReservation
7. Set the adcReservation control’s CursorType property to 3-adOpenStatic .
This will provide record browsing capability ot users without allowing them to add
change or delete records

 To bind controls to adcReservation


1. Using the adReservation ADO Data Control as the DataSource, set the DataField
properties for all of the bound controls on frmReservation. (Do not bind the
Payment Type option buttons. Thy will be bound programmatically at run time.)

2. Set the dataFormat property of mskChekIn ot Date as illustrated in Figure.

Figure: Formatting the mskCheckIn Control (DataFormat Property)

3. Type the following code statement before the End Sub statement in the
DisableControls Sub procedure

adcReservation.Enabled = True

The user will now be abel to use the ADO Data control to navigate between records
in the Reservation table.

4. To have the Payment Type option buttons programmatically display the correct type
of payment (Credit Card, Cheque or Cash) from adcReservation recordset, add the
following code to the adcReservatio_MoveComplete event. (Double click on the
adcReservation and Select MoveComplete event from the event list)

Dept. of Computer Application, SIT 24 | P a g e


Laboratory Manual MCA-E592B
Private Sub adcReservation_MoveComplete(ByVal adReason As
ADODB.EventReasonEnum, ByVal pError As ADODB.Error, adStatus As
ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset)

Dim strStatus As String


Dim dtCheckIn As Date
Dim intNumDays As Integer

'If no value has been entered into the recordset,


'continue without stopping application.

On Error Resume Next

'Check the payment type in the database and


'set the option button to correspond with the
'payment type value from the recordset.

strStatus = adcReservation.Recordset![PaymentType]
MsgBox strStatus
Select Case UCase(strStatus)
Case "CREDIT CARD"
grpPmtType(0).Value = True
Case "CHEQUE"
grpPmtType(1).Value = True
Case "CASH"
grpPmtType(2).Value = True
End Select

'set the dtCheckIn and intNumDays variables


'to the values from the ADO Data contrl's recordset

dtCheckIn = adcReservation.Recordset![CheckInDate]
intNumDays = adcReservation.Recordset![NumberOfDays]

'Calcuate the check out date and palce it itno


'the txtCheckOut control

txtCheckOut.Text= Format(DateAdd("d", intNumDays, _


dtCheckIn), "mm-dd-yyyy")

End Sub

5. Save and Test your application.

Dept. of Computer Application, SIT 25 | P a g e


Laboratory Manual MCA-E592B
Experiment Set-VII
(Continuation from Experiment Set- VI)
Using ActiveX Data Objects
In this lab you will write ADO code to create a Connection and Recordset objects. The
Recordset object will be created based on a SQL statement built from a Search form that you
will create.

Estimated lesson time: 20 Minutes

A. Creating ADO Objects

In this exercise you will declare Record set and Connection object variables. These
variables will be used in Exercise 2. You will also create a custom search form t allow users
to locate guest records.

 To declare a connection and Recordset object variable


1. Open your application HotelRes.VBP

2. Click on Project  Reference Set a reference to the Microsoft ActiveX Data


Objects 2.0. Object Library.

3. In the General Declaration section of frmReservation, type the following code:

Private cnReservation As ADODB.Connection


Private rsReservation As ADODB.Recordset.

 To implement a custom Search form


1. Create a new form and set the Name property to frmSearch.

2. Place three Label and TextBox Controls and one CommandButton on frmSearch,
and set the following properties.

Object Control Type Properties Value


Name lblLastName
Label1 Label
Caption Last Name
Name txtLastName
Text1 Text Box
Text <blank>
Name lblFirstName
Label2 Label
Caption First Name
Name txtFirstName
Text2 Text Box
Text <blank>
Name lblPhone
Label3 Label
Caption Phone
Name txtPhone
Text3 Text Box
Text <blank>
Name cmdSearch
Command1 Command Button
Caption &Search

Dept. of Computer Application, SIT 26 | P a g e


Laboratory Manual MCA-E592B
Figure Position of controls on frmSearch

3. In the Click event of cmdSearch, type the following code:

‘Hide the current form


Me.Hide

4. Save your work as frmSearch.frm

B. Writing ADO Code

In this exercise, you will write ADO code to query the Reservation table in the
Rsvn.mdb database. The rsReservation Reecordset object that you created in Exercise 1 will
be based on information that the user enters into the fields on frmSearch.

 To open the rsReservation recordset.

1. In the mnuCustSearch_Click event, type the following code

Private Sub mnuCustSearch_Click()

Dim SQL As String


Dim msgRetVal As Integer

frmSearch.Show vbModal

'create a SQL statement to search for a guest

SQL = "SELECT * FROM Reservation WHERE LastName LIKE '" &


_ frmSearch!txtLastName.Text & "%'" & _
" AND FirstName LIKE '" & _
frmSearch!txtFirstName.Text & "%'" & _
" AND Phone = '" & frmSearch.txtPhone.Text & "';"

Unload frmSearch
Set frmSearch = Nothing

Dept. of Computer Application, SIT 27 | P a g e


Laboratory Manual MCA-E592B
'create and open the connection
Set cnReservation = New ADODB.Connection

cnReservation.ConnectionString = _
"Provider = Microsoft.Jet.OLEDB.3.51;" & _
"Data Source=Rsvn.mdb"

cnReservation.Open

Set rsReservation = New ADODB.Recordset


'create and open the recordset
rsReservation.Open SQL, cnReservation, adOpenDynamic

'If no records found, then display a message


'and exit the sub
If rsReservation.BOF And rsReservation.EOF Then
MsgBox "Unable to locate guest."
Exit Sub
End If

'Place each field from the record into the fields on


frmReservation
txtFirstName.Text = rsReservation![FirstName]
txtLastName.Text = rsReservation![LastName]
txtAddress.Text = rsReservation![Address]
txtNumDays.Text = rsReservation![NumberOfDays]
txtNumPeople.Text = rsReservation![NumberOfPeople]
txtPhone.Text = rsReservation![Phone]
txtRate.Text = rsReservation![Rate]
txtRoomNumber.Text = rsReservation![RoomNumber]
mskCheckIn.Text = Format(rsReservation![CheckInDate], _
"mm-dd-yyyy")
txtCheckOut.Text = Format(DateAdd("d",
Val(txtNumDays.Text), mskCheckIn.Text), "mm-dd-yyyy")
Select Case rsReservation![PaymentType]
Case "CREDIT CARD"
grpPmtType(0).Value = True
Case "CHECK"
grpPmtType(1).Value = True
Case "CASH"
grpPmtType(2).Value = True
End Select
End Sub

Dept. of Computer Application, SIT 28 | P a g e


Laboratory Manual MCA-E592B
 To test your application
1. Start the application
2. The Reservation Form appears. On the Guest Menu click Searhc
3. The Search from appears
4. Type Fleming for last name. Brian for First Name and (206) 555-3412 (place a spce
after the close parenthesis) for phone number, as illustrated in Figure.

Figure: Entering Search Values


It will find the record in the Reservation table. The Recordset’s field values appear in the
controls on frmReservation, as illustrated in figure.

Figure: the frmReservation form containing the found record.

5. On the Run menu click End.

6. On the Visual Basic File menu click Save Project.

Dept. of Computer Application, SIT 29 | P a g e


Laboratory Manual MCA-E592B
Assignment-1
(Submission Date: Will be announced in the Classroom)
1. Develop a program that will accept a Temperature either in Fahrenheit or in
Centigrade Scale and convert it into Centigrade or into Fahrenheit accordingly. User
will choose this option from a option list. Your input form should be like this:

Point to be noted:
i) According to the user’s choice the Label with the text box will be changed.
(i.e. if he chooses Fahrenheit to Centigrade the first text box associated label will
be Fahrenheit and second text box associated label will be Centigrade)
ii) If user clicks on the close button in the form program will display a
message saying “Click on the Exit Button to exit”.

2. Develop a Program that will count the number of words written in a text box. Output
form of this program may like this. (Note the text box properties. It supports Multi
Line and Scroll Bars

Dept. of Computer Application, SIT 30 | P a g e


Laboratory Manual MCA-E592B
Develop a program that will accept Sentences and convert it into Upper Case/ Lower
Case/Sentence Case / Title Case/Toggle Case according to the user’s choice. Input form
may look like this:

3. Check the following input form

In this form user will enter a number in the 1st text box and get the changed number in
the text box2 according to chosen option from the List Box. Four options are available in the
list box
a. Reverse the Number  It will reverse the given number.
b. Sum of its Digits  it will find the sum the digits of the given number i.e. if
the given number is 532 out put will be 5+3+2 , 10
c. Sum of its Digits cube. d. Refresh  Clear the content of the text boxes.

Dept. of Computer Application, SIT 31 | P a g e


Laboratory Manual MCA-E592B
Assignment 2
(To be announced in the Classroom)

1. Develop a program that uses three Horizontal scroll bars to change the Red Green
Blue colors of two picture box. Each scroll bar has two events Change() and Scroll().
Change event will change the Picture box 2’s back color and Scroll event will change
the back color of Picture Box 1’s Back color according to these three scroll bar’s value.

Q2. Develop a Tic-Tac-Toe Game. Form lay out may be like this.

Q3. Develop an image browser with stretch option using a common Dialog Box
Q4. Create a Progress Bar using line drawing in Picture box , with the progress bar
exhibiting all the VIBGYOR Colours.
Dept. of Computer Application, SIT 32 | P a g e
Laboratory Manual MCA-E592B

You might also like