设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 3024|回复: 2
打印 上一主题 下一主题

[与其它组件] 怎样用VB新建外部MDB文件.

[复制链接]
跳转到指定楼层
1#
发表于 2008-3-25 18:03:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我做了一个程序,将当前的表的数据保存起来,用的是
      DoCmd.TransferDatabase acExport, "Microsoft Access", filename, acTable, "表名", "表名 "
这里的filenae 这个文件必须先存在,但当filename这个文件不存在时,怎样先新建这个文件呢.
查找外部文件命令是 dir filename
,但是新建外部文件呢,请指教。
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2008-3-25 18:31:00 | 只看该作者
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
3#
 楼主| 发表于 2008-3-25 18:39:16 | 只看该作者

找到答案了,用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
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-4-29 07:28 , Processed in 0.106175 second(s), 26 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表