2016-12-05 63 views
0

让我解释怪异的客户的要求,我们正在挠头了:查找其形成场点,其中在MS Access数据库中的字段

我们有成千上万的一个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 
+0

这将是几乎是不可能的,因为字段可以在记录集和使用DLookup和功能,又可以在查询中使用使用,这可以在别名其他查询并在作为ControlSource达到控件之前用于表达式中。 – Gustav

+0

对于绑定到表或简单查询的标准表单,ControlSources将是字段名称,因此可以通过这种方式找到一些字段名称。棘手的部分是捕捉所有事件。我发现的唯一方法是艰苦的工作 - 通过重命名查询并查看发生了什么的强力操作。不太好。您可以使用函数_SaveAsText_将对象保存到文本文件,这可以使搜索更容易一些。 – Gustav

回答

0

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

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 

编辑:控件,而不是行来源

+0

有没有办法获取表名? – xameeramir

0

的代码,你想出了非常出色!这会给你:

  • 表单名称
  • 控制名称
  • 控制源

你需要的仅仅是该列即将到来的表名。 因为,表是链接到SQL Server表,则find all the tables with all their columns

这会给你:

  • 表名
  • 列名

请两个Excel表这两个信息。

做一个V-Lookup对列名的查找表名