Вы находитесь на странице: 1из 66

1

Key Areas
Databases
 Introduction to Databases
 Concept of Keys
 Creating Databases (Microsoft Access)

Programming
 Database Connectivity
 Coding for different events

2
Databases

3
Databases
What is a
Database ?

“A database is a
set of data that
has a regular
structure and it
is well
organized”

4
Database Applications
( DBMS – Database Management Systems )

Examples for Database Applications

• MS Access
• MySQL
• Oracle
• PostgreSQL
• MariaDB

5
Concept of Keys
What is a KEY ?
A key is an attribute that is used to identify data

• Primary Key - key that uniquely identifies the data of


each row

• Foreign Key – Primary key of another table

• Composite Key – a key that has more than one


attribute
6
Database Example
EmpNo EmpName DeptNo
110 John 5
120 Peter 6
Primary Key
Primary Key

130 Anne 7
140 Bobby 5
150 Alex 5 DeptNo DeptName
5 Accounting
Foreign Key
6 Management
7 Security

7
Creating Databases
Design View

Database View

8
Saving Database

9
Saving Database

10
Saving Database


11
Programming

12
Database Connectivity

13
Database Connectivity (VB)
Setting the Database Connection

In Visual Basic Screen, go to

Project >
References >
Microsoft Data Access Object 3.6 Library >
OK

14
Database Connectivity (VB)

15
Programming
Database is located at …

So, the path of the Database is


C:\VB\students.mdb 16
Programming
In General Declaration area, declare two(2) variables for Database & the Query(Record Set)

Code – General Dec


Dim db As Database
Dim rs1 As Recordset

If you need more Queries(record sets) declare


variables for them as you need

17
Programming
Identify the Database & the Record Set to VB.
Write this code at Form Load Event DB location(path)

Code for Form_Load() SQL Statement


Set db = OpenDatabase("C:\VB\students.mdb") SQL – Structured Query Language
Set rs = db.OpenRecordset("select * from subjects")
End Sub
18
Programming
Form Activate Event

Code for Form_Activate Event


TXTSubID.Text = rs("subjectid") Fields of the table
TXTSubName.Text = rs("subject")
CMBField.Text = rs("field")
CMBCatog.Text = rs("category")
CMBPoints.Text = rs("points")
19
Programming
Clear All/New Button

Private Sub CMDNew_Click()


TXTSubID.Text = ""
TXTSubName.Text = ""
CMBField.Text = ""
CMBCatog.Text = ""
CMBPoints.Text = ""
End Sub

20
Programming
Close/Exit Button

Private Sub CMDclose_Click()


Dim x As String
x = MsgBox("Are you sure ?", vbExclamation + vbYesNo, "Notification")
If x = vbYes Then
End
Else
CMDnew.SetFocus
End If
End Sub

21
Programming
Form Controller Buttons

CMDF_Click() CMDL_Click()
rs.MoveFirst rs.MoveLast
TXTSubID.Text = rs("subjectid") TXTSubID.Text = rs("subjectid")
TXTSubName.Text = rs("subject") TXTSubName.Text = rs("subject")
CMBField.Text = rs("field") CMBField.Text = rs("field")
CMBCatog.Text = rs("category") CMBCatog.Text = rs("category")
CMBPoints.Text = rs("points") CMBPoints.Text = rs("points")
End Sub End Sub
22
Programming
Form Controller Buttons

CMDP_Click() CMDN_Click()
rs.MovePrevious rs.MoveNext
If rs.BOF = True Then If rs.EOF = True Then
MsgBox "Top Reached" MsgBox "End of file“
rs.MoveFirst Rs.MoveLast
Else Else
TXTSubID.Text = rs("subjectid") TXTSubID.Text = rs("subjectid")
TXTSubName.Text = rs("subject") TXTSubName.Text = rs("subject")
CMBField.Text = rs("field") CMBField.Text = rs("field")
CMBCatog.Text = rs("category") CMBCatog.Text = rs("category")
CMBPoints.Text = rs("points") CMBPoints.Text = rs("points")
End If End If
End Sub 23
Programming
Delete Button

