    68   , 

     4.       

      Data        
          . 
    ,      ,  
         
,       . 
              

    4.1.    

           ,    
      . 
     Microsoft Access       
1    ,   .   
< ,.  .     
          
   . 
     ,         

    4.1.1.      

       Microsoft Jet,   ,   
 ,       .   
          .	~ 
       Nwind    . , 
  Quantity (  )   Order Details 
( nosi/ ) ,      0. 

    ,    

     Visual Basic       
.   Field  Recordset   ,    . 
           Microsoft Jet: 
ValidationRule      . 
ValidationText       ,      
 HI  . 

          Immediate    
ValidationRi ValidationText   Quantity: 

    Sub   DisplayRule() 
    Set   rstOrders   =  dbDbl.OpenRecordset("Order   Details") '    
  ">0" Debug.Print   rstOrders("Quantity").ValidationRule 


     4.    	69 

    '      "Quantity must  be   greater   than   0" Debug.Print  
rstOrders("Quantity").ValidationText End  Sub 

    :     ,  
  Validat/onRule  Field. 


    ,     

         ,   
.  ValidateOnSet  , .  
 . 
     ValidateOnSet  True,      
 ,     : 

    recorders("Quantity").ValidateOnSet   =  True 
    recorders("Quantity").Value   =   0   '             
    recorders.Update 
     ValidateOnSet  False,       
Update,    :	
    recorders("Quantity").ValidateOnSet   =   False 
    recorders("Quantity").Value  =   0 
    recorders.Update   '             

       

    ,    ,   
       . 
   ynj3aBiiejHtig__Data     
,   jlrror^   Data    
  ValidationText   .   
      Data ,   
 . 
           ,  
     Error. ,     
 ,     ,       
 JoreColor  ( ),  ,   .	"~* 
             , 
  .    ^ 
     . 
       ,    ,    : 

    Private Sub cmdUpdate_Click() On  Error  GoTo Update_err rs.Edit 
    rs ("Orderld")   =   txtOrder.Text rs.Update 
    Exit   

    Sub Update_err: 
    If  Err.Number  =   3316   Then   '       
   IblError.Caption=  Err.Description rs.Cancel
   Update Exit   
   Sub End   
   If End  Sub 


    70   , 

     Description  Err    ValidationText. 
       Edit  AddNew      
    CancelUpdate.       
, (        . 

    4.1.2.   

        ,    
           
,    . 

        

     Microsoft Jet       
  Jet. 
    ,    Nwind   Customers ()  
 Orders ()   "--"   
 .        
(CustomerlD)    ,     , 
  . 

    :        
,   CreateRelation Visual Basic,  ,  
Microsoft Access. 

     CreateRelation 

     CreateRelation    Relation ().  : 

    Set  = 6a3a_dawHb/x.CreateRelation (, , 
__, ) 


     : 

    	                                                           

    	   ,         ! 
Relation.                                                                      
    _	 ,   Database,  - 
    Relation.                                        
    	. String Variant,     
Relation.                                                                      
    	. String Variant,     
   .      RelationB   
 ,   .    
    _	. String Variant,    
   .      Relation !! 
  ,   .             
    	.    ,  
   .                                       
     Relation   Microsoft Jet   
     TableDef  QueryDef.   
     Attributes  Field, 
Relation  TableDef. 


     4.    	71 


        

            
  dbRelationUpdateCascade  dbRelationDeleteCascade. 
   ,    Microsoft Jet  
           
     . 
    , ,      Customers  
Orders.    dbRelationDeleteCascad,  Microsoft Jet 
       Orders  ,  
      Customers. 

    :  Microsoft Jet      
    .      
 ,       , 
   . 


        

           
 .         . 
       ,      
,    ,      : 

    Private   Sub   cmdDelete_Click() On   Error   GoTo   Del_Err rs.Delete rs.MoveNext 
    FillFields   '             Exit   Sub 
    lel_Err: 
    '         If  Err.Number   =   3200   Then 
IblError.Caption  =  _ 
    "           " Exit   Sub Else 
    '           ... End   If End  Sub 

     ,  ^ H^^aj^jj^j^^	 .    
    . 

    4.1.3.  

       -,      
.        
  . 
       ,   .    
  ,     . 

      

    ,   ,    Workspace.  
 Begin-Trans   .      
  ,    Rollback    
 . 


    72    

       ,   Commit!rans  
     .     
       Orders  Order Details: 

    Sub  cmdAddOrder_Click   () 
    On  Error   GoTo  AddOrder_Err 
    DBEngine.Workspaces(0).BeginTrans 
    db.Execute" "TNSERT   INTO  Orders...    .",   dbFailOnError 
    db.Execute   "INSERT   INTO    [Order   Details]...."   ,    dbFailOnError 
    DBEngine.Workspaces(0).CommitTrans 
    IblStatus.Caption  =   "   " 
    Exit   Sub 
    AddOrder_Err: 
    IblStatus.Caption = Err.Description 
    Msgbox "He    " 
    DBEngine.Workspaces(0).Rollback 
    Exit Sub	"     """  " End Sub 

         

            
,      SQL UPDATE,   
  .      
  ,  !   .	J ~~ 
        SQL UPDATE   : 

    DBEngine.Workspaces(O).BeginTrans 
    '             10% 
    stSQL   =   "UPDATE   Products   SET    [Unit   Price] 
    db.Execute   strSQL,   dbFailOnError 
    DBEngine.Workspaces(0).CommitTrans 
    =   [Unit   Price]    *   0.1' 

    :   Workspace    . 
        .~ , 
  -     .  , 
  ,  ? v    ,   
  Workspace. 


    4.2.    

            
^ >.      .    
      . : 
               .   
) Microsoft Jet   ,   
!    .   
    ,    ; 
         ,      
  .     ,  
  ;      MeTOflaJEdit ,    
