QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2660|回复: 0
打印 上一主题 下一主题

怎样用ADO打开一个带密码的Access库?

[复制链接]
字体大小: 正常 放大
韩冰        

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2005-1-26 12:38 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
<>  </P>
' L0 P) ]) x8 H; {% y5 f+ ?/ }<>Creates a new Recordset object and appends it to the Recordsets collection. </P>; H3 n  j4 g- M" h" }  _
<>  </P>" y: V8 b- t6 K! V1 f0 k# i
<>Syntax </P>
8 t' e0 E4 {2 V1 c<>  </P>
9 D: P/ H4 A" E7 K<>For Connection and Database objects: </P>) D1 U: f8 g) v
<>  </P>
, {0 F! c; v$ c. d3 U  Y/ N<>Set recordset = object.OpenRecordset (source, type, options, lockedits) </P>
" ?3 p( ^" |8 v8 C<>  </P>
7 x) ]2 p6 {# V# S7 Q( a) h  L<>For QueryDef, Recordset, and TableDef objects: </P>
! b4 H( o8 d$ y2 w5 V. n<>  </P>; U# R/ v7 S- M: g
<>Set recordset = object.OpenRecordset (type, options, lockedits) </P>/ o& o; l9 E9 B  l: Q; Y2 x
<>  </P>
% W  c8 L' r$ j7 S& x<>The OpenRecordset method syntax has these parts. </P>% p2 V! [+ b- y  v# n* ]4 P, ~, r
<>  </P>% O* q. I) V3 D( B  T; C% i& ?$ E
<>art    Description </P>8 U/ _2 t  U/ _. R/ V
<>recordset       An object variable that represents the Recordset object you wantt to </P>2 a; G& G% Z# Y4 I  n& A9 t
<>open. </P>( S3 Z# n8 h* d& f0 n1 u: x
<>object  An object variable that represents an existing object from which you </P>; F3 M0 w5 [0 L7 u) Q# G7 q
<>want to create the new Recordset. </P>
+ A$ V+ i1 L" ?( q7 L<>source  A String specifying the source of the records for the new Recordset. </P>9 V/ C( K$ m+ n& e- ?0 k5 n
<>The source can be a table name, a query name, or an SQL statement that </P>, x. k, p, s4 z5 u6 e2 y
<>returns records. For table-type Recordset objects in Microsoft Jet databases, </P>( B+ v$ Y& R6 C' z( R1 w; u
<>the source can only be a table name. </P>+ _1 C  i3 S7 O% W
<>type    Optional. A constant that indicates the type of Recordset to open, as </P>' \4 y% x# q& |: Q" }
<>specified in Settings. </P>/ m/ S9 ?9 |6 A. e& s
<>options Optional. A combination of constants that specify characteristics of </P>
: g5 l. }0 z; }$ j4 P  ]<>the new Recordset, as listed in Settings. </P>' j8 A8 A* ^; ~5 e% G5 l
<>lockedits       Optional. A constant that determines the locking for the Recordsset, </P>
8 h0 e1 M# \; o& @<P>as specified in Settings. </P>7 T  ~( A% K0 c. X0 S4 A9 a
<P>Settings </P>& G% y+ J# b2 b% g+ [
<P>  </P>- N2 B# H/ j8 G! z& f' j
<P>You can use one of the following constants for the type argument. </P>1 {* a" p; d1 Z9 A) N. G) \. E
<P>  </P>
% r* A" @3 X6 B5 t1 x& q* G<P>Constant        Description </P>' L3 B# X  O( `/ F( v
<P>  </P>: S3 Z4 s' Y! y/ d7 n& H
<P>  </P># I* S8 y8 K6 {% m. g2 X! t5 e
<P>dbOpenTable     Opens a table-type Recordset object (Microsoft Jet workspaces </P>
3 ~. x. g" B  a# V! B7 k" y<P>only). </P>0 x- v6 S* b' S/ v$ m0 R
<P>dbOpenDynamic   Opens a dynamic-type Recordset object, which is similar to an </P>% v* S* T' A  L% ]
<P>ODBC dynamic cursor. (ODBCDirect workspaces only) </P>
: c0 n. O" B( p<P>dbOpenDynaset   Opens a dynaset-type Recordset object, which is similar to an </P>
, q8 c  O0 K; u<P>ODBC keyset cursor. </P>( n" w0 ]! v0 I% \9 D  E, ~/ ?
<P>dbOpenSnapshot  Opens a snapshot-type Recordset object, which is similar to an </P>
; f, r% }% H7 i<P>ODBC static cursor. </P>9 G# u$ j6 X, V: D9 M
<P>dbOpenForwardOnly?Opens a forward-only-type Recordset object. </P>
3 w& l' Q! I2 x<P>Note   If you open a Recordset in a Microsoft Jet workspace and you don't </P>
; k3 J6 n5 R, t' T9 u- M  t$ m% \<P>specify a type, OpenRecordset creates a table-type Recordset, if possible. If </P>  r2 o) X. Y! R
<P>you specify a linked table or query, OpenRecordset creates a dynaset-type </P>. V2 g) g2 S' D% N: J) f4 x& P  k
<P>Recordset. In an ODBCDirect workspace, the default setting is dbOpenForwardOnl </P>
: X8 ~$ O9 ^) J$ f<P>y. </P>; M) ]) t, `; K- d, n7 N% E' r6 e
<P>  </P>% K. R8 _2 x% @6 ]' y/ |- y# ^
<P>You can use a combination of the following constants for the options </P># m6 l  P( J- G
<P>argument. </P>) }6 U0 B- g4 p3 y0 q/ X5 T
<P>  </P>" t7 A& H' }2 K- f0 t
<P>Constant        Description </P>
" a" P. M+ I; m4 k2 J3 f% g1 a<P>dbAppendOnly?Allows users to append new records to the Recordset, but </P>
2 }+ Q! I- `# w<P>prevents them from editing or deleting existing records (Microsoft Jet </P>
- p2 k! y  L# g<P>dynaset-type Recordset only). </P>
6 K( c$ z) s1 D- A<P>dbSQLPassThrough?Passes an SQL statement to a Microsoft Jet-connected ODBC </P>
, r' i: m6 ]7 b3 a; y+ P<P>data source for processing (Microsoft Jet snapshot-type Recordset only). </P>
4 U' O: q) _5 ~! f- z2 h& ^<P>dbSeeChanges    Generates a run-time error if one user is changing data that </P>
& B* O( k" N$ p5 e+ N& _* F8 O2 Z<P>another user is editing (Microsoft Jet dynaset-type Recordset only). This is </P>* M! v; G- w% C" n+ w+ Y9 u
<P>useful in applications where multiple users have simultaneous read/write </P>
9 N. G8 p( L9 s3 s8 M9 h<P>access to the same data. </P>  F) {& O- H! H+ J3 W
<P>dbDenyWrite?Prevents other users from modifying or adding records (Microsoft </P>
. _4 W; l3 Q* N* ~2 z7 c4 s<P>Jet Recordset objects only). </P>
8 E. }7 h' @' q7 f6 R+ p+ S<P>dbDenyRead?Prevents other users from reading data in a table (Microsoft Jet </P>) N/ _0 V8 [7 A6 w
<P>table-type Recordset only). </P>
. e% u0 e7 X- a! c/ N<P>dbForwardOnly?Creates a forward-only Recordset (Microsoft Jet snapshot-type </P>' Z- M, _# {$ M* i3 c7 j  ]! x& y1 ^8 l
<P>Recordset only). It is provided only for backward compatibility, and you </P>
! l, c& k1 i) I4 e/ y. i<P>should use the dbOpenForwardOnly constant in the type argument instead of </P>3 Z) f2 ^$ P' m
<P>using this option. </P>+ R# }) o$ f; Z
<P>dbReadOnly?Prevents users from making changes to the Recordset (Microsoft Jet </P>
" b5 G/ Q/ K1 I' _# R9 H9 r<P>only). The dbReadOnly constant in the lockedits argument replaces this </P>
6 r5 o8 [3 v& _8 X<P>option, which is provided only for backward compatibility. </P>  s* r( D" ]3 a* h
<P>dbRunAsync      Runs an asynchronous query (ODBCDirect workspaces only). </P>
* A7 J- l/ a( k9 b* _<P>dbExecDirect?Runs a query by skipping SQLPrepare and directly calling </P># _5 I8 Z- [# C. y  Q4 Y
<P>SQLExecDirect (ODBCDirect workspaces only). Use this option only when you抮e </P>
6 P$ a7 j5 c8 O) H' v3 k<P>not opening a Recordset based on a parameter query. For more information, see </P>2 I9 Q, K; y1 W2 E- g
<P>the "Microsoft ODBC 3.0 Programmer抯 Reference." </P>
8 O$ |- e8 r+ T3 D<P>dbInconsistent?Allows inconsistent updates (Microsoft Jet dynaset-type and </P>
0 G& ?) u0 Q' `<P>snapshot-type Recordset objects only). </P>
) L0 X' ^8 O9 w8 i  t1 \<P>dbConsistent?Allows only consistent updates (Microsoft Jet dynaset-type and </P>7 u+ x5 w* c: D6 }$ {& V  Y
<P>snapshot-type Recordset objects only). </P>
6 l/ k0 F, {% K<P>Note   The constants dbConsistent and dbInconsistent are mutually exclusive, </P>0 t" X' g$ n8 W8 S1 C* p! I
<P>and using both causes an error. Supplying a lockedits argument when options </P>
/ M0 Z! T/ M/ ^# I6 F0 F' O<P>uses the dbReadOnly constant also causes an error. </P>
( y7 u+ \% h: r6 o<P>  </P>/ V2 ~- l8 U5 p; i+ s- [2 k
<P>You can use the following constants for the lockedits argument. </P>
9 D- n/ f8 u6 {% ^/ E<P>  </P>; s2 X$ A( ]3 Y) I
<P>Constant        Description </P>
2 w# T! r! {7 _) J<P>dbReadOnly      Prevents users from making changes to the Recordset (default for </P>
4 J1 n% ?' f) F# G7 H. ]( x; U<P>ODBCDirect workspaces). You can use dbReadOnly in either the options argument </P>
4 _3 U7 B9 L* L3 Q" j/ k8 H<P>or the lockedits argument, but not both. If you use it for both arguments, a </P>2 W& m/ B! \3 M# b3 ?3 _: m" I0 j
<P>run-time error occurs. </P>
. K. R  j: O; C. C. J<P>dbPessimistic?Uses pessimistic locking to determine how changes are made to </P>3 G8 c3 f( D# S
<P>the Recordset in a multiuser environment. The page containing the record </P>
  e. B/ A0 d: M. b<P>you're editing is locked as soon as you use the Edit method (default for </P>! `# |4 r; A3 m, ^- ]" u3 x
<P>Microsoft Jet workspaces). </P>& v0 {6 z- M( m8 l) B8 i3 o* G
<P>dbOptimistic?Uses optimistic locking to determine how changes are made to the </P>
8 F0 K: k0 e: q& Q: a<P>Recordset in a multiuser environment. The page containing the record is not </P>5 n) D. f4 j9 }8 W2 ?' I  @( c
<P>locked until the Update method is executed. </P>
1 w, q1 O, X: U! Y. ~<P>dbOptimisticValue?Uses optimistic concurrency based on row values (ODBCDirect </P>$ O* y6 v3 \. g; }
<P>workspaces only). </P>
- ~: Z. l1 O* T$ C<P>dbOptimisticBatch?Enables batch optimistic updating (ODBCDirect workspaces </P>. ~6 C5 u( O+ o# l1 l) F2 j
<P>only). </P>2 f  c! ?2 I) P
<P>Remarks </P>: |* T& Q; v0 N) s4 _  A
<P>  </P>
  F( w# @9 b2 l( v<P>In a Microsoft Jet workspace, if object refers to a QueryDef object, or a </P>
# _) x! p* t7 z+ v+ f<P>dynaset- or snapshot-type Recordset, or if source refers to an SQL statement </P>% u% T. @- X( H& X$ T, O2 s
<P>or a TableDef that represents a linked table, you can't use dbOpenTable for </P>% s2 H+ J$ I! V" j3 |3 t5 e
<P>the type argument; if you do, a run-time error occurs. If you want to use an </P>
3 L. n' h; u  v<P>SQL pass-through query on a linked table in a Microsoft Jet-connected ODBC </P>% j4 f  W" P! P; Z# m, v$ v
<P>data source, you must first set the Connect property of the linked table's </P>
: x" n% g8 P7 K" R, n3 Y& y0 M/ h8 w<P>database to a valid ODBC connection string. If you only need to make a single </P>+ Z+ z# F# v1 K1 f1 n: y
<P>pass through a Recordset opened from a Microsoft Jet-connected ODBC data </P>$ B% n* q7 e- L, Z$ V& x
<P>source, you can improve performance by using dbOpenForwardOnly for the type </P>9 h" C% |8 m5 }2 i  E  }* l; X
<P>argument. </P>8 ?: S. D. x+ c
<P>  </P>
$ P; _# B) {7 |/ E7 }<P>If object refers to a dynaset- or snapshot-type Recordset, the new Recordset </P>0 b, y- d: E9 c8 N. I4 E
<P>is of the same type object. If object </P>( O! `: R% K0 m* A! `+ J
<P> refers to a table-type Recordset object, the type of the new object is a </P>! M9 d( _! D) C( e, |
<P>dynaset-type Recordset. You can't open new Recordset objects from forward-only </P>  p; i7 W  @9 d
<P>杢ype or ODBCDirect Recordset objects. </P>$ P7 w9 R; ]# e7 z) O( N
<P>In an ODBCDirect workspace, you can open a Recordset containing more than one </P>; @) T  P4 C% F" N- h& e
<P>select query in the source argument, such as </P>! x  z: B& W  F7 v
<P>  </P>
2 m- z  m$ N% {$ }9 W0 G<P>"SELECT LastName, FirstName FROM Authors </P>, M7 C7 h2 Q% E9 M& O" I5 f
<P>WHERE LastName = 'Smith'; </P>
1 y- t) ?/ Y  x<P>SELECT Title, ISBN FROM Titles </P>5 V, Y" U. X" o, j% @, e
<P>WHERE ISBN Like '1-55615-*'" </P>
! q0 Z3 D7 F# n) l; R+ O<P>  </P>
5 P( F; X0 u3 _! x- U+ E" ~0 P6 x$ |<P>The returned Recordset will open with the results of the first query. To </P>
/ {% b  Q6 V/ |& V9 w) z3 p7 G<P>obtain the result sets of records from subsequent queries, use the </P>
, o( M3 J0 X! ]<P>NextRecordset method. </P>
6 j, [9 o7 U% g# S( z8 R9 {$ C<P>  </P>
4 J5 ]( x6 ~. O+ P# Q$ C& Q& j<P>Note   You can send DAO queries to a variety of different database servers </P>( \4 e5 X: ?) T1 R9 p+ h$ c
<P>with ODBCDirect, and different servers will recognize slightly different </P>, C1 h+ l# a2 ^5 k
<P>dialects of SQL. Therefore, context-sensitive Help is no longer provided for </P>( i; }, p- r% ?1 M) Q% C9 l
<P>Microsoft Jet SQL, although online Help for Microsoft Jet SQL is still </P>
4 c! Y& `: m# `) @. r# f' }+ r<P>included through the Help menu. Be sure to check the appropriate reference </P>' ~3 F; d+ U( M- m* v8 W8 }8 K
<P>documentation for the SQL dialect of your database server when using either </P>- p- C4 t9 j. _! K$ o3 N
<P>ODBCDirect connections or pass-through queries in Microsoft Jet-connected </P>
6 ~9 F* L' y0 i% {, K! \2 T5 G/ G% T0 \<P>client/server applications. </P>3 f$ c; s7 o0 h3 A
<P>  </P>; n& C6 C6 K  H. {5 f9 P& u
<P>Use the dbSeeChanges constant in a Microsoft Jet workspace if you want to </P>! F0 i4 C. z& o; v0 ^7 c3 w0 W  r& ?# ]
<P>trap changes while two or more users are editing or deleting the same record. </P>
  n, [  Y% @9 ^* T8 H<P>For example, if two users start editing the same record, the first user to </P>
% P5 h, r, H4 W. q: o<P>execute the Update method succeeds. When the second user invokes the Update </P>
; F& C8 x" T6 `6 q. }1 C<P>method, a run-time error occurs. Similarly, if the second user tries to use </P>
$ g5 i  v, @& i2 I* v3 q<P>the Delete method to delete the record, and the first user has already </P>, Y1 h# y$ E$ J( i' b9 r3 n
<P>changed it, a run-time error occurs. </P>
: H& [  O) E# P<P>  </P>
( L4 M# N- V" s  X+ C" R<P>Typically, if the user gets this error while updating a record, your code </P>6 E; Q% o. p) b$ N
<P>should refresh the contents of the fields and retrieve the newly modified </P>- Z- ~, S( x5 K$ O5 x6 M' G* G! `
<P>values. If the error occurs while deleting a record, your code could display </P>% z  `4 r& {& f
<P>the new record data to the user and a message indicating that the data has </P>" W; S( D7 ^7 \
<P>recently changed. At this point, your code can request a confirmation that </P>3 \$ v/ C- U+ K, G
<P>the user still wants to delete the record. </P>
( K, U7 m- [: H' v9 o; j- N9 D' R" O<P>  </P>
9 M/ T/ M' d6 n% w<P>You should also use the dbSeeChanges constant if you open a Recordset in a </P>
7 ~2 |' o( [7 Q& q4 @4 Q: _<P>Microsoft Jet-connected ODBC workspace against a Microsoft SQL Server 6.0 (or </P>9 K  Z, J& B% r5 y3 f
<P>later) table that has an IDENTITY column, otherwise an error may result. </P>- |  f$ F$ x! f& U! Y3 [5 y0 y
<P>  </P>7 u4 C% S9 C9 U. _& f+ X
<P>In an ODBCDirect workspace, you can execute asynchronous queries by setting </P>
+ E" S$ Z3 `, w( W2 F. V5 g<P>the dbRunAsync constant in the options argument. This allows your application </P>$ e* B4 E+ x5 @. t
<P>to continue processing other statements while the query runs in the </P>
1 w5 M& K/ t3 G<P>background. But, you cannot access the Recordset data until the query has </P>
* ~2 C1 @/ ]9 j" u: i+ m<P>completed. To determine whether the query has finished executing, check the </P>
$ O3 S4 c- Q( W# H<P>StillExecuting property of the new Recordset. If the query takes longer to </P>  E! s/ D5 `% M6 m, n7 Y
<P>complete than you anticipated, you can terminate execution of the query with </P># m" m3 P6 ]0 _( u: |
<P>the Cancel method. </P>
% D$ i' ^+ a# i3 z<P>  </P>' P/ J/ ]; q1 J' Q. Z
<P>Opening more than one Recordset on an ODBC data source may fail because the </P>
8 A$ o" R6 x2 U5 R<P>connection is busy with a prior </P>5 Y0 o/ T! N  Y
<P>OpenRecordset call. One way around this is to use a server-side cursor and </P>
2 x7 x2 J. E: P3 B<P>ODBCDirect, if the server supports this. Another solution is to fully </P>
9 b$ s( N( a8 n, r5 p$ x<P>populate the Recordset by using the MoveLast method as soon as the Recordset </P>
: `3 \" |6 n' M4 Q! O<P>is opened. </P>- J5 I: f5 n. W* ]( c% }2 G. }
<P>  </P>/ m# {5 d3 x' `% k/ n, g8 L
<P>If you open a Connection object with DefaultCursorDriver set to </P>
( ~, l  D$ i  a7 v5 X; Q<P>dbUseClientBatchCursor, you can open a Recordset to cache changes to the data </P>
) ?  K- Q9 [9 M* C<P>(known as batch updating) in an ODBCDirect workspace. Include dbOptimisticBatc </P>
3 n$ d" X, n/ F7 F! |4 ~8 W<P>h in the lockedits argument to enable update caching. See the Update method </P># y: a! j7 k9 @6 G9 d4 c
<P>topic for details about how to write changes to disk immediately, or to cache </P>
. M5 [3 _: V. }- N. S7 A; s  O<P>changes and write them to disk as a batch. </P>
* K% b6 Z5 H, R' Y5 y<P>  </P>
) n) G+ \( {) F3 _$ I" z$ d+ [* T5 E<P>Closing a Recordset with the Close method automatically deletes it from the </P>
& P3 A7 k  x5 C: c
2 u. X- A+ w6 t& j2 K<P>Recordsets collection. </P>
% d3 |/ s* ~! ^  J* n<P>  </P>
# n6 |' \2 ]# Z2 i6 U<P>Note   If source refers to an SQL statement composed of a string concatenated </P>
( ]2 e6 o6 u0 R: x( y4 m, T<P>with a non-integer value, and the system parameters specify a non-U.S. </P>* v- u  U7 A/ r4 q
<P>decimal character such as a comma (for example, strSQL = "PRICE &gt; " &amp; </P>
+ u' [# B% R1 |) ]9 l( N- j4 }<P>lngPrice, and lngPrice = 125,50), an error occurs when you try to open the </P>
- [, T+ b5 e# L8 v<P>Recordset. This is because during concatenation, the number will be converted </P>/ b. p9 \+ I# P) l
<P>to a string using your system's default decimal character, and SQL only </P>% Z5 H2 c! u  F& t4 F/ I3 @9 Z
<P>accepts U.S. decimal characters.</P>
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-10-9 07:00 , Processed in 0.430284 second(s), 52 queries .

回顶部