CMDDel_Click()
Dim RV As String
RV = MsgBox("Are you sure want to delete ? ", vbYesNo + vbCritical, "Confirm")
If RV = vbYes Then
rs.Delete
Form1.Refresh
MsgBox "Deleted ! "
Else
CMDNew.SetFocus
End If
End Sub

24
Programming
Edit Button

CMDEdit_Click()
rs.Edit
rs("mano") = TXTMno.Text
rs("repstat") = TXTRepStat.Text
rs("repcomp") = TXTRepCom.Text
rs.Update
End Sub
25
Programming
Save Button CMDSave_Click()
rs.FindFirst "mano='" & TXTMno.Text & "'"
If rs.NoMatch = False Then
MsgBox "Duplicate Records has been detected"
TXTMno.Text = ""
Else
If TXTMno.Text = "" Then
MsgBox "Check Machine No "
Else
rs.AddNew
rs("mano") = TXTMno.Text
rs("repstat") = TXTRepStat.Text
rs("repcomp") = TXTRepCom.Text
rs.Update
MsgBox " Successfully Saved", vbInformation
End If
End If
End Sub 26
Programming
Loading data into ComboBoxes
There are two ways to do this
1.

Code
CMBField.AddItem ("Electrical")
CMBField.AddItem ("Civil")
CMBField.AddItem ("Mechanical")

(USE THIS FOR THE PAPER)

27
Programming
Loading data into ComboBoxes
2.

Press CTRL+ENTER to get the next line


28
Programming
ComboBoxes [Special]

How to fill the list of a Combo Box using


The records in the database already

Code
rs.MoveLast
Dim nre As Integer
nre = rs.RecordCount
rs.MoveFirst
For i = 1 To nre
Combo1.AddItem rs("regno")
rs.MoveNext
Next 29
KeyPress > SetFocus Event

TXTN1_KeyPress
If KeyAscii = 13 Then
TXTN2.SetFocus
End If

30
Example No.1

31
TXTName TXTage TXTpoints TXTregno

CMDNew

CMDSave

CMDEdit

CMDDel

CMDExit

CMDFirst CMDPre CMDNext CMDLast


32
The description of the table will be given
33
From Where Should I Start this ?
34
CMDNew_Click
TXTName.Text = ""
TXTage.Text = ""
TXTpoints.Text = ""
TXTregno.Text = ""
End Sub
CMDExit_Click
Dim x As String
x = MsgBox("Are you sure ?", vbExclamation + vbYesNo, "Notification")
If x = vbYes Then
End
Else
CMDnew.SetFocus
End If
End Sub
35
Link the DLL file

In Visual Basic Screen, go to

Project >
References >
Microsoft Data Access Object 3.6 Library >
OK

36
General Declaration
Dim db As Database
Dim rs As Recordset

Form_Load()
Set db = OpenDatabase("C:\VB\students.mdb")
Set rs = db.OpenRecordset("select * from subjects")
End Sub

Form_Activate
TXTName.Text = rs("name")
TXTage.Text = rs("age")
TXTpoints.Text = rs("points")
TXTregno.Text = rs("regno")
37
CMDFirst_Click CMDLast_Click
rs.MoveFirst rs.MoveLast
TXTName.Text = rs("name") TXTName.Text = rs("name")
TXTage.Text = rs("age") TXTage.Text = rs("age")
TXTpoints.Text = rs("points") TXTpoints.Text = rs("points")
TXTregno.Text = rs("regno") TXTregno.Text = rs("regno")
End Sub End Sub

CMDNext_Click CMDPre_Click
rs.MoveNext rs.MovePrevious
If rs.EOF = True Then If rs.BOF = True Then
MsgBox "end" MsgBox "start is this"
rs.MoveLast rs.MoveFirst
Else Else
TXTName.Text = rs("name") TXTName.Text = rs("name")
TXTage.Text = rs("age") TXTage.Text = rs("age")
TXTpoints.Text = rs("points") TXTpoints.Text = rs("points")
TXTregno.Text = rs("regno") TXTregno.Text = rs("regno")
End If End If
End Sub End Sub 38
CMDEdit_Click
rs.Edit
rs("name") = TXTName.Text
rs("age") = TXTage.Text
rs("points") = TXTpoints.Text
rs("regno") = TXTregno.Text
rs.Update
End Sub

