Tạo file access .mdb có chứa mật khẩu

Euler

Administrator
Thành viên BQT
Chào mọi người.
Như chúng ta đã biết, việc tạo file access, table, field thì chúng ta đã làm được rồi. Các bạn xem thêm .
Để tiến thêm một bước nữa, hôm nay mình giới thiệu đoạn code tạo ra file cơ sở dữ liệu access .mdb có chứa password.
Mã:
Option Explicit

Public Sub CreatePasswordProtectedDatabase()
    Dim strPath As String
    strPath = "D:\VBA\NewDB2.mdb"

    'create new Access application
    Dim objAccess As Object
    Set objAccess = CreateObject("Access.Application")

    'objAccess.Visible = True 'show or hide Access

    'create new database
    objAccess.NewCurrentDatabase strPath

    'example to add a table
    objAccess.DoCmd.RunSQL "CREATE TABLE Test", False
    objAccess.DoCmd.RunSQL "ALTER TABLE Test add Gender char(1)", False

    'set password
    Dim DbPassword As String
    DbPassword = "your_password"
    objAccess.CurrentProject.Connection.Execute "ALTER DATABASE PASSWORD " & DbPassword & " NULL"

    'close database
    objAccess.CloseCurrentDatabase

    'quit Access application
    objAccess.Quit
End Sub
 

tuhocvba

Administrator
Thành viên BQT
Lợi dụng đoạn code trên, ta tiến thêm một bước, đó là:
1. Tạo file cơ sở dữ liệu .mdb
2. File có password

3. File được nạp dữ liệu từ excel (import excel data into access by excel VBA)
Mã:
''Option Explicit

Public Sub CreatePasswordProtectedDatabase()
    Dim strPath As String
    strPath = "D:\VBA\NewDB22.mdb"

    'create new Access application
    Dim objAccess As Object
    Set objAccess = CreateObject("Access.Application")

    'objAccess.Visible = True 'show or hide Access

    'create new database
    objAccess.NewCurrentDatabase strPath

    'example to add a table
    objAccess.DoCmd.RunSQL "CREATE TABLE Test", False
    objAccess.DoCmd.RunSQL "ALTER TABLE Test add Gender char(255)", False
    objAccess.DoCmd.RunSQL "ALTER TABLE Test add Gender2 char(255)", False
    'set password
    Dim DbPassword As String
    DbPassword = "1234"
    objAccess.CurrentProject.Connection.Execute "ALTER DATABASE PASSWORD " & DbPassword & " NULL"
   
    strTable = "Test"     ' name of table you want in your database
    strFile = "C:\\VBA\tn.xlsx" ' you will get the file name from user selection
    objAccess.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, strTable, strFile, True
    'close database
    objAccess.CloseCurrentDatabase

    'quit Access application
    objAccess.Quit
End Sub
Dữ liệu file excel như sau:
Bạn cần đăng nhập để thấy hình ảnh


Kết quả trên access thể hiện như sau:
Bạn cần đăng nhập để thấy hình ảnh


Bạn cần đăng nhập để thấy hình ảnh
 

vbano1

SMod
Thành viên BQT
Chú ý nếu bạn muốn tạo access 2002-2003 thì bạn cần phải truyền tham số cho nó:
Mã:
'create new database
objAccess.NewCurrentDatabase strPath, 10
Tham khảo thêm ở đây:
 

Euler

Administrator
Thành viên BQT
Chú ý, với những field thiết định là string (text), tuy nhiên khi nạp từ excel vào, nếu cột đó vô tình có số (0,1,2,..) ở hàng trên cùng, thì access xảy ra hiện tượng lỗi. Mặc dù nhìn thiết định thì vẫn là text. Giảip phải khắc phục là xây dựng một dòng dữ liệu dummy trên excel cho lên trên cùng, ở các vị trí cột thiết định là text thì không ghi số, có thể ghi ký tự "-". Sau khi làm như trên thấy access được tạo ra không còn bị lỗi.
Khi tạo file mdb định dạng 2002-2003 thì tuy không có lỗi, nhưng ID khá lộn xộn dù trên excel trước đó đã sắp xếp ID theo đúng thứ tự tăng dần. Giải pháp là khi truy vấn dữ liệu nên sử dụng thêm .
 
Top