Microsoft Jet  ; 
      ,    
~1      , 
Microsoft Jet  . 
             . 


     4.    	73 

    4.2.1.      

      ,   ,   
      ,   
   .     
      .     
,       . 
        ,     OpenRecordset. 
        Orders    
.     ,    
   ,     : 

    Set  recorders   =  dbDbl.OpenRecordset  _ 
    ("Orders",   dbOpenTable,   dbDenyRead  +   dbDenyWrite)	^'?t    f J 
'f*;." !/&/ 

        Orders     
 .       ; 
       ,    
 : 

    Set  recorders   =  dbDbl .OpenRecordset  _	.'. ("Orders",   
dbOpenTable,   dbDenyWrite)                                                    
           '  ' 

              . 
      ,     . 
      .	/ 

    Set  recorders   =   dbDbl.OpenRecordset  _ ("Orders",   dbOpenTable,   dbReadOnly) 

            .  
    ,      
   .      
 _. 

    Set  recorders   =  dbDbl.OpenRecordset  _ 
    ("Orders",   dbOpenDynaset,   dbAppendOnly) 

          ,   
     dbAppendOnly.  
dbDenyRead       table. 

    4.2.2.     Microsoft Jet 

            
,      . 
       Microsoft Jet    
.      2048  (2).  Visual Basic 
 ,   ,     
   . 

    :       ,   
   Microsoft Jet.      
 ODBC       
 . 


        

      LockEdits  Recordset ,  
     . 


    74    

      LockEdits  True,   
.  ,  ,   , , 
  '  Edit.  ,   
 Update. 
           
      , ,    
  Edit, |     . 
      LockEdits  False,   
.  ,  ,     
 . 
            ,  
!     Edit   
. ,    ,  
  <  ,     Update. 
             
 ,   LockEdits  False. 

     

       Microsoft Jet  ,   
.1    . ,  
.,      . 
      Idle  DBEngine   dbFreeLocks,  
^     Microsoft Jet  
?~      .    : 

    DBEngine.Idle  dbFreeLocks 


    4.2.3.    

         ,  
    : 
    3260   Couldn't update; currently locked (   
      Edit,     