CMDDel_Click
Dim RD As String
RD = MsgBox("confirm action", vbQuestion + vbYesNo, "condifm")
If RD = vbYes Then
rs.Delete
Form1.Refresh
Else
TXTName.SetFocus
End If
End Sub

39
CMDSave_Click
rs.FindFirst "RegNo = ‘ “ & TXTregno.Text & “ ‘ "
If rs.NoMatch = False Then
MsgBox "Duplicate Entry"
Else
rs.AddNew
rs("name") = TXTName.Text
rs("age") = TXTage.Text
rs("points") = TXTpoints.Text
rs("regno") = TXTregno.Text
rs.Update
End If
End Sub

40
41
Example No.2

42
TXTSubID TXTSubName CMBField CMBCatog CMBPoints

CMDNew

CMDSave

CMDEdit

CMDDelete

CMDFirst CMDPre CMDNext CMDLast


43
CMDNew_Click()
TXTSubID.Text = ""
TXTSubName.Text = ""
CMBField.Text = ""
CMBCatog.Text = ""
CMBPoints.Text = ""
End Sub

CMDExit_Click
Dim x As String
x = MsgBox("Are you sure ?", vbExclamation + vbYesNo, "Notification")
If x = vbYes Then
End
Else
CMDnew.SetFocus
End If
End Sub
44
General Dec
Form_Activate()
Dim db As Database
TXTSubID.Text = rs("subjectid")
Dim rs As Recordset
TXTSubName.Text = rs("subject")
CMBField.Text = rs("field")
CMBCatog.Text = rs("category")
CMBPoints.Text = rs("points")
Form_Load()
Set db = OpenDatabase("students.mdb")
CMBField.AddItem ("Electrical")
Set rs = db.OpenRecordset("select * from subjects")
CMBField.AddItem ("Civil")
End Sub
CMBField.AddItem ("Mechanical")

CMBCatog.AddItem ("GPA")
CMBCatog.AddItem ("Non-GPA")

CMBPoints.AddItem ("1")
CMBPoints.AddItem ("2")
CMBPoints.AddItem ("3")
CMBPoints.AddItem ("4")
CMBPoints.AddItem ("5")
End Sub

45
CMDF_Click() CMDL_Click()
rs.MoveFirst rs.MoveLast
TXTSubID.Text = rs("subjectid") TXTSubID.Text = rs("subjectid")
TXTSubName.Text = rs("subject") TXTSubName.Text = rs("subject")
CMBField.Text = rs("field") CMBField.Text = rs("field")
CMBCatog.Text = rs("category") CMBCatog.Text = rs("category")
CMBPoints.Text = rs("points") CMBPoints.Text = rs("points")
End Sub End Sub

CMDN_Click() CMDP_Click()
rs.MoveNext rs.MovePrevious
If rs.EOF = True Then If rs.BOF = True Then
MsgBox "End of file“ MsgBox "Top Reached"
Rs.moveLast rs.MoveFirst
Else Else
TXTSubID.Text = rs("subjectid") TXTSubID.Text = rs("subjectid")
TXTSubName.Text = rs("subject") TXTSubName.Text = rs("subject")
CMBField.Text = rs("field") CMBField.Text = rs("field")
CMBCatog.Text = rs("category") CMBCatog.Text = rs("category")
CMBPoints.Text = rs("points") CMBPoints.Text = rs("points")
End If End If
End Sub End Sub
46
CMDEdit_Click()
rs.Edit
rs("subject") = TXTSubName.Text
rs("field") = CMBField.Text
rs("category") = CMBCatog.Text
rs("points") = CMBPoints.Text
rs.Update
End Sub

CMDDelete_Click()
Dim RD As String
RD = MsgBox("Confirm ? ", vbQuestion +
vbYesNo, "!")
If RD = vbYes Then
rs.Delete
Form1.Refresh
Else
CMDNew.SetFocus
End If
End Sub

