2

在工作中,我们有一个拆分 ms 访问数据库。后端位于本地映射的驱动器上(因此对于每个人来说都是相同的路径)。我知道想在前端创建一个按钮,单击该按钮会自动创建数据库的合并版本。此版本对于特定的备份/历史记录需求是必需的。我对 VBA 编程知之甚少,因此不胜感激。

要创建合并版本,代码只需执行以下操作: 创建重复的前端 (?) 删除重复中的所有现有表 从后端导入表到重复

(我知道合并拆分数据库并不是一个好主意,但在这种情况下,对于许多完全不了解 CS 的用户来说,这是最有用的解决方案)

4

1 回答 1

1

使用以下函数在前端数据库中创建一个模块

Public Function ImportLinkedTables()
Dim cdb As DAO.Database, tbd As DAO.TableDef
Dim tablesToLink As Collection, item As Variant, a() As String
Const LinkPrefix = ";DATABASE="
Set cdb = CurrentDb

Set tablesToLink = New Collection
For Each tbd In cdb.TableDefs
    If tbd.Connect Like (LinkPrefix & "*") Then
        '' tab-delimited list: TableDef name [tab] Source file [tab] Source table
        tablesToLink.Add tbd.Name & vbTab & Mid(tbd.Connect, Len(LinkPrefix) + 1) & vbTab & tbd.SourceTableName
    End If
Next
Set tbd = Nothing

For Each item In tablesToLink
    a = Split(item, vbTab, -1, vbBinaryCompare)
    DoCmd.DeleteObject acTable, a(0)
    Debug.Print "Importing [" & a(0) & "]"
    DoCmd.TransferDatabase acImport, "Microsoft Access", a(1), acTable, a(2), a(0), False
Next
Set tablesToLink = Nothing

Set cdb = Nothing
DoCmd.Quit
End Function

只需一步即可创建名为“ImportLinkedTables”的宏:

RunCode
    Function Name  ImportLinkedTables()

启动该过程的表单按钮背后的代码是

Private Sub Command0_Click()
Dim fso As FileSystemObject
Dim wshShell As wshShell
Dim accdbName As String, command As String

Const SourceFolder = "Y:\_dev\"
Const DestFolder = "C:\Users\Gord\Desktop\"

accdbName = Application.CurrentProject.Name

'' copy front-end file to new location
Set fso = New FileSystemObject
fso.CopyFile SourceFolder & accdbName, DestFolder & accdbName, True
Set fso = Nothing

Set wshShell = New wshShell
command = """"
command = command & wshShell.RegRead("HKLM\Software\Microsoft\Office\" & Application.Version & "\Common\InstallRoot\Path")
command = command & "MSACCESS.EXE"" """ & DestFolder & accdbName & """ /x ImportLinkedTables"
wshShell.Run command, 7, False
Set wshShell = Nothing

End Sub
于 2013-04-21T18:35:33.493 回答