Professional Documents
Culture Documents
*
! "# $% %&'
.( http://www.vba4a.com/vb/showthread.php?t=15 ) (!&
)
+
ActiveX Data Objects
!
/
01
2 3
4 "
#5 6
4
/
"
<5= ; 37 81 9: / $ $4 ( ADO )
.Visual Basic > 39:
Microsoft ActiveX Data Objects 2.8 )
4 >
A5 &I
MG
:!
)1
&
O1 8
E; K )&C
5 <
=&5 6
4 %
#;
5A! N
5
D% K ( Library
U1
A
V2
5A! 8
D% HGI
! >
A
4
5
@5 T6
4 /
Q,S G
7P
$
(!&
Microsoft Visual Studio 6.00
2 8
1 )6
! ( http://www.vba4a.com/vb/showthread.php?t=176 )
.X YZ E; /@51 "&W! @C5
:: 4 dA ::
( Microsoft ActiveX Data Objects 2.x Library ) <=&5 `A1
/
! )
! g
h /
! 3I
' H
YC
; 3I
' 5 / !J .. eJ
/
@
1&B /
C
; 8
1
5 / T64 K H
T6
4 /
_
6
:
a T6
4 / \1&I > E@1 K Connection a
: $ A 3I'J YC; $%
J a
: ADO 32 )' H>1V Microsoft &C $ (!& ! )@51 **
http://support.microsoft.com/default.aspx/kb/183606
H
:> H>
=5
<
5 <h_
' $
3
D U2 <=&
/
@ 39
: /
)
+
H
< 3I'J YCj! J D' E i^1 K
.
3I
J
H
D! 3I
'J $
% M _
1 `6
Connection
>
@ $
%
5 /@ 64 / H 1 = 2 YC; $4 3I'J YC; $% k Hh
: &:b mW! H&5 T64 l#&W !_
Dim CN As ADODB.Connection
Set CN = New ADODB.Connection
'////////////////////\\\\\\\\\\\\\\\\\\\\
'For MS ACCESS
strProvider = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data
Source = <0
%">
'=========================================
'Open Connection
CN.ConnectionString = strProvider
CN.Open
:: dA ::
7
Command /
@
0 q&
:k ADO
p
! `
A'
/
@ 3I
'J
! _
;
D' $
Connection
.H&Z ADODB.Connection
<
_
5 8
> 0 3I'J 5= $% HG65 95 )
.J; t \# (D% # /@ K ( > )6 < ) 9 T64 [62 i^1
H
D! 3I
'J N
DD2
E
@' /D!_
/D1&
h q
2; "'!
/
H / 3IJ )@51 3I'J
/ YJ ! K HG65
: $1 5 Close d 39:
CN.Close
1
`
A' ( 3I
'J _
) 3I'J 9 Ej% e1 1G 5
G&
5
5 User ID
_
5 [&
5C
' $
5 /
T6
4
! 8
1 U
2 Database Name
H
8
Password
" ; "
BD5
7
9! I
8
D Y5
k / nu HG7 $% 95
: 5 /' ; nA' 3I'J 9 YC;
'For MS ACCESS
"Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =
C:\Books.mdb"
'or
'=============================================
'or
ODBC yC
4 3I
'J 5 )C; )'G: 2
Provider
5 Driver
5
5 /
! )+ OLE DB >V
Open Database HG
eGI
: ODBC &
' U
2 K e
549@ t
.
6 H>=5 9yC5 D' $4 Connectivity
.E@P G e5>
! 7?1 6 K D! / z 2 $% OLE DB
** G I 8
**
8
)
@51 K
/ N 5 H&0@!
/ 8w&
/
3I
'J _
/5
# Data Source Name ( DSN )
G I
: $ >@ $% 5 ODBC ! G I
(
D% `
A' $
% .1y h_! >@ 64 $% 3I'J _ E% q&' 5
! G I D! /1V
' 8' U2 G&5 5
_5 8 DSN
$
\
% K )
2
ODBC
! G I
>
P .i
A
[&?5 ODBC
&
: 8
F Administrative Tools
(y
# 8F Control Panel 8@A 2
ODBC Data Source H6
% )
&O
)
+
! Data Sources
8
1 U
2 3I
'J
! 3
:>; $
%
)
@51 $
Administrator
_
5! User DSN o
1
5! /
_5 <5^ System DSN oI
'
: )! g: ODBC 3I' 8 YCP GI &O ... (D% $A
T
1 A!
5 3
05 $
%
4 K
H M&A " 1 A! 8 HGI $%
T> HGI $% 5 SQL Server l
)
! g
yC
5 8
4 K ODBC Name /
!
5 K
HGI
$
%
>
! )6
K ODBC 9yC
5 /5
#
!
5% &O
`6
. H D {|_5 ( &%&_ )
5! : $
'f }
2J /@ K >
3: 3I'J ! 1 A! 5 HGI $%
4 K 3k : /1G
: 1
% MS SQL Server <
5
G& 5
_ 8
J "> J Windows NT Authentication
> I
! /A
% $
0 G
... HGI
!
4
5 ( ~ ) 8
G G
:J G
: )@51 6
8
3
:>;
ih
6
SQL Server Authentication
.HGI / ( ) 8G YV^! 4 5 G&5 5@ %#P!
_5
. HGI! \# 4 5 H 8 &:
(y|! 8 K q&: H6% &O D!_ HGI $% 5 Next (y| !
l% (y#% TC; 8' 5! &1&D' &O 8F &y' 1 z 2; E> Finish
: H6% T64 &O OK 5
** Execute d
**
$
%
_' $ Execute d Connection /@ `A1
/
9^
"=&
! HG
T64 N +; . G I SQL HG yC'
oI
'
1&B /
H 1 Z B_! 9^_ T64
)@51 K G I
9^
5^ /
; Execute
d /
^'&5
5D
.RecordSet Object
:: dA ::
/
&
0 39
: /
[
t
; 3
7 )
@51 K ADO <
5
/
`
1&B /
46
' /
@51 $
"=&
5 )
+ 3
0 .
D1&B
`
A1 U
2 Connection Command RecordSet
@
. H / "=&J d 8
`>
$
"&C
YC
j! 8
8
F K Visual Basic 6.00 &1
h' a! \%
.Standard EXE
.( ~ ) 8G HGI! \# 8' 5 K ADO @ <=& %#j! 8
; 90
l5_
'
>k 7 / ( ListBox ) >& <!& %#j! 8
.lstStudents
>k
7 /
( Command Button ) &
%
#j! 8
Run
; ( Caption ) l
&
y! 8
cmdLoadList
; l5_
'
.Query
: !_ &k 5! g
Click z A ; $ >@ %#j! 8
RS.Close
CN.Close
Set RS = Nothing
Set CN = Nothing
/
@ $
5' $
Execute
d Y
8
' !_
>
@ $
%
$
RecordSet 9^
5^ "=Gj! 4G ! N U2 Connection
H5_
5
1G&@
DA HG
8
' )
+
! .RS &y5
!
1V
' 8
'
; l5G iB 8 %#; 9^_ 5^ 39: M&A DO .. LOOP
.ListBox l [&?1 >&_ "&
#
H
/ 3IA 5% %&'
/
[
7
4G
! `
A' $
RecordSet 9^_
5^ /
:>
6
' 39
: /
)
+
H 3 = / &0 3 = :> H>=5
E J; 3
= /
&
0
5C
1
5!G 9
J E /
8w&
.9J 2
`
A1 (
D%
2 3
=
W ]
& _
!
' ^' 9^_ 5^
5 K 2 ^ 3DA / 5^ @C' U2 K
5 5 3DA Y5
.^' 9^_ 5^ /1@ 9^_ <5^'
RecordSet 9^
5^ YC; [&! $1 5% D
.Visual Basic > 39: / '9^ &
** 9J
! 9^_ 5^ YC; **
! RecordSet /
@ Y
YC
;
5%
5'
5^
`
A1 /
@ 6
4 E J; ConnectionObject.Execute
<
5= <
3
A
4
5 .
"=&
$
%
_
5 oI
{
d /
E
RecordSet /
@ /
H
1 =
2 YC
j! J
D' E i
^1 @
: $ >@ $% 5 l
! D'
rsStudent.ActiveConnection = CN
'OR
rsStudent.ActiveConnection = "DSN = Students"
: U2
G I
; &C
1
3I
'
05' CN E >@ / 3k &h_ $% &?1
. / A# 5
/
YC
; 8
1
A T6
4 $
% 3I' _
8' $0 &h_ $%
.e5# 3I'J
Y
5 RecordSet.Open
d
$
>
@ \
#1
/
YC
; J 8
1 U
2
! RecordSet 9^_
5^
>
=5 ( ListBox ) >&_
<
!&
; l
'! %
#; 8
F
1 = RecordSet
8
F !_
n+
5
7 t! $A h 1 = n+5 %#j! 8 .n+5!
: 5 n+5! >=5 5 Click z A Y&=; $% $ >@ 3:>j! 8
CN.Open strConnect
RS.Close
CN.Close
Set RS = Nothing
Set CN = Nothing
RS.Fields("st_ID")
'OR
RS.Fields(0)
8
n+
5
@ Y5
&
y'
5' "
' D!_
ZC
85I
' !
: $ >@ !@!
Option Explicit
'--------------------------------
RS.Move Int(txtJumb.Text), 1
Call Display_Current_Record
End Sub
'--------------------------------
RS.MoveNext
Call Display_Current_Record
End Sub
RS.MovePrevious
Call Display_Current_Record
End Sub
'--------------------------------
RS.CursorType = adOpenStatic
Call Display_Current_Record
End Sub
'--------------------------------
Dim i As Integer
Dim s As String
For i = 0 To RS.Fields.Count - 1
Next i
End Sub
:: 4 dA ::
/
E>
H
D H&
Z 3
7!
5%
5
H
<
D1&B /
&
0 ) <# 2 $ e 5 )+ K Connection
.
Static /
U
A
!
! 5^ YCj! !_ >@ $% 5
7
adOpenStatic
5D oI
! )
+ Cursor
t
@
/4
^'J 9
$
% 9^_
/! D! \5_' $ CursorType
D! \5_
' $
/ 4
$ adForwardOnly 5D
5_
5 $
& Y&
=P YC
j!
5 )
+
! .(
D%
BOF /
7
G
:
% 81 $ ( Display_Current_Record )
1y 1G&
# H
h
T6
4 .$A ^_ 3D2 8 Y5 & J EOF
$
% W
h: z
2
; `>1 $A ^_! >= &w D2 ; 37 A EJ )+
.3I'J 5
4 Form3
1 ^ n+
5
^' E _
' J !_
h yC! 8
: HGI! 5 ] IA' h )yC' KyC Y ! n+5
** 9^_ > 3IA **
7
Recordset /
@ 2I
5
oI
/
/
8w&
% .
5^5 9^
>
"=&
J
_
5 RecordCount
K
1 ; 5^5 1 ! / 9^_ > m_A EOF 7:
@;
$
% 5 ^5 h: / 1 &%1 RecordCount 7
E J;
: $ >@
If rsBooks.RecordCount = 0 Then
MsgBox "No books entered yet !!"
Else
MsgBox "Number of books registered = " &
rsBooks.RecordCount
End If
** 9^_ 5^ \Z&' u&% **
$
Sort Filter /
7
Recordset /
@ `
A1
U
2 K
_
5 SQL 9
$
% &
y E> D!_
5 t
Y>
@51
3
DA 8
D! I
I
! 9^_
i
'& Sort
7
)
@51
: $1 5 " , " 9! I i'& $%
_5
:: dA ::
8
1 l% 5; 8 >; Sort Filter /7
/ ` /'
&
y t
@' $
e
D' Recordset 9^_
5^ 1A U1 A'
.H 1 ^
Rs.Fields("FirstName").Value = "Asmaa"
Rs.Update
.Recordset
Dim RS As New ADODB.
Dim strConnect As String, strSQL As String
'Open a recordset
RS.CursorType = adOpenDynamic
RS.Open strSQL, strConnect
RS.AddNew
RS!st_ID = 8
RS!st_FName = "Abdo"
RS!st_LName = "Negm"
RS.Update
RS.Close
MsgBox "A new record has been added successfully !!",
vbInformation
:: dA ::
9^_
3
D2 8
/
'
! Update
d Y
! 8
D' 8
+;
nG>
'! i46
H
; 9^_
T6
4 %
#; 8
1 /
% H 1 ^
/
!
D {
d /
`
! &:b ^ ; 3DJ >&^5! ED1 5 1&
. / &+ $ 9^_
d
! 9^_
5^
:> $
A ^_
[6
2 e|
1 )
@51
: $1 5 Delete
Rs.Delete
6
K $
A ^_
e>= M4 \I1 J ( Delete ) d T64 Y >&^5!
9^_
/
!
D {
d q
2; Y
! T64 [6A 5 iD' E i^1
.3DA 8 ; 37 A
** ADO $% 9^_ /W' **
A
% 6
K (
D% HY&
D e
#&% ADO 9^
5^ E
@'
>G +j
% .W
h:
G G
d $
% i_
1
A T6
4 $
% 3DA 2 5 oI
'
q&
:
5 /
! J
D' E )
i
^1 K 9^_
[6
2 &
y' %
#;
$
%
5
_
5 9^_
/W
' " 1 A
_5 LockType 7
:$ >@
RS.LockType = adLockOptimistic
"=&
<1&_
_
' : ( adOpenForwardOnly )
5D
T6
4 / H>J )@51 ... 9^_ 5^ :> (D% k ;
ComboBox / a D1&h T6! 3 ^! 3I'J \% $% 5D
H&6
$
% ^_
!
2J
; =2 $% E@' / )k K .xListBox ..
; 3
DJ ^_
HY&
>&
^5!
D1&h T6
)
! K l
'Y&
!
{
1
5 H&6
/
5= D!_
9^_
[6
2 8
1 l $ ^_
. u^ H&+ $% 5 Y
. /1&:f /
_5 / 8' $
D1&B N
)
k )
+ K /1&
:f /
_5 / &y' `
$
% D1&h T64 0
)@51K ( /_ ) $@'J \
. &1GD 7 YC; e90 \
<
5=
/
H " 9^_ &Z5 25 8D " 51
5D E@' K 305 Ms Access ! H 2 $% .
HY&
D
_
' $
adOpenForwardOnly $
4 &
Z5
#&%J
adOpenKeySet &
Z5
|
?1
5! K
H
/
1&_
.q&:k &5 5 U1 A 5 2 $%
** Command &k / **
<
$
% N
8
O $|
D' )
% K / 5^ < 5
T6
4 "=&
>G
+; /
@ .Recordset
a /
@ {
d oI
:
/5|
' )
\
1 `6
ADO Command
/
w J E ^_% K
4 E@1 $! H& / &0
9 ! / :> EV
SQL Y&=; 9
9
/1V
' 8
1 .H
1 &
5 /
5 6
' >G +; $
05
A
/
@! g
Parameters <
5^
:> EV
5 Y&
=P 9
J
T
l
'9 &
y' )
@51 /
@ 64 > ; / $ E ! .Command
.H& / &0
?1
EV
5 Y&=P
! TG
:; Command /
@ YC
; )
@51 )
+
!
&
1&5' Ms SQL Server
! H
:> >
=5 Stored Procedure
: $ >@! 5 Y&=P )+ $% mh5 5 5
6
4 $
% 42&
Z $
ADO <5= `A1 >@ 64 E% q&' 5
@ /
5^
Command /
@ `
A1
5 .Ef
2 "
#5
YC
; ! )@51 .EV
5 Y&=P :> 051 $ Parameters
>
39
: /
q&
: H&
95 T64 Y2 Command /@
: $1 5 >@ &h /
&
k 9
&
y'
! q&
: H&
9^_
5^ \
% H>; 81 >@ 64 $%
.A [&A! ' $ Y5k <5= "=& 81 U2 Command /@
:: dA ::
5' $
Execute
d
e|
1 Command /
@ `
A1
.Connection /@ $% 05 !C D1&h!
RS.CursorType = adOpenKeyset
RS.CursorLocation = adUseClient
RS.LockType = adLockOptimistic
RS.ActiveConnection = Nothing
'Create Structure
RS.Fields.Append "Name", adVarChar, 25, adFldIsNullable
RS.Fields.Append "Age", adInteger, , adFldMayBeNull
'Add a record
RS.Open
RS.AddNew
RS.Fields("Name").Value = "Ahmed Negm"
RS.Fields("Age").Value = 22
MsgBox "Data has been saved successfully !!",
vbInformation
RS.Update
5' Ef N K / e ! ) 4 >@ 64 $% 1 ^
N
:>
) 0% mh 4 5 hD T64 NG> +; .. $I
C Mu= H&6!
e
5> >
'
U
2 e_
7 E
@'
H&
@ E 8
K /%&
A5 [I
{
:
<
#
4 /
@ K ! H D %#P! CN / < RS
YC
j!
5
D% K 9
BP
"
` / ! H D & 8 4 /A% [9:J
`6
u
^ H&
+ 4 T>= E@ $54 3 = 4 9BP 1 = " / 3 =
&
B )
@j! /
@ )
+ /
&
0
hD T6
4 /
z A <h / .. l 5'
)_
! 3
^!
&A (
!G 8_
$
% q
5
MG_
4 >
@ 6
4 $
% |
1
1 = &
d
5 .. )_
! 3
^% 8_
D!
A5
/
&
0
`
A' 5^ ; D2 %#j! D' $ Append d
0 l'
7 p
! )6
DA 64 3B l'! " DA 8 0
@; K adFldIsNullable
7
)
! Null
5D 3D
DA $
%
5 K adFldMayBeNull
7
)
!
wG% 8
3D
.i'& Age Name
RS.Open "C:\TempTable.xml"
)
W!
' .. 5 T64 &D' H& 3 N +; m&h|1 ! )D EW! &Z
6
4 Y
w &
0k
@A \I5 q 2; $% {ZGk 8_D! 1 = {d5 )' 8'
k $#5 &C! 7
^_' ) M&1 ) 3 305
@A AI
5 $
% )
5 u
^; <h_' 8 N )54> K ! ^_' 8
$
% )
5 V
^' $
3V
5
5
5 6
: M&1
/
NB )5 N YF
q&
: H&
5 T64 6: / !J ) 4 @C5 /@ K Wh: J nu; E ! Y 4
: $^&! 9@ 64 8=& $ 3' .q&: H& $% # {ZG 4u^; !
$
ADO
; &
k Y
=Gj!
k K /D1&
h!
H
U1
A' )@51
8
%
0
D1&h /
K Batch Update
5^?5 U1
A
5!
D1
Y
! I
5 9^_
5^
:> ^ ^5! >@ 39: / 1 1
B: H&O YDj! D $1 5% .i2I5 H ^ U1 A EcV
? Y&=;
./D1&h /
RS.LockType = adLockBatchOptimistic
: 'f h
U1 A 2G 5C' )+
RS.MoveNext
RS.Delete
RS.ActiveCommand = CN
RS.MarshalOptions = adMarshalModifiedOnly
RS.UpdateBatch
RS.MoveNext
Loop
End Sub