47
CMDSave_Click()
rs.FindFirst "subject='" & TXTSubName.Text & "'"
If rs.NoMatch = False Then
MsgBox "Duplicate Detected"
Else
rs.AddNew
rs("subject") = TXTSubName.Text
rs("field") = CMBField.Text
rs("category") = CMBCatog.Text
rs("points") = CMBPoints.Text
rs.Update
End If

48
49
Lets Turn into
2012 Past Paper

50
2012 Question No.2

TXTIndex

TXTPure LBLPure

TXTApp LBLApp

TXTChem LBLChem

TXTPhy LBLPhy

LBLTotal CMDResult

CMDNew
CMDClose
LBLAvg
51
2012 Question No.2 (Cont’d)
CMDClose_Click()
General Dec Dim X As String
Public a, b, c, d, tot, avg As Integer X = MsgBox("Are you sure want to exit ?",
vbCritical + vbYesNo, "Confirm")
If X = vbYes Then
CMDNew_Click()
End
TXTIndex.Text = ""
Else
TXTPure.Text = ""
CMDNew.SetFocus
TXTApp.Text = ""
End If
TXTChem.Text = ""
End Sub
TXTPhy.Text = ""

LBLPure.Caption = ""
LBLApp.Caption = ""
LBLChem.Caption = ""
LBLPhy.Caption = ""

LBLTotal.Caption = ""
LBLAvg.Caption = ""
End Sub

52
2012 Question No.2 (Cont’d)
TXTPure_LostFocus()
a = Val(TXTPure.Text) If a <= 59 Then
If a > 100 Then LBLPure.Caption = "C"
MsgBox "Invalid Value", vbInformation + vbOKOnly, "?" Else
TXTPure.Text = "" If a <= 74 Then
End If LBLPure.Caption = "B"
Else
If a <= 25 Then LBLPure.Caption = "A"
LBLPure.Caption = "E" End If
Else End If
If a <= 35 Then End If
LBLPure.Caption = "F" End If
Else End If
If a <= 49 Then End Sub
LBLPure.Caption = "S"
Else

53
2012 Question No.2 (Cont’d)
TXTApp_LostFocus()
b = Val(TXTApp.Text)
If b > 100 Then
MsgBox "Invalid Value", vbInformation + vbOKOnly, "?"
TXTApp.Text = "" If b <= 59 Then
End If LBLApp.Caption = "C"
Else
If b <= 25 Then If b <= 74 Then
LBLApp.Caption = "E" LBLApp.Caption = "B"
Else Else
If b <= 35 Then LBLApp.Caption = "A"
LBLApp.Caption = "F" End If
Else End If
If b <= 49 Then End If
LBLApp.Caption = "S" End If
Else End If

End Sub

54
2012 Question No.2 (Cont’d)
Repeat the same steps for
c = val(txtchem.text)

&

d=val(txtphy.text)

55
2012 Question No.2 (Cont’d)

CMDResult_Click() TXTApp_KeyPress
CMDResult_KeyPress
tot = a + b + c + d If KeyAscii = 13
If KeyAscii = 13 Then
avg = tot / 4 Then
CMDNew.SetFocus
LBLTotal.Caption = tot TXTChem.SetFocus
End If
LBLAvg.Caption = avg End If
End Sub
End Sub End Sub

TXTIndex_KeyPress TXTChem_KeyPress
If KeyAscii = 13 Then If KeyAscii = 13 Then
TXTPure.SetFocus TXTPhy.SetFocus
End If End If
End Sub End Sub

TXTPure_KeyPress TXTPhy_KeyPress
If KeyAscii = 13 Then If KeyAscii = 13 Then
TXTApp.SetFocus CMDResult.SetFocus
End If End If
End Sub End Sub

56
2012 Question No.3

TXTMNo

TXTMonth
TXTYear

TXTPre
TXTCur

CMDClose

CMDNew CMDSave CMDedit CMDDelete

CMDF CMDPre CMDNext CMDL


57
2012 Question No.3 (Cont’d)

CMDNew_Click()
TXTMNo.Text = ""
TXTYear.Text = ""
TXTMonth.Text = ""
TXTCur.Text = ""
TXTPre.Text = ""
End Sub

CMDClose_Click()
Dim x As String
x = MsgBox("Are You sure want to exit ?", vbCritical + vbYesNo, "Confirm")
If x = vbYes Then
End
Else
CMDNew.SetFocus
End If
End Sub

