Chú ý: Mã mẫu trong bài viết này sử dụng Microsoft Data Access Objects. Đối với mã này để chạy cho đúng, bạn phải tham khảo Microsoft DAO Object Library 3.6. Để làm như vậy, Tài liệu tham khảo trên menu Tools trong Visual Basic Editor, và chắc chắn rằng Microsoft DAO 3,6 Object Library hộp kiểm tra được chọn. Bài viết này mô tả làm thế nào bạn có thể sử dụng các đối tượng truy cập dữ liệu (DAO) để nhập khẩu tất cả các đối tượng từ một cơ sở dữ liệu Microsoft Access vào cơ sở dữ liệu Access hiện tại. Trong một số trường hợp, mã này có thể được sử dụng để phục hồi các đối tượng cơ sở dữ liệu từ một cơ sở dữ liệu bị hỏng hoặc bị hư hỏng có thể được mở ra nhưng không thể được nén thành công. Mã này không nhập khẩu các yếu tố sau đây: - Tài liệu tham khảo - Thông số kỹ thuật import /export - An ninh thông tin (người sử dụng và cho phép nhóm)Người sử dụng hiện tại (thường là các quản trị viên) trở thành chủ sở hữu của tất cả các đối tượng import Bài viết này giả định rằng bạn đã quen thuộc với các ngôn ngữ lập trình được chứng minh và các công cụ được sử dụng để tạo ra và gỡ lỗi thủ tục. Microsoft hỗ trợ các chuyên gia có thể giúp giải thích các chức năng của một thủ tục đặc biệt, nhưng họ sẽ không sửa đổi các ví dụ để cung cấp tính năng bổ sung hoặc xây dựng các thủ tục để đáp ứng nhu cầu cụ thể của bạn.Nếu bạn đã giới hạn kinh nghiệm lập trình, bạn có thể muốn liên hệ với Microsoft Certified Partner hoặc Microsoft Dịch vụ tư vấn. Để biết thêm thông tin, truy cập vào các trang web của Microsoft:Microsoft Certified Đối tác - https://partner.microsoft.com/global/30000104Microsoft Dịch vụ Tư vấn - http://support.microsoft.com/gp/advisoryservice Đối với thông tin thêm về các tùy chọn hỗ trợ sẵn có và làm thế nào để liên hệ với Microsoft, hãy truy cập vào trang web sau của Microsoft: http://support.microsoft.com/default.aspx?scid=fh; EN-US; CNTACTMS Để import tất cả các đối tượng từ một cơ sở dữ liệu vào cơ sở dữ liệu hiện tại, hãy làm theo các bước sau: 1. Bắt đầu truy cập, và sau đó mở cơ sở dữ liệu mà bạn muốn nhập khẩu các đối tượng. Điều này có thể là một cơ sở dữ liệu mới để trống. 2. Trong cửa sổ cơ sở dữ liệu, nhấp vào Modules, và sau đó kích New.Trên trình đơn Tools, nhấn Tài liệu tham khảo. Hãy chắc chắn rằng Microsoft DAO 3.6 Object Library hoặc sau đó được chọn trong danh sách các tài liệu tham khảo. Ngoài ra hãy chắc chắn rằng bất kỳ tài liệu tham khảo Microsoft ActiveX Data Objects không được chọn. 3. Nhấp vào OK. 4. Nhập hoặc dán đoạn mã sau vào cửa sổ mô-đun: '============================================================ Option Compare Database Option Explicit Public Function ImportDb(strPath As String) As Boolean On Error Resume Next Dim db As Database 'Database to import Dim td As TableDef 'Tabledefs in db Dim strTDef As String 'Name of table or query to import Dim qd As QueryDef 'Querydefs in db Dim doc As Document 'Documents in db Dim strCntName As String 'Document container name Dim x As Integer 'For looping Dim cntContainer As Container 'Containers in db Dim strDocName As String 'Name of document Dim intConst As Integer Dim cdb As Database 'Current Database Dim rel As Relation 'Relation to copy Dim nrel As Relation 'Relation to create Dim strRName As String 'Copied relation's name Dim strTName As String 'Relation Table name Dim strFTName As String 'Relation Foreign Table name Dim varAtt As Variant 'Attributes of relation Dim fld As Field 'Field(s) in relation to copy Dim strFName As String 'Name of field to append Dim strFFName As String 'Foreign name of field to append 'Open database which contains objects to import. Set db = DBEngine.Workspaces(0).OpenDatabase(strPath, True) 'Import tables from specified Access database. For Each td In db.TableDefs strTDef = td.Name If Left(strTDef, 4) <> "MSys" Then DoCmd.TransferDatabase acImport, "Microsoft Access", strPath, acTable, _ strTDef, strTDef, False End If Next 'Import queries. For Each qd In db.QueryDefs strTDef = qd.Name DoCmd.TransferDatabase acImport, "Microsoft Access", strPath, acQuery, _ strTDef, strTDef, False Next 'Copy relationships to current database. Set cdb = CurrentDb For Each rel In db.Relations With rel 'Get properties of relation to copy. strRName = .Name strTName = .Table strFTName = .ForeignTable varAtt = .Attributes 'Create relation in current db with same properties. Set nrel = cdb.CreateRelation(strRName, strTName, strFTName, varAtt) For Each fld In .Fields strFName = fld.Name strFFName = fld.ForeignName nrel.Fields.Append nrel.CreateField(strFName) nrel.Fields(strFName).ForeignName = strFFName Next cdb.Relations.Append nrel End With Next 'Loop through containers and import all documents. For x = 1 To 4 Select Case x Case 1 strCntName = "Forms" intConst = acForm Case 2 strCntName = "Reports" intConst = acReport Case 3 strCntName = "Scripts" intConst = acMacro Case 4 strCntName = "Modules" intConst = acModule End Select Set cntContainer = db.Containers(strCntName) For Each doc In cntContainer.Documents strDocName = doc.Name DoCmd.TransferDatabase acImport, "Microsoft Access", strPath, intConst, _ strDocName, strDocName 'Debug.Print strDocName 'for debugging, will list document names in debug window. Next doc Next x 'Clean up variables to recover memory. Set fld = Nothing Set nrel = Nothing Set rel = Nothing Set cdb = Nothing Set td = Nothing Set qd = Nothing Set cntContainer = Nothing db.Close Set db = Nothing ImportDb = True End Function '==================================
|