标题: 怎样用VB新建外部MDB文件. [打印本页] 作者: zhichaochen 时间: 2008-3-25 18:03 标题: 怎样用VB新建外部MDB文件. 我做了一个程序,将当前的表的数据保存起来,用的是
DoCmd.TransferDatabase acExport, "Microsoft Access", filename, acTable, "表名", "表名 "
这里的filenae 这个文件必须先存在,但当filename这个文件不存在时,怎样先新建这个文件呢.
查找外部文件命令是 dir filename
,但是新建外部文件呢,请指教。作者: andymark 时间: 2008-3-25 18:31
Dim CatalogX As New ADOX.Catalog
Dim ConnX As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub cmdOK_Click()
If txtDBName.Text = "" Then
MsgBox "请输入要生成的数据库名.", vbInformation
txtDBName.SetFocus
Exit Sub
End If
If Dir(txtDBName.Text) <> "" Then
MsgBox "要生成的数据库名已存在.", vbInformation
txtDBName.SetFocus
Exit Sub
End If
Dim ConnString As String
ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;"
ConnString = ConnString & "Data Source=" & txtDBName.Text
On Error GoTo CreateErr
'创建数据库
CatalogX.Create ConnString
CatalogX.ActiveConnection = ConnString
'建立新表格
Dim TableX As New Table
TableX.Name = "MyTable"
'向表格中添加字段并指定字段类型
TableX.Columns.Append "ID", adInteger
TableX.Columns.Append "Name", adVarWChar, 8
TableX.Columns.Append "Address", adVarWChar, 50
'向数据库中添加表格
CatalogX.Tables.Append TableX
Exit Sub
CreateErr:
MsgBox "创建数据库时发生错误:" & Err.Description, vbInformation
End Sub作者: zhichaochen 时间: 2008-3-25 18:39 标题: 找到答案了,用Workspace.CreateDatabase If Dir(filename) = "" Then
Dim wrkDefault As Workspace
Dim dbsNew As Database
Dim prpLoop As Property
' Get default Workspace.
Set wrkDefault = DBEngine.Workspaces(0)
' Make sure there isn't already a file with the name of the new database.
' Create a new encrypted database with the specified collating order.
Set dbsNew = wrkDefault.CreateDatabase(filename, dbLangGeneral, dbEncrypt)
' With dbsNew
' Debug.Print "; Properties of " & .name
' Enumerate the Properties collection of the new
' Database object.
' For Each prpLoop In .Properties
' If prpLoop <> "" Then Debug.Print " " & _
' prpLoop.name & " = " & prpLoop
' Next prpLoop
' End With
dbsNew.close
End If