58
2012 Question No.3 (Cont’d)

General Dec
Dim db As Database
Dim rs As Recordset

Form_Load()
Set db = OpenDatabase("electricity.mdb")
Set rs = db.OpenRecordset("SELECT * FROM
TBLMETER")
End Sub

Form_Activate()
TXTMNo.Text = rs("MeterNo")
TXTYear.Text = rs("BYear")
TXTMonth.Text = rs("BMonth")
TXTPre.Text = rs("PRead")
TXTCur.Text = rs("CRead")
End Sub
59
2012 Question No.3 (Cont’d)

CMDF_Click() CMDL_Click()
rs.MoveFirst rs.MoveLast
TXTMNo.Text = rs("MeterNo") TXTMNo.Text = rs("MeterNo")
TXTYear.Text = rs("BYear") TXTYear.Text = rs("BYear")
TXTMonth.Text = rs("BMonth") TXTMonth.Text = rs("BMonth")
TXTPre.Text = rs("PRead") TXTPre.Text = rs("PRead")
TXTCur.Text = rs("CRead") TXTCur.Text = rs("CRead")
End Sub End Sub
CMDP_Click() CMDN_Click()
rs.MovePrevious rs.MoveNext
If rs.BOF Then If rs.EOF Then
MsgBox "First Record is reached" MsgBox "Last record is reached"
rs.MoveFirst rs.MoveLast
Else Else
TXTMNo.Text = rs("MeterNo") TXTMNo.Text = rs("MeterNo")
TXTYear.Text = rs("BYear") TXTYear.Text = rs("BYear")
TXTMonth.Text = rs("BMonth") TXTMonth.Text = rs("BMonth")
TXTPre.Text = rs("PRead") TXTPre.Text = rs("PRead")
TXTCur.Text = rs("CRead") TXTCur.Text = rs("CRead")
End If End If
End Sub End Sub 60
2012 Question No.3 (Cont’d)

CMDDel_Click()
Dim RV As String
RV = MsgBox("Are You sure want to delete this record?", vbCritical + vbYesNo,
"Confirm")
If RV = vbYes Then
rs.Delete
Form1.Refresh
Else
CMDNew.SetFocus
End If CMDEdit_Click()
End Sub rs.Edit
rs("MeterNo") = TXTMNo.Text
rs("BYear") = TXTYear.Text
rs("BMonth") = TXTMonth.Text
rs("PRead") = TXTPre.Text
rs("CRead") = TXTCur.Text
rs.Update
MsgBox "Changes has been Saved Successfully",
vbInformation, "Info"
End Sub
61
2012 Question No.3 (Cont’d)

CMDSave_Click()
rs.FindFirst "MeterNo= ' " & TXTMNo.Text & "'"
If rs.NoMatch = False Then
MsgBox "Duplicate Error has been detected", vbInformation, "Info"
Else
rs.AddNew
rs("MeterNo") = TXTMNo.Text
rs("BYear") = TXTYear.Text
rs("BMonth") = TXTMonth.Text
rs("PRead") = TXTPre.Text
rs("CRead") = TXTCur.Text
rs.Update
MsgBox "Record saved successfully", vbInformation, " !"
End If
End Sub

62
2012 Question No.1

a) Strucured Programming
Structured programming is a program written with only the structured programming
constructions: (1) sequence, (2) repetition, and (3) selection.
Click Here for More Info
Add flour.
Add salt.
Add yeast.
Mix.
Add water.
Bake.
b) Form Caption

63
2012 Question No.1 (Cont’d)

c) Labels

d) Dim Fname,Lname as String


Dim Age,Trate as Integer
Dim Status as Boolean

e) ^$&#*$*%

f) Mnu

64
2012 Question No.1 (Cont’d)

g)

h) One

i) To get only the integer value ( or Data Validation )

j) A function returns a value whereas a subroutine does not. A function should not
change the values of actual arguments whereas a subroutine could change them 65
THANK YOU
w w w. n i s a n k a . i n f o

www.eDealsColombo.webs.com
66

Вам также может понравиться