.  /          
Edit   !   . 
    3186   Couldn't save; currently locked (    
).         
Update,    .       
     Update     
 . 
    3197   Data has changed; operation stopped ( ;  
     Edit  Update,   
 ^         
.         
  . 

         Edit 

         ,   
    Edit: 

    Private  Sub  cmdEdit_Click() On  Error  GoTo   HandleError rs.Edit 


     4.    	75 

    ButJ^nEditAddMode Exit   Sub	( 
    HandleError: 
    Select   
    Case   Err.Number 
    Err   
    Select  Case   .Number 
    Case   3260   '         
    MsgBox   "    .      " 
    Case   3197   '      
    MsgBox   "          " 
    rs.Bookmark  =   rs.Bookma,rk '      
    "FTllFields   '        
    rs.Edit 
    Case  Else 
    MsgBox  Err.Number   &   
    ":"   &   
    Err.Description 
    End  Select 
    End  Sub 

      ,   ,   
  Edit,  .   ,  
   . 

    OnepSTOgjecOrders.bookmark = recorders.bookmark   
   -    ' . 

         Update 

        ,     
  Update: 

    Private Sub cmdSave_Click() Dim answer As Integer On Error GoTo HandleError 
    rs.Fields("Category Name") = txtCategoryName.Text 
    rs.Fields("Description") = txtDescription.Text rs.Update 
    rs. Bookmark = rs.LastModJJg-ied "FiTTFigia'S' ButtonNavigateMode 
    'Exit Sub
    HandleError: 
    Select 
    Case Err.Number 
    Case 3260 
    MsgBox "   . " & _ 
    "       ." 
    Case 3197 
    answer = MsgBox ("    . " & 
    "  ? ", vbYesNo) 
     If answer = vbYes Then Resume 
     Else rs.Bookmark = rs.Bookmark '    
     '   
     cmdCancel_Click 
     End If 
     Case Else 2 


    76     

    MsgBox Err.Number & End Select End Sub 
    " & Err.Description 

           

               
 dynaset  ,        . 
               _, 
         .     '   ×""~ 
            ,  
     dynaset.	"    ~~          -
       ,   ,  
    : 

    Private Sub cmdMoveFirst_Click() 
    On Error GoTo Err_Movefirst: \rretry_MoveFirst: rs.MoveFirst 
    txtLName.Text = rs("Last Name") ' Exit Sub Err_Movefirst: 
    If Err.Number = 3167 Then 
    '  ,   '  ^  dynaset rs . Delete  
      "*' Resume retry_MoveFirst Els'e 
    MsgBox Err.Description Exit Sub End If End Sub 

       

    4.3.     

    Visual Basic       
--  (ISAM), ,  dBASE, Paradox  
Microsoft Visual FoxPro.     Visual Basic  
 ISAM. 
    Visual Basic         
,      . 

    :  ,      
  Microsoft Jet,      Visual Basic. 

       ,     Visual Basic  
 ,    ISAM    . 

    4.3.1.     

               
 ,    . 


     4.     77 


        Microsoft Jet 

            Microsoft Jet,  
   _ .     
      Microsoft Jet.   
,           Microsoft 
Jet,   ,       
..  table. 
         Microsoft Jet    
    Microsoft Access.     
 ,   CreateTableDef Visual Basic. 
      CreateTableDef : 

    Se( o6beKm_TableDef = _.\.\, , , )	~ 

     : 
    	 
    o6beKm_TableDef	   ,         
 TableDef. 
    6_	 ,   Database, 
     TableDef. 
    	.  String Variant,    
 TableDef. 
    	.    ,  
      TableDef. 
    	.  String Variant,    
      .   
     TableDef. 
    	.  String Variant,   
      . 
    ? 

    :       ODBC,   
  Microsoft Jet, /__,_   
^ ,     {_ 
  " Microsoft Jet.   ,  ,  
      . 

      

           
 ,    OpenDatabase   
Connect   Data. 
            
OpenDatabase  OpenRecordset: 

    Sub  OpenTable () 
    Dim wspCurrent  As  Workspace Dim dbCurrent  As   Database Dim  
rstProducts  As   Recordset 
    Set  wspCurrent  =  CreateWorkspace ("New Workspace",   "Admin",   "",   _ 
    dbUseJet)	9 Set dbCurrent = wspCurrent.OpenDatabase("C:\VB\Nwind. mdb") f 
Set rstProducts = dbCurrent.OpenRecordset("Products", dbOpenTable) 


    78    . 

    rstProducts.MoveLast 
    MsgBox "  " & rstProducts.RecordCount & " " 
    rstProducts.Close dbCurrent.Close wspCurrent.Close 
    End Sub 

    4.3.2.     ISAM 

      .   ISAM,    
  6_  OpenDatabase (  DatabaseName 
 ! Data)  ,    ISAM.   
        (, \\server1\share1). 
      Connect  Database    Data  
    .     Connect 
  f  ISAM    : 

      		                           .    | 

      Microsoft Jet	[_];	:\\_.' | 
    dBASE III	dBASE III;	                              
    dBASE IV	dBASE IV;	                            | 
    dBASE 5	dBASE 5.0;	:\                             
    Paradox 3.x	Paradox 3.x;	                              
    Paradox 4.x	Paradox 4.x;	                              
    Paradox 5.x	Paradox 5.;,:\, 
    FoxPro 2.0	FoxPro 2.0;	                            ! 
    FoxPro 2.5	FoxPro 2.5;	                             ^ 
    FoxPro 2.6	FoxPro 2.6;	                              
    Excel 3.0	Excel 3.0;	:\\_.!     
    Excel 4.0	Excel 4.0;	:\\_.1    | 
    Excel 5.0 or Excel 95	Excel 5.0;	:\\ _.!    
    Excel 97	Excel 97;	:\\ _.!   | 
    HTML Import	HTML Import;	:\\ _         
    HTML Export	HTML Export;	                             ' 
    Text	Text;	                              

          Microsoft Visual FoxPro: 

    Public   Sub  OpenFoxProTable () Dim  dbFox  As   Database Dim  
recAccounts  As   Recordset Set   dbFox  =  OpenDatabase  __ 
    ("\\FoxPro\Data\AP",   False,   False,   _ "FoxPro   5.0;") 
    Set   recAccounts   =  dbFox.OpenRecordset("Accounts") End  Sub 


     4.    	79 

        ISAM     .mdb, 
    TableDef    Connect  
SourceTableName  TableDef. 
       ^  Microsoft Jet   
Visual FoxPro.      flaHHbix_Mi?rosof^Jet,  
 : 

    Dim  tbdAttach  as   TableDef 
    Set  dbDbl=OpenDatabase   ("Dbl.mdb") 
    Set   tbdAttach  =  dbDbl.CreateTableDef    ("Attached  FoxPro  Table") 
    tbdAttach.Connect   =   "FoxPro~5T6;DATABASE=\\FoxPro\AP" 
    tbdAttach.SourceTableName  =   "Accounts" 
    dbDbl.TableDefs.Append tbdAttach 

          Microsoft Jet   , 
  : 

    Set  dbDbl   =  OpenDatabase   ("Dbl.mdb") 
    Set   recAttach  =  dbDbl.OpenRecordset    ("Attached  FoxPro   Table") 


    4.3.3.     

     Visual Basic,    ,    
   . 
              
 : 
       -  Open, Get, Put, Input #, Print #, 
Write #; 
           .     
     Microsoft Access. Microsoft Access  
  , ,    ,  
   ; 
            .   
 ,          
  . 
            
-         : 

    Private Sub Commandl_Click() Dim fhandle As Integer f handle = FreeFileO 
    Open "TextFilel" For Input As fhandle Do 

    '       
    '   SQL 
    '   SQL INSERT Loop Until EOF(fhandle) End Sub 

    4.4.     ODBC 

       3.5     /  
ODBCDirgct.         ODBC 
         
  Microsoft Jet. 


    80     ODBC 

    4.4.1.    ODBCDirect 

        ODBCDirect      
    :     
     ODBCDirect. 

        

           ODBCDirect,  
  DefaultType  DBEngine.     
  dbUseODBC.       
      Workspace.   
      flajHjjbj^jyiicrosqft Jet ( 
7~          Microsoft Jet). 
            ODBCDirect: 

    DBEngine.DefaultType = dbUseODBC 


        

        ,    
 CreateWorkspace. ,     ODBCDirect: 

    Dim  wspODBC  As  Workspace 
    Set  wspODBC  =  DBEngine.CreateWorkspace  _ 
    ("NewODBCWorkspace",    "Admin",    "",   dbUseODBC) 


    4.4.2.      

             
Microsoft Jet.     (,   
ODBCDirect),     . 
           Connection, 
  ,      Microsoft Jet   
Database.         
 . 

      

       Connection   . 
      Set. 
            : 

    Dim conNewConnection As Connection 
    Set conNewConnection = OpenConnection _ 
    ("New", dbDriverPrompt, false, _ 
    "ODBC;DATABASE=pubs;UID=sa;PWD;DSN=SQLServer") 

      OpenConnection: 

    Set  = _.OpenConnection (, , 
__, _) 

           OpenConnection: 

    	 
    	   Connection,    . 


     4.     81 

    	 

    _	.    Workspace, 
    Workspace,     . 
    	 ,      ODBC (DSN) 
 ^ _^.      
Name  Connection.    DSN     
_-,     ODBC DSN,   
  Name  Connection. 
    	    . ,  
          
 . 
    __	True  False.  True,     
  .     False. 
    _	  ODBC.                                  

            : 

    	 

    dbDriverNoPrompt	ODBC Driver Manager   ,  
      _˸7 
    dbDriverPrompt	ODBC Driver Manager      ODBC Data 
Sources,   ""1\   _. 
      DSN ,  
   DSN,     DSN. 
    dbDriverComplete	  _    
   , ODBC Driver Manager   
  _.     ,     
   dbDriverPrompt.     . 
    dbDriverComplete-Required	      
dbDriverComplete,   ,   ODBC   
  ,     . 
    dbRunAsync	 .     
    . 

    4.4.3.    

          ODBCDirect 
-. >|_ flaHH^b]x_MjcjrosofUet.    
 ODBCDirect     ODBC  OpenRecordset 
   Recordset    ,     
     Microsoft Jet. 
          ODBCDirect     
        SQL Server: 

    Sub  OpenODBCDirectRecordset() 
    Dim  wspODBC  As   Workspace 
    Dim  conODBC  As   Connection 


    82   


    Dim  rstODBC  As   Recordset 
    Set  wspODBC  =  CreateWorkspace  _ 
    ("",   "sa",    "",   dbUseODBC) Set   conODBC  =  wspODBC.OpenConnection  _ 
    ("NewConncetion",   dbDriverNoPrompt,   False,   _ 
    "ODBC;DATABASE=pubs;UID=sa;PWD=;DSN=SQLServer") Set   rstODBC  =   
conODBC.OpenRecordset  _ 
    ("SELECT   *   FROM  authors  WHERE   au_lname  =   'Green'",   _ 
    dbOpenSnapshot,   0,   dbReadOnly) rstODBC.MoveLast MsgBox   "     
         "   &  _ 
    rstODBC.RecordCount   &   "   ." 
    rstODBC.Close conODBC.Close 
    wspODBC.Close 
    End  Sub 

     ,        
 ( ,    ,     
Microsoft Jel   .   3. 

     Open Recordset    ODBCDirect 

    '    OpenRecordset,   
   ODBCDirect: 
     
    dbOpenDynamic	    dynamic,   
  ,       
    .   dynamic     
     . 
     
    dbRunAsync	  . :    
 ,   
        ,     
 .       . 
     
    dbOptimisticValue	  ,   
 . 
    dbOptimisticBatch	   . 
    :    ODBCDirect    . 
          
 ODBCDirect,     dbOptimistic. 

    4.5.   

      -       
 .       
  : 


     4.    	83 

         .   SQL  
,  . ,   ,   
,  ,   ,     ; 
         .    
   dynaset  1__ .   
         
.      prpa|j^eHjHbj^ejj^a6pp_bi   
   ; 
                
.      ,   
,  . ,    "", 
    ,     "": 

    strSQL = "SELECT [LastName], [FirstName] FROM Employees   "   &   _ 
    "WHERE    [LastName]    Like   "   & 
    "'"   &   txtName.text   &   '""   &   
    "*"   '      
    Set   recEmployees   =  dbDbl.OpenRecordset    (strSQL,dbOpenDynaset) 

           .  
    ,      
.   ,        
 Microsoft Jet    . 
         ,      
 .       ,  
    dbAppendOnly; 
        .    
    - ,  ,   
 ,     .     
 ; 
         .     
,    SQL  .   
-,       .  
,    ,     .mdb,   
   .  ,     
  SQL   , ,    
,   -"     Update; 
        . ,  ,  
   ODBC     .mdb.    
     .mdb, , , 
  . 


    84  Crystal Reports 

     5.       Crystal Reports 

            
,       .    , 
 ,      -, , 
   . ..      
   .         
    ,     
.      Visual Basic. 
       Visual Basic 5.0    Crystal 
Reports 4.6  Seagate Software.  ,      
       Microsoft Visual 
Basic (^_._  ,  Borland Visual 
dBase  .),        '. " * 
    Crystal Reports  Visual Basic   ,  
           
.mdb     SQL/ODBC,     
    ,       
Visual Basic. 

     Crystal Reports Pro - [Untitled Report II 
    -l.PI.xl 
    1 file   Edit   insert   Format   ?>atabase   Fjepoft   Window   
Help	-Jfl] x| 
    ~I\ 
    Design j Preview | 
    Today 18:45     Close ] 
    ( 
    !.  i . li 
    .   fr.   ,   .   IB   i  .   IU 
    Title 
    Page header 
     ...."     l               >	Au 
    tti:AtUD-A LJ			Insert 1   Done j    ESrowse Field Data...	/ 
    1               d		jd-J':	 in _	5 

    . 5.1.   Crystal Reports. 


    5.1.  Crystal Reports 

    Crystal Reports       
,       .  
    : 

