View
126
Download
7
Category
Tags:
Preview:
DESCRIPTION
za
Citation preview
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:
eyIgdwgehIyfa Database RtUvEtman Application rYmTaMg Business logic. Database RtUv)anbegáIt
eLIgeday DBMS nig Application rYmCamYy Development tools dUcCa VB, VB2005, Java….
etIeFVIdUcemþcedIm,IeGay Development tools TaMgenaHGaceFVIkar)anCamYy Database enaHKW Data
Access. dUcenH Data Access KWCa Tool Edlpþl;nUv Objects sMrab;eFVIkarCamYy Database. Data Access
rYmman ³
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)
……..
edaysareyIgeRbIR)as; plitpl Microsoft Visual Basic 6 and Ms Access dUcenHeyIgnwgsikSaEt DAO nig
ADO .
2. DAO (Data Access Object)
DAO Ca Library (pÞúkComponents) sRmab;[elakGñkGac Connect BI Application CamYy Database.
DAO mantYnaTIbkERb nigbBa¢¢¢ÚnTinñn½yBI Application mk Database nig BI Database mk
Application vij.
munnwgelakGñkGaceRbIR)as; DAO )anCadMbUgsUmelakGñkcUleTAFIkkñúg Project Menu eRCIserIs
yk References bnÞab;mkFIkyk 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. rebobebIk Database File (Microsoft Access File)
SyntaxTI1
]TahrN_
Option Explicit
Dim DB As DAO.Database
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Private Sub Form_Load()
Set DB = DBEngine.OpenDatabase("D:\Employee.accdb", False, False)
End Sub
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
SyntaxTI2
]TahrN_
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
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
taragBnül;GMBI Syntax xagelI
smasFatukñúg Syntax
GFib,ay
DBVariable CaGefr Database Object EdltMNag[ Database File EdlelakGñk
cg;ebIk.
DBName Ca String EdltMNag[ Path nigeQµaH Microsoft Jet Database File b¤ Ca
Data Source Name (DSN) rbs; ODBC Data Source
Option
Ca Optional Argument EdlRtUve)aHtMéleTA[ Parameter nUvtMélBIrKW
True b¤ False.
- True : elakGñkebIk Microsoft Jet Database CalkçN³ Exclusive
Mode (Only one user can access) mann½yfaelakGñkcg;
karBarmineGaymanGñkepSgeTotebIk Database File enH CaBiess
enAelIRbB½n§ Network.
- False (Default): mann½yfaelakGñkebIk Microsoft Jet Database
CalkçNH Shared Mode(Multiple users can access).
Read-Only
- True : elakGñkebIk Database File kñúgkRmitmYyKWGacRtwmEtGan
Tinñn½yEtminGacEk lubral; Object EdlmanenAkñúg Database File
)aneLIy.
- False (Default): elakGñkebIk Database File kñúgkRmitmYy
EdlGacEk lubral; Object EdlmanenAkñúg Database File )an.
2.2 rebobebIk Microsoft Access File EdlmanCab; Password
SyntaxTI1
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
SyntaxTI2
Microsoft Access File Edlman Password KWtRmUv[man [Connection] EdlCa Optional
Argument mYyeTot. dUecñHRbsinebI Microsoft Access rbs;elakGñkmanCab; Password enaHelakGñk
RtUVve)aHtMél[ Connection Parameter edayP¢ab;CamYy Password eTACamYy ebIBMuenaHeTnwgman Error
ekIteLIg.
]TahrN_
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 kareRbIR)as; RecordSet Object rbs; DAO
RecordSet Object eRbob)aneTAnwg Table beNþaHGasnñ (Virtual Table) EdlmantYnaTIrkSaTuk
Tinñn½yenAkñúg Memory CabeNþaHGasnñbnÞab;BIelakGñk)anTajykTinñn½yBI Table cUlkñúg RecordSet Object.
munnwgelakGñkGaceFVIkarCamYYy RecordSet Object )anluHRtaEtelakGñkRbkasGefrNamYy[man
RbePTCa RecordSet Object Camunsin ehIyRtUv Set RecordSet Object enaH[eTATTYlykTinñn½yBI
Table b¤BI QueryNamYYy.
Syntax:
]TahrN_
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 …………………………………………………………………………………………………………………………………..
taragBnül;GMBI Syntax énkareRbI RecordSet
Argument
GFib,ay
RS CaeQµaH RecordSet Object EdlelakGñkcg;ebIk
DBVariable Ca Database Object EdlelakGñkkMBugQrenAelIva
Name Ca String Parameter EdlGacCa Table b£ CaeQµaH Query b£ mYyCa
SQL Statement
[Type] Ca Optional Argument eRbIedIm,IbBa¢ak;BITRmg; RecordSet EdlelakGñk
cg;ebIkEdlBiBN’nalMGitenAtaragxageRkam
[Option] Ca Optional Argument EdleRbIedIm,IbBa¢ak;BIsiT§kñugkarbBa©ÚlTinñn½y
[LockEdits] Ca Optional Argument EdleRbIsRmab; Lock RecordSet enAeBlEdl
man User elIsBImñak;cUlmkdMeNIrkar Table EtmYy.
[Type] Argument:
dbOpenTable
- eFIVkarpÞal;enAelI Table
- GacbBa©Úl Ek lub nig GanTinñn½y)an
- minGaceRbICamYy SQL Statement b¤ Query )aneLIy
dbOpenDynaset
- eFIVkarenAelI Virtual Table
- Gac bBa©Úl Ek lub nig GanTinñn½y)an
- GaceRbICamYy SQL Statement b¤ Query )an.
karEkTinñn½ykñúg Table BI Users epSgeTotelakGñkGacemIleXIjtamry³
RecordSet EdlelakGñkkMBugebIk b:uEnþcMeBaHkarlub b¤bBa©ÚlTinñn½yfµIvij
University of Management and Economics UME Visual Basic 6.0
Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object
elakGñkminGacemIleXIjBITinñn½yEdlERbRbYlenaHeLIy.
dbOpenDynamic
- eFIVkarenAelI Virtual Table
- Gac bBa©Úl Ek lub nig GanTinñn½y)an
- GaceRbICamYy SQL Statement b¤ Query )an
- karEk lub bBa©©ÚlTinñn½yTaMgGs;kñúg Table BI Users epSgeTot
elakGñkGacemIleXIjral;Tinñn½yTaMgGs; bnÞab;BImankarEk lub nig
bBa©ÚlTinñn½yfµI.
dbOpenSnapshot mann½yfaebIk RecordSet kñúgTRmg;mYyEdlelakGñkminGacemIleXIjral;
karpøas;bþÚrTinñn½yEdlmanenAkñúg Table edaysar User epSgeTot.
dbOpenForwardOnly
mann½yfaelakGñkebIk RecordSet kñúgTRmg;mYyKWGnuBaØat[EteFVIkarGan
Tinñn½yBI RecordSet b:ueNÑaH. b:uEnþminGnuBaØateGayeFIVkar Ek lub nig
bBa©ÚlTinñn½yfµIeLIy.
[LockEdits] Argument:
dbReadOnly eFVIkar Lock eTAelI Record edayminGnuBaØat[Ekral;Tinñn½yKWmann½y
faelakGñkGac)anRtwmEtGanTinñn½yBI RecordSet Etb:ueNÑaH.
dbPessimistic
cMeBaHlkçN³ Lock EbbenHmann½yfaenAeBlEdl User1 ebIk RecordSet
edIm,IEkelI Record NamYy ehIykñúgeBlenaHman User2 ebIk RecordSet
edIm,IEkelI Record enaHEdr eBlenaH User2 EdlcUlmkEktameRkayenaH
minGacmansiT§iEkeLIy RbsinebI User1 kMBugEtEk Record enaH (eRbI Edit
Method) . User2 mansiT§iEkluHRtaEt User1 )anEkrYcral;sin.
University of Management and Economics UME Visual Basic 6.0
Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object
dbOptimistic
mann½yfaenAeBlEdl User1 ebIk RecordSet edIm,IEkelI Record Na
mYyehIykñúgeBlenaHman User2 ebIk RecordSet edIm,IEkelI Record
enaHEdr eBlenaH Users TaMgBIrsuT§EtmansiT§iEkelI Record enaHdUcKañ.
2.4 karbBa©ÚlTinñn½yeTAkñúg Table edayeRbI RecordSet
edIm,IbBa©ÚlTinñn½yeTAkñúg Table tamry³ RecordSet elakGñkRtUveRbI Syntax dUcxageRkam³
Syntax TI1
Syntax TI2
Syntax TI3
]TahrN_³ ]bmafaelakGñkman Database mYysßitenAkñúg “D:\Employee.accdb” nigman Table
mYyeQµaH tblEmployee EdlmanTRmg;dUcrUbPaBxageRkam³
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
……………………………………………………………………………………………………………………..
b¤
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
……………………………………………………………………………………………………………………..
b¤
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
……………………………………………………………………………………………………………………..
elakGñkGacbegáItGnuKmn_ (Sub Procedure) dUcxageRkam³
Sub AddToRS()
Rs(0) = txtEmpID
Rs(1) = txtName
Rs(2) = txtSex
Rs(3) = txtPhone
Rs(4) = txtAddress
End Sub
sUmelakGñksakl,gehA Sub Procedure xagelImkeRbIkñúg cmdAddNew_Click dUcxageRkam³
……………………………………………………………………………………………………………………..
Private Sub cmdAddNew_Click()
Rs.AddNew
Call AddToRS
Rs.Update
End Sub
……………………………………………………………………………………………………………………..
2.5 karGanTinñn½yBI Table edayeRbI RecordSet
Syntax TI1
Syntax TI2
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 TI3
]TahrN_
……………………………………………………………………………………………………………………..
Private Sub ReadToTextBox_Click()
txtEmpID = Rs(0)
txtName = Rs(1)
txtSex = Rs(2)
txtPhone = Rs(3)
txtAddress = Rs(4)
End Sub
……………………………………………………………………………………………………………………..
elakGñkGacbegáItGnuKmn_ (Sub Procedure) dUcxageRkam³
Sub ReadFromRS()
txtEmpID = Rs(0)
txtName = Rs(1)
txtSex = Rs(2)
txtPhone = Rs(3)
txtAddress= Rs(4)
End Sub
sUmelakGñksakl,gehA Sub Procedure xagelImkeRbIkñúg cmdReadToTextBox_Click dUcxageRkam³
……………………………………………………………………………………………………………………..
Private Sub cmdReadToTextBox_Click()
Call ReadFromRS
End Sub
……………………………………………………………………………………………………………………..2
2.6 kareRbI RecordSetObject.BOF Property
BOF (Begin of File) Ca Property mYyEdleRbIsRmab;bBa¢ak;faetI Table EdlelakGñkTajTinñn½y
mkkñúg RecordSet manTinñn½y b¤minTan;manTinñn½y. RbsinebImanTinñn½yenaH BOF mantMélesµI False
pÞúymkvijKW BOF mantMélesµI 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
]TahrN_
……………………………………………………………………………………………………………………..
Private Sub ReadToTextBox_Click()
If Rs.BOF = False Then
Call ReadFromRS
Else
MsgBox “No data in your table!”
End If
End Sub
……………………………………………………………………………………………………………………..
2.7. kareRbI RecordSetObject.MoveFirst
Ca Method EdleRbIsRmab;rMkil Cursor eTAkan;TItaMg RecordTImYyrbs; RecordSet. Ca
Default KWTItaMg Record sßitenA MoveFirst.
]TahrN_
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. kareRbI RecordSetObject.MoveNext
Ca Method EdleRbIsRmab;rMkil Cursor eTAkan;TItaMg Record edaycab;BITItaMg Current Record
eTAmuxmþgmYy Record.
]TahrN_
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. kareRbI RecordSetObject.MoveLast
Ca Method EdleRbIsRmab;rMkil Cursor eTAkan;TItaMg Record cugeRkaybMputén RecordSet.
]TahrN_
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. kareRbI RecordSetObject.MovePrevious
Ca Method EdleRbIsRmab;rMkil Cursor eTAkan;TItaMg Record edaycab;BITItaMg Current Record
[fyeRkayvijmþgmYy Record.
]TahrN_
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. kareRbI RecordSetObject.RecordCount
Ca Property EdleRbIsRmab;rab;cMnYn Record EdlmanenAkññúg RecordSet.
]TahrN_
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
lT§pl³
2.12. kareRbI RecordSetObject.EOF
Ca Property mYyEdleRbIsRmab;RtYtBinitüemIlfaetI Cursor )anrMkildl;TIbBa©b;én Record ehIyb¤
enA RbsinebI Property enHTTYl)antMél True mann½yfa Cursor )anrMkildl;TIbBa©b;én Record ehIy
pÞúymkvijebITTYl)antMél False mann½yfa Cursor minTan;rMkildl;TIbBa©b;én Record eT.
]TahrN_³ ]bmafaelakGñkman ListView mYyeQµaH ListView1 enAeBlelakGñk Click elIbUtugeQµaHfa
cmdShowInList_Click enaHvanwgGanTinñn½ykñúg RecordSet TaMgGs;cUleTAkñúg 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 ………………………………………………………………………………………………………………………………….
lT§pl³
2.13. kareRbI RecordSetObject.AbsolutePosition
Ca Property mYyEdlGac[elakGñkTTYl)annUvTItaMg Record EdlelakGñkkMBugQrenAelIvakñúg
RecordSet. ehIyProperty enHGaceRbI)anluHRtaEtelakGñkebIkRecordSet kñúgTMrg; dbOpenDynaset
Type cMeBaH Property enHvae)aHtMélsUnüRbsinebI RecordSet kMBugQrenATItaMg RecordTI1.
]TahrN_
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. kareRbI RecordSetObject.Close
Ca Method eRbIsRmab;biT RecordSet EdlelakGñk)anebIk.
Syntax:
]TahrN_³ 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 Ca Library mYyeTotrbs; Microsoft sMrab;eGay Developer eRbIR)as; sMrab;
Access Database. ADO KWCa Database Access components CMnan;fµIrbs; Microsoft
vaRtUv)an Microsoft eRbIR)as;cab;BI Visual Basic 6. cMnucBiessrbs; ADO:
- Using OLE DB Technology(Object Linking and Embedding, Database)
- Gac Distribute elII Internet mann½yfaeKGaceRbI ADO edIm,I Develop Web
Application dUcCa ASP.
- Support Clients/Server Cag DAO and Support Distributed Database.
Object and structure of ADODB
- Connection: Ca Object edIm,IP¢ab;cUl Database mann½yfaebIeyIgcg;sresrkUd
edayeRbI ADODB cUl Database NamYyeyIgRtUv Tajyk Database enaHtamry³
Connection Object rbs; ADODB.
- RecordSet: pÞúk Records rbs;EdlTajecjBI table,views
-Command³ Ca Object pÞúk Parameters edIm,I Execute Server subprogram dUcCa
Stored procedure or functions.
Note:
edIm,IeRbI ADODB GñkRtUvyk Reference Microsoft Active Data Objects 2.6 Library
3.1 Connection
edIm,I Connect cUl Database eyIgRtUv³
- begáIt 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
- ebIk Connection edIm,ITaj Database dak;elI Connection
Cn.Open "Connection String"
Connection KWGaRs½yelI Database EdlmanTMrg;TUeTA³
ConnStr="Privider=OLE Provider;[Data Source="path/database name"]
OLE Privider CaeQµaH Database provider EdlsÁal;eday ADOdUcCa
DBMS Provider
Ms Access 2007 microsoft.ACE.oledb.12.0
Data source: Ca String GaRs½yelI DBMS ebI
Access: Path\filename.mdb
]TahrN_1 Connect cUl 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 pÞúk Records Edl select ecjBI Database .
Syntax Open RecordSet
Open([Source], [ActiveConnection], [CursorType As CursorTypeEnum = adOpenUnspecified],
[LockType As LockTypeEnum = adLockUnspecified], [Options As Long = -1])
kñúgenaH
Source:Ca 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:
kMNt;TMrg; Object EdlRtUv)an Open
man adCmdText ebI Source Ca 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 .
]TahrN_
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 sMxan;²rbs; RecordSet
Methods sMxan;²rbs; ADODB.RecordSet manRbhak;RbEhleTAnwg DAO.RecordSet
MoveNext MovePrevious
MoveLast
MoveFirst
AddNew
Update
Delete
Close
edIm,IEk Record kñúg Database, ADO Kµan Method Edit eT dUcenHebIRtUvkarEk eyIgEk RYc
Update EtmþgeTA.
University of Management and Economics UME Visual Basic 6.0
Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object
sMnYr niglMhat;
1> etIGVICa Data Access Object ? etIvamantYnaTIdUcemþcxøH?
2> cUrelakGñksresr Syntax edIm,IebIk Database File edayeRbI DAO?
3> etIGVICa RecordSet Object rbs; DAO?
4> cUrelakGñksresr SyntaxmYy edIm,IbBa©ÚlTinñn½yeTAkñúg Table edayeRbI RecordSet rbs; DAO ?
5> cUrelakGñksresr SyntaxmYy edIm,IGanTinñn½yecjBI Table edayeRbI RecordSet rbs; DAO ?
6> etI BOF mantYnaTIdUcemþcxøH?
7> etI MoveFirst mantYnaTIdUcemþcxøH?
8> etI MoveLast mantYnaTIdUcemþcxøH?
9> etI MoveNext mantYnaTIdUcemþcxøH?
University of Management and Economics UME Visual Basic 6.0
Lecturer: TONG SAMNIENG (MSc-IT) Phone: (017) 933 396 9- Data Access Object
10> etI MovePrevious mantYnaTIdUcemþcxøH?
11> etI EOF mantYnaTIdUcemþcxøH?
12> etI RecordCount mantYnaTIdUcemþcxøH?
lMhat;³ ]bmafaelakGñkman Database mYysßitenAkñúg “D:\Student.accdb” nigman Table
mYy eQµaHfa tblStudent(StudentID,StudentName,Phone) nigmanTRmg; Form
dUcrUbPaBxageRkam³
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
Recommended