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

University of Management and Economics UME Visual Basic 6.

0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

Visual Basic 6.0
Database Application
1. Introduction:
..... Database [.:. Application . . Business logic Database [....
._... DBMS . Application . Development tools . .. VB, VB2005, Java
..........,.. Development tools ......... Database ... Data
Access. . ... Data Access .. Tool :.... Objects ... Database Data Access
.
DAO (Data Access Object)
RDO(Remote Data Access Object)
ADO(ActiveX Data Object)
ADO.NET: The new version of ADO
OO4O(Oracle Object for OLE)
JDBC(Java Database Connectivity)
..
.....[[ ..... Microsoft Visual Basic 6 and Ms Access .......,:. DAO .
ADO
2. DAO (Data Access Object)
DAO . Library (.Components) [..... Connect . Application . Database



DAO ..:[ .. . . Application Database . . Database
Application _
.......[[ DAO ........... . Project Menu .[. .
References . Microsoft Office 12.0 Access Database Engine Object Library











Application

DAO

Database
University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

Project Menu References Microsoft Office 12.0 Access Database Engine
Object Library







2.1. ::--:-- Database File (Microsoft Access File)
Syntax.







...
Option Explicit
Dim DB As DAO.Database

Private Sub Form_Load()
Set DB = DBEngine.OpenDatabase("D:\Employee.accdb", False, False)
End Sub

Syntax.





...
Option Explicit
Dim DB As DAO.Database


Dim DBVariable As DAO.Database
Set DBVariable = DBEngine.OpenDatabase (DBName,[Options], [Read-Only])


Dim DBVariable As DAO.Database
Set DBVariable = DAO.OpenDatabase (DBName,[Options],[Read-Only])

University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

Private Sub Form_Load()
Set DB = DAO.OpenDatabase("D:\Employee.accdb", False, False)
End Sub

-:::---ua- Syntax -:-:u


u-:u::--- Syntax

a:-:e
DBVariable
... Database Object :..... Database File :.....
.. .
DBName
. String :..... Path .... Microsoft Jet Database File .
Data Source Name (DSN) ODBC Data Source
Option
. Optional Argument :..[.... :... Parameter .:...
True False
True : .... Microsoft Jet Database .. .. Exclusive
Mode (Only one user can access) .....
......,...j.. Database File .. ...
...[. Network
False (Default): .... Microsoft Jet Database
.... Shared Mode(Multiple users can access)

Read-Only
True : .... Database File .[....[. :..
. :. ..: .. Object :... . Database File
._
False (Default): .... Database File .[.
:....: .. Object :.... Database File
2.2 ::--:-- Microsoft Access File :cu-:-::- Password
Syntax.




Dim DBVariable As DAO.Database
Set DBVariable = DBEngine.OpenDatabase (DBName, [Options],
[ReadOnly],[Connection])

University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object


Syntax.







Microsoft Access File :.. Password ..[ [Connection] :... Optional
Argument ..j. ... .[. Microsoft Access .... Password .....
[. ...:. Connection Parameter .... Password ... .. .... . Error
. .._.
...
Option Explicit
Dim DB As DAO.Database
......................................................................................................................................................................................................
Private Sub Form_Load()
Set DB = DAO.OpenDatabase("d:\Employee.accdb", False, False, ;pwd=123)
End Sub
......................................................................................................................................................................................................
2.3 -:::--:u RecordSet Object :-u DAO
RecordSet Object .[j... Table .... (Virtual Table) :....,.
.. . Memory ..... ....._.. Table .. . RecordSet Object
......... RecordSet Object . .[.:....[...
[.... RecordSet Object . .[. Set RecordSet Object .........
Table . Query.
Syntax:





...
Option Explicit
Dim DB As DAO.Database
Dim Rs As DAO.RecordSet

..
Private Sub Form_Load()

Dim DBVariable As DAO.Database
Set DBVariable = DAO.OpenDatabase (DBName,[Options],[Read-Only],
[Connection])


Dim RS As DAO.RecordSet
Set RS = DBVariable.OpenRecordSet(Name As String,[Type],
[Options], [LockEdits] ) As RecordSet

University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

Set DB = DAO.OpenDatabase("D:\Employee.accdb", False, False )
Set Rs = DB.OpenRecordSet(tblEmployee, dbOpenDynaset)
End Sub
..

-:::---ua- Syntax :--:::- RecordSet

Argument

a:-:e
RS
.... RecordSet Object :........
DBVariable
. Database Object :...... .....

Name
. String Parameter :..... Table .... Query .
SQL Statement

[Type]
. Optional Argument .[..,. ..[. RecordSet :.....
.. .EdlBiBNnalMGit.....[
[Option]
. Optional Argument :...[..,.. . ....
[LockEdits]
. Optional Argument :...[[ Lock RecordSet ....:..
User .. .... .. Table :.

[Type] Argument:



dbOpenTable
....... Table
.... : . . ..
...[. SQL Statement Query ._





dbOpenDynaset
..... Virtual Table
.. .. : . . ..
...[. SQL Statement Query
:. . Table . Users ..,...j.........._..
RecordSet :..... ... :..... ... _
University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

........._..:..:[[ ...._



dbOpenDynamic
..... Virtual Table
.. .. : . . ..
...[. SQL Statement Query
: . ... .... Table . Users ..,...j.
........._.. . .. .: . .
...


dbOpenSnapshot
. RecordSet ..[.:..........._.
. . :.... Table .. User ..,...j.


dbOpenForwardOnly
.... RecordSet ..[.....:....
. . RecordSet .. . :....... : . .
...._


[LockEdits] Argument:


dbReadOnly
.. Lock .... Record .....:.. .
.....[.:.... RecordSet :....




dbPessimistic
..... .. Lock :......:.. User1 . RecordSet
.. ,:.. Record . ....... User2 . RecordSet
.. ,:.. Record ..:. ..... User2 :....:..[..
...:._ [. User1 ..:.: Record .. (.[ Edit
Method) User2 .:. .[.:. User1 : ..
University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object




dbOptimistic
....:.. User1 . RecordSet .. ,:.. Record .
....... User2 . RecordSet .. ,:.. Record
..:. ..... Users . ... :.. :.. Record .....

2.4 -::--:ua--e:a:-- Table :c:e:- RecordSet
.. ,..... . Table .. RecordSet ...[..[ Syntax . ...[.
Syntax a








Syntax au










Syntax a-










... . .... Database .. . D:\Employee.accdb . Table
... tblEmployee :...[.......[.

RecordSetObject.AddNew
RecordSetObject(index) = Value1
..
RecordSetObject(indexN) = ValueN
RecordSetObject.Update

RecordSetObject.AddNew
RecordSetObject(FieldName1) = Value1
..
RecordSetObject(FieldNameN) = ValueN
RecordSetObject.Update


RecordSetObject.AddNew
RecordSetObject!FieldName1 = Value1
..
RecordSetObject!FieldNameN = ValueN
RecordSetObject.Update

University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object










Y Source Code .
Option Explicit
Dim DB As DAO.Database
Dim Rs As DAO.RecordSet

..
Private Sub Form_Load()
Set DB = DAO.OpenDatabase("D:\Employee.accdb", False, False)
Set Rs = DB.OpenRecordSet (tblEmployee, dbOpenDynaset)
End Sub
..
Private Sub cmdAddNew_Click()
Rs.AddNew
Rs(0) = txtEmpID
Rs(1) = txtName
Rs(2) = txtSex
Rs(3) = txtPhone
Rs(4) = txtAddress
Rs.Update
End Sub
..

Private Sub cmdAddNew_Click()
Rs.AddNew
Rs(EmpID) = txtEmpID
Rs(EmpName) = txtName
Rs(Sex) = txtSex
Rs(Phone) = txtPhone
Rs(Address) = txtAddress
Rs.Update
End Sub
..

Private Sub cmdAddNew_Click()
Rs.AddNew
Rs!EmpID = txtEmpID

University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

Rs!EmpName = txtName
Rs!Sex = txtSex
Rs!Phone = txtPhone
Rs!Address = txtAddress
Rs.Update
End Sub
..

.......... (Sub Procedure) . ...[.
Sub AddToRS()
Rs(0) = txtEmpID
Rs(1) = txtName
Rs(2) = txtSex
Rs(3) = txtPhone
Rs(4) = txtAddress
End Sub
....,.. Sub Procedure ... .[. cmdAddNew_Click ....[.
..
Private Sub cmdAddNew_Click()
Rs.AddNew
Call AddToRS
Rs.Update
End Sub
..

2.5 -::a:-a--e- Table :c:e:- RecordSet
Syntax a






Syntax au

















Variable1 = RecordSetObject(index)
..
VariableN = RecordSetObject(indexN)



Variable1= RecordSetObject(FieldName1)

VariableN = RecordSetObject(FieldNameN)

University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

Syntax a-





...
..
Private Sub ReadToTextBox_Click()
txtEmpID = Rs(0)
txtName = Rs(1)
txtSex = Rs(2)
txtPhone = Rs(3)
txtAddress = Rs(4)
End Sub
..
.......... (Sub Procedure) . ...[.
Sub ReadFromRS()
txtEmpID = Rs(0)
txtName = Rs(1)
txtSex = Rs(2)
txtPhone = Rs(3)
txtAddress= Rs(4)
End Sub
....,.. Sub Procedure ....[ . cmdReadToTextBox_Click ....[.
..
Private Sub cmdReadToTextBox_Click()
Call ReadFromRS
End Sub
..2
2.6 -:::- RecordSetObject.BOF Property
BOF (Begin of File) . Property :...[[... Table :......_.
. RecordSet . .. [.... BOF .:.. False
. _. BOF . :.. True

Rs.BOF


Rs.MoveFirst

Rs.MoveNext

..

Rs.MovePrevious

Rs.MoveLast

Rs.EOF


Variable1= RecordSetObject!FieldName1

VariableN = RecordSetObject!FieldNameN

University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

...
..
Private Sub ReadToTextBox_Click()
If Rs.BOF = False Then
Call ReadFromRS
Else
MsgBox No data in your table!
End If
End Sub
..
2.7. -:::- RecordSetObject.MoveFirst
. Method :...[[ . Cursor .... . Record. RecordSet .
Default .. .. Record .. MoveFirst
...
Private Sub cmdFirst_Click()
If Rs.BOF = True Then
MsgBox "No Data in table!", vbInformation
Else
Rs.MoveFirst
Call ReadFromRS
End If
End Sub
2.8. -:::- RecordSetObject.MoveNext
. Method :...[[ . Cursor .... . Record ...... . Current Record
... Record
...
Private Sub cmdNext_Click()
If Rs.BOF = True Then
MsgBox "No data in table!", vbInformation
Else
Rs.MoveNext
If Rs.EOF = True Then
Rs.MoveLast
MsgBox "This is the Last Record!", vbInformation
Else
Call ReadFromRS
End If
End If
End Sub
University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

2.9. -:::- RecordSetObject.MoveLast
. Method :...[[ . Cursor ..... Record ...[..: RecordSet
...
Private Sub cmdLast_Click()
If Rs.BOF = True Then
MsgBox "No data in table!", vbInformation
Else
Rs.MoveLast
Call ReadFromRS
End If
End Sub
2.10. -:::- RecordSetObject.MovePrevious
. Method :...[[ . Cursor .... . Record ...... . Current Record
.[_. Record
...
Private Sub cmdPrevious_Click()
If Rs.BOF = True Then
MsgBox "No Data in Your Table!", vbInformation
Else
Rs.MovePrevious
If Rs.BOF = True Then
Rs.MoveFirst
MsgBox "This is the First Record!", vbInformation
Else
Call ReadFromRS
End If
End If
End Sub
2.11. -:::- RecordSetObject.RecordCount
. Property :...[[. Record :... . RecordSet
...
Private Sub cmdRecordCount_Click()
If Rs.BOF = True Then
MsgBox No data in your table!
Else
Rs.MoveLast
MsgBox Rs.RecordCount & Records
End If
University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

End Sub
uacu




2.12. -:::- RecordSetObject.EOF
. Property :...[[[........ Cursor .....: Record .
. [. Property .... ..:. True Cursor .....: Record .
. _.....:. False Cursor ......: Record ..
.... .... ListView ... ListView1 ....... Click ... ....
cmdShowInList_Click ...... RecordSet ....... . ListView1

Y Source Code .

Option Explicit
.
Sub AddColumnHeader(Lvw As Object, ParamArray Col())
Dim i As Integer
Lvw.ColumnHeaders.Clear
For i = 0 To UBound(Col)
Lvw.ColumnHeaders.Add , , Col(i)
Next i
Lvw.View = lvwReport
Lvw.GridLines = True
Lvw.FullRowSelect = True
Lvw.MultiSelect = True
End Sub
.
Sub AddDataToLvw(Lvw As Object, ParamArray Data())
Dim i As Integer
Set LItem = Lvw.ListItems.Add(, , Data(0))
For i = 1 To UBound(Data)
LItem.SubItems(i) = Data(i)
Next i
End Sub
.
Private Sub cmdShowInList_Click()
If Rs.BOF = True Then

University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

MsgBox "No data in your table!", vbInformation
Else
Call AddColumnHeader(ListView1,Rs(0).Name,Rs(1).Name,Rs(2).Name,
Rs(3).Name, Rs(4).Name)
Rs.MoveFirst
Do While Not Rs.EOF
Call AddDataToLvw (ListView1, Rs(0), Rs(1), Rs(2), Rs(3), Rs(4))
Rs.MoveNext
Loop
End If
End Sub
.
uacu




2.13. -:::- RecordSetObject.AbsolutePosition
. Property :............ . Record :..... .......
RecordSet .Property .....[. .[.:.....RecordSet ... dbOpenDynaset
Type .... Property .....:.[. RecordSet ....... Record.
...
Private Sub cmdNext_Click()
If Rs.BOF = True Then
MsgBox "No data in Table!", vbInformation
Else
Rs.MoveNext
If Rs.EOF = True Then
Rs.MoveLast
MsgBox "This is the Last Record!", vbInformation
Else
Call ReadFromRS
MsgBox Records & Rs.AbsolutePosition + 1
End If
End If
End Sub



University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

2.14. -:::- RecordSetObject.Close
. Method .[ [. RecordSet :......
Syntax:





.... Rs.Close

rrrhsss


























RecordSetObject.Close


University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

3.ADODB
ADODB . Library ..,... Microsoft .... Developer .. ..
Access Database ADO .. Database Access components ... .. Microsoft
.... Microsoft ... Visual Basic 6 .. ..... ADO:
- Using OLE DB Technology(Object Linking and Embedding, Database)
- . Distribute .. Internet ...... ADO ..] Develop Web
Application .. ASP
- Support Clients/Server . DAO and Support Distributed Database
Object and structure of ADODB










- Connection: . Object ..]. . Database .... ......
... ADODB . Database .. .. . Database ....
Connection Object .. ADODB
- RecordSet: . Records ........ table,views
-Command . Object . Parameters ..] Execute Server subprogram ..
Stored procedure or functions
Note:
..]. ADODB ... Reference Microsoft Active Data Objects 2.6 Library


3.1 Connection
..] Connect . Database ...
.. Object Connection
Dim Cn as ADODB.Connection
Connection Connection
Command
RecordSet
Field
Field
University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

Set Cn=new ADODB.Connection
. Connection ..] . Database . .. Connection
Cn.Open "Connection String"
Connection ..... Database ..... ....
ConnStr="Privider=OLE Provider;[Data Source="path/database name"]
OLE Privider ... Database provider ....... ADO..
DBMS Provider
Ms Access 2007 microsoft.ACE.oledb.12.0

Data source: . String .... DBMS .
Access: Path\filename.mdb
... Connect . Access Database

Dim cn as ADODB.Connection
Set cn=new ADODB.Connection
cn.Open "Provider=microsoft.ACE.oledb.12.0;Data Source=C:\MyDb\db1.mdb"

3.2 RecordSet . Records ... select .. Database
Syntax Open RecordSet
Open([Source], [ActiveConnection], [CursorType As CursorTypeEnum = adOpenUnspecified],
[LockType As LockTypeEnum = adLockUnspecified], [Options As Long = -1])
..
Source:. table name, Select SQL,View ,Query name or Stored Procedure name
Active Connection
This specifies which database to get the data from.
You can use two different methods, either:
A connection string (something like "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\my
documents\DB1.mdb")
Cursor Type
The cursor type basically determines two things:
1) what methods you can use to move through the recordset (.MoveFirst/.MoveNext/..),
2) whether you see the changes to the data that other users have made since you opened the
recordset.
adOpenForwardOnly
This is the default. It is the fastest, and uses the least amount of memory and network traffic.
1) You can only use .MoveNext to move thru the records (not .MoveFirst etc).
2) You will not see changes to the data by other users since you opened the recordset.
objRS.Open "SELECT field1 FROM table1", objConn, adOpenForwardOnly, adLockReadOnly, adCmdText
University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

Do while Not objRS.EOF
'do something with the data here!
Msgbox objRS.Fields("field1").Value
objRS.MoveNext
Loop
adOpenStatic
1) You can use any of the .Move methods (.MoveFirst/.MovePrev/..).
2) You will not see changes to the data by other users since you opened the recordset.
adOpenKeyset
1) You can use any of the .Move methods (.MoveFirst/.MovePrev/..).
2) You will see Some changes to the data (edited/deleted records only) by other users since you
opened the recordset.
adOpenDynamic
1) You can use any of the .Move methods (.MoveFirst/.MovePrev/..).
2) You will see All changes to the data (added/edited/deleted records) by other users since
you opened the recordset.
Lock Type
The lock type also determines two things:
1) whether you can make changes to the data (add/edit/delete),
2) how other users will be stopped from editing the same record as you.
adLockReadOnly
This is the default. It has the least amount of effect on other users, and is better in terms of
speed/memory/network usage.
1) You cannot add/edit/delete data, you can only read it.
2) You do not block other users from editing records in the table.
adLockOptimistic
1) You can add/edit/delete data.
2) When you edit a record, another user can edit it at the same time - but if you both try to
write the data, one of you will get an error when the .Update occurs.
adLockPessimistic
1) You can add/edit/delete data.
2) When you edit a record, it is immediately blocked from other users. An error will occur as
soon as any values are altered in the recordset (eg: objRs.Fields("Field1").Value = 10 ).
Depending on the database you are using, this may block multiple records, rather than just the
one you are editing!
Option:
.... Object ...... Open
. adCmdText . Source . SQL Select Statement
adCmdTable if Source is table,
adCmdStoredProcedure if source is stored procedure
University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

and adCmdUnknown if unspecified object type
...
Private Sub Command1_Click()
Dim rst As New ADODB.Recordset
Dim sql As String
'sql = "select * from tblproduct"
'rst.Open sql, cnn
rst.Open ("select * from tblproduct"), cnn
Me.Text2.Text = rst(2)
End Sub
Disconnect recordset.
Set rs1.ActiveConnection = Nothing

3.3 Methods ..... RecordSet
Methods . .. .. ADODB.RecordSet ...... DAO.RecordSet
MoveNext
MovePrevious
MoveLast
MoveFirst
AddNew
Update
Delete
Close
..] . Record Database, ADO .. Method Edit .. .... .... ..
Update ....










University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object






















u-: --u-:-
. .... Data Access Object ... ......
u. ..... Syntax .. ,. Database File ...[ DAO
-. .... RecordSet Object DAO
c. ..... Syntax ..,..... . Table ...[ RecordSet DAO
a. ..... Syntax ..,...._. Table ...[ RecordSet DAO
. .. BOF .. .....
o. .. MoveFirst . ......
c. .. MoveLast .......
e. .. MoveNext .......
University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

o. .. MovePrevious .......
. .. EOF .......
u. .. RecordCount .. .....
u-:-. .... Database .. . D:\Student.accdb . Table
... tblStudent(StudentID,StudentName,Phone) ..[. Form
. .....[.


























Y Source Code .

Option Explicit
Dim DB As DAO.Database
Dim Rs As DAO.Recordset
.

University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

Sub AddColumHeader(Lvw As ListView, ParamArray Col())
Dim I As Integer
For I = 0 To UBound(Col)
Lvw.ColumnHeaders.Add , , Col(I)
Next
Lvw.View = lvwReport
Lvw.FullRowSelect = True
Lvw.GridLines = True
End Sub
.
Sub addDataToLvw(Lvw As ListView, ParamArray Data())
Dim Litem As ListItem, I As Integer
Set Litem = Lvw.ListItems.Add(, , Data(0))
For I = 1 To UBound(Data)
Litem.SubItems(I) = Data(I)
Next
End Sub
.
Private Sub Timer1_Timer()
Dim X1 As String, X2 As String
X1 = Left(Me.Caption, 1)
X2 = Right(Me.Caption, Len(Me.Caption) - 1)
Me.Caption = X2 + X1
End Sub
.
Sub ShowInTextBox()
If Rs.BOF = True Then MsgBox "No data in table!", vbInformation: Exit Sub
Me.txtID.Text = Rs(0)
Me.txtName.Text = Rs(1)
Me.txtPhone.Text = Rs(2)
End Sub
.
Function AutoID() As String
Dim Rs1 As Recordset
Set Rs1 = DB.OpenRecordset("Select Max(studentID) from tblstudent")
Dim N As Long
If IsNull(Rs1(0)) Then
N = 1
Else
N = Right(Rs1(0), 4) + 1
End If
Set Rs1 = Nothing
AutoID = "S" & Format(N, "0000")
End Function
.
Private Sub Form_Load()
University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

Set DB = DAO.OpenDatabase(Application.Path & "\Student.accdb", False,
False)
Set Rs = DB.OpenRecordset("select * from tblstudent order by studentid")
Me.txtID.Text = AutoID
Me.txtID.Enabled = False
Me.cmdSave.Caption = "New"
Call ShowInTextBox
Call AddColumHeader(Me.ListView1, "StudentID", "StudentName", "Phone")
Timer1.Enabled = True
Timer1.Interval = 250
End Sub
.
Private Sub cmdShow_Click()
If Rs.BOF = True Then MsgBox "No data in table!", vbInformation: Exit Sub
ListView1.ListItems.Clear
Rs.MoveFirst
Do While Not Rs.EOF
Me.addDataToLvw ListView1, Rs(0), Rs(1), Rs(2)
Rs.MoveNext
Loop
Rs.MoveFirst
End Sub
.
Sub UnEnableButton()
Dim Ctl As Control
For Each Ctl In Me.Controls
If TypeOf Ctl Is CommandButton Then
Ctl.Enabled = False
End If
Next
Me.cmdSave.Enabled = True
End Sub
.


Sub EnableButton()
Dim Ctl As Control
For Each Ctl In Me.Controls
If TypeOf Ctl Is CommandButton Then
Ctl.Enabled = True
End If
Next
End Sub
.
Sub AddtoRecordset()
Rs(0) = Me.txtID.Text
Rs(1) = Me.txtName.Text
University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

Rs(2) = Me.txtPhone.Text
End Sub
.
Private Sub cmdSave_Click()
If Me.cmdSave.Caption = "New" Then
txtID.Text = AutoID
txtName.Text = ""
txtPhone.Text = ""
txtName.SetFocus
Me.cmdSave.Caption = "Save"
Call UnEnableButton
Else
If Trim(txtName.Text) = "" Then
MsgBox "Please, input name of student!", vbInformation
txtName.SetFocus
Else
Rs.AddNew
Call AddtoRecordset
Rs.Update
Call cmdShow_Click
txtID.Text = AutoID
txtName.Text = ""
txtPhone.Text = ""
Me.cmdSave.Caption = "New"
Call EnableButton
End If
End If
End Sub
.
Private Sub cmdSearch_Click()
Dim SID As String
SID = InputBox("Please, input a StudentID for searching!")
If Trim(SID) = "" Then
MsgBox "Please, input a StudentID again!", vbInformation
Exit Sub
Else
Rs.MoveFirst
Rs.FindFirst "StudentID='" & Trim(SID) & "'"
If Rs.NoMatch = True Then
MsgBox "Not Found!", vbInformation
Else
Call ShowInTextBox
End If
End If
End Sub
.
University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

Private Sub cmdDelete_Click()
Dim SID As String
SID = InputBox("Please, input a studentID for deleting!")
If Trim(SID) = "" Then
MsgBox "Please, input a studentID again!", vbInformation
Exit Sub
Else
Rs.MoveFirst
Rs.FindFirst "StudentID='" & Trim(SID) & "'"
If Rs.NoMatch = True Then
MsgBox "Not Found!", vbInformation
Else
Dim B As Variant
B = MsgBox("Are you sure, to delete this record!", vbYesNo)
If B = vbYes Then
Rs.Delete
MsgBox SID & " was deleted!", vbInformation
Call cmdShow_Click
End If
End If
End If
End Sub
.
Private Sub cmdUpdate_Click()
If Trim(txtID.Text) = "" Then
MsgBox "Please input StudentID!", vbInformation
txtID.SetFocus
ElseIf Trim(txtName.Text) = "" Then
MsgBox "Please input name of student!", vbInformation
txtName.SetFocus
Else
Dim B As Variant
B = MsgBox("Are you sure, to update this record!", vbYesNo)
If B = vbYes Then
Rs.Edit
Call AddtoRecordset
Rs.Update
Call cmdShow_Click
End If
End If
End Sub
.
Private Sub cmdFirst_Click()
If Rs.BOF = True Then MsgBox "No data in table!", vbInformation: Exit Sub
Rs.MoveFirst
Call ShowInTextBox
University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

Me.txtNo.Text = Rs.AbsolutePosition + 1
End Sub
.
Private Sub cmdLast_Click()
If Rs.BOF = True Then MsgBox "No data in table!", vbInformation: Exit Sub
Rs.MoveLast
Call ShowInTextBox
Me.txtNo.Text = Rs.AbsolutePosition + 1
End Sub
.
Private Sub cmdNext_Click()
If Rs.BOF = True Then MsgBox "No data in table!", vbInformation: Exit Sub
Rs.MoveNext
If Rs.EOF = True Then
Rs.MoveLast
MsgBox "This is the last record!", vbInformation
Else
Call ShowInTextBox
Me.txtNo.Text = Rs.AbsolutePosition + 1
End If
End Sub
.
Private Sub cmdPrevious_Click()
If Rs.BOF = True Then MsgBox "No data in table!", vbInformation: Exit Sub
Rs.MovePrevious
If Rs.BOF = True Then
Rs.MoveFirst
MsgBox "This is the first record!", vbInformation
Else
Call ShowInTextBox
Me.txtNo.Text = Rs.AbsolutePosition + 1
End If
End Sub
.

rrrhsss










University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object







University of Management and Economics UME Visual Basic 6.0

Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object

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