[英]Create an MS Access database file with password from Excel VBA
I want to create an MS Access file .mdb
by Excel VBA.我想通过 Excel VBA 创建一个 MS Access 文件
.mdb
。 I want to set a password for this Access file by Excel VBA.我想通过 Excel VBA 为这个 Access 文件设置一个密码。
This below code can create Access file but without password.下面的代码可以创建访问文件,但没有密码。
Sub Example1()
'the path to create the new access database
Dim strPath As String
'an Access object
Dim objAccess As Object
strPath = "D:\VBA\NewDB2.mdb"
Set objAccess = CreateObject("Access.Application")
Call objAccess.NewCurrentDatabase(strPath)
objAccess.Quit
End Sub
How do I set a password?如何设置密码?
Should be something like this应该是这样的
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
Please note, if you are using Office 2007 or newer, you must add the FileFormat
parameter, in order to have your Access file be in mdb format:请注意,如果您使用的是 Office 2007 或更新版本,则必须添加
FileFormat
参数,以使您的 Access 文件为 mdb 格式:
'create new database
objAccess.NewCurrentDatabase strPath, 10
10, being the value of the acNewDatabaseFormatAccess2002
constant . 10,是
acNewDatabaseFormatAccess2002
常量的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.