代码之家  ›  专栏  ›  技术社区  ›  Zameer Ansari

在MS Access中查找哪个表单字段指向哪个数据库字段

  •  0
  • Zameer Ansari  · 技术社区  · 8 年前

    让我解释一下 奇怪的 客户要求,我们正在抓狂:

    我们有一个MS Access VBA应用程序,在数百个表单中有数千个表单字段。

    这些表单中的几个字段填充来自几个表/查询的数据。

    表单中的一些其他字段通过查询/直接代码将数据插入到一些表中。

    请注意,这些表是 链接表 到SQL Server表。

    因此,我们需要一些工具/宏来实现这一点。

    如何在MS Access中找到哪些表单字段指向哪些数据库字段?

    基于 @ClintB's answer ctl.ControlSource

    Sub GetFormFieldToDBFieldMapping()
    Dim frm As Object
    Dim LiveForm As Form
    Dim ctl As control
    Dim i As Integer
    Dim fso As Object
    Dim ctlSource As String
    Set fso = CreateObject("Scripting.FileSystemObject")
    Dim oFile As Object
    Set oFile = fso.CreateTextFile("D:\ControlSources.txt")
    
        For Each frm In Application.CurrentProject.AllForms
    
            'To access form controls, open it
            DoCmd.OpenForm frm.Name, acViewDesign
            Set LiveForm = forms(frm.Name)
            For i = 0 To LiveForm.Controls.Count - 1
            Set ctl = LiveForm.Controls.Item(i)
            If ctl.ControlType = 106 Or ctl.ControlType = 111 Or ctl.ControlType = 110 Or ctl.ControlType = 109 Then
            ctlSource = ctlSource & vbCrLf & "Form Name :" & LiveForm.Name & ": Control Name :" & ctl.Name & ": Control Source :" & ctl.ControlSource
            End If
            Next i
            'Do not forget to close when you are done
            DoCmd.Close acForm, frm.Name
        Next
    
    oFile.WriteLine ctlSource
    oFile.Close
    Set fso = Nothing
    Set oFile = Nothing
    
    End Sub
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   ClintB    8 年前

    我会这样做。(不是实际代码)

    For each form in db
       For each control in form
           'Write a record to a table stating which formName, controlName and the control.controlSource
       Next
    Next
    

        2
  •  0
  •   Community CDub    8 年前

    你提出的代码非常棒!这将为您提供:

    • 表格名称
    • 控件名
    • 控制源

    唯一需要的是列所要到达的表名。 由于表是链接到SQL server的表,因此您可以 find all the tables with all their columns .

    这将为您提供:

    • 表名
    • 列名

    V-Lookup