2016-10-01 24 views
2

我想弄清楚如何从Access数据库中的Access数据库中从我启动代码的位置获取表单,控件和属性数据。我已经想出了如何从数据库中获取数据,但我无法弄清楚如何从数据库之外的表单获取数据。从不同的数据库中获取表单,控制和属性数据

我认为,如果我将外国数据库设置为当前数据库,我的代码将工作。但是,在执行“For AppAccess.Forms中的每个frm”之后,光标将转到“End Sub”。

我试着使用容器,我能够返回表单名称,但我无法弄清楚如何遍历控件和属性集合。

下面是我的第一个想法相关的代码。我的最终目标是能够将表单数据保存在不同的数据库中。我的代码中是否有小错误,或者是否有其他方法可用于获取数据?

Sub GetControlForm() 
Dim strPath As String 
Dim frm As Form 
Dim ctrl As Control 
Dim prop As Property 

Dim appAccess As New Access.Application 
Dim dbs As DAO.Database 

strPath = "C:\Users\Tyrone\Desktop\Test14.accdb" 
Set appAccess = CreateObject("Access.Application") 
appAccess.OpenCurrentDatabase (strPath) 

'MsgBox appAccess.CurrentDb.Name 
For Each frm In appAccess.Forms 
    MsgBox frm.Name 

    For Each ctrl In frm.Controls 
     MsgBox ctrl.Name 
     MsgBox ctrl.ControlType.TypeName 
     MsgBox TypeName(ctrl) 

     For Each prop In ctrl.Properties 
      If prop.Name = "RowSource" Then 
       MsgBox "stop it" 
      End If 
      If (TypeName(ctrl) = "ComboBox" Or TypeName(ctrl) = "TextBox") And (prop.Name = "RowSource" Or prop.Name = "ControlSource") Then 
       MsgBox prop.Value 
      End If 
     Next prop 
    Next ctrl 
Next frm 

End Sub 
+0

你看'CodeDB'和'CurrentDB'和'CodeProject'和'CurrentProject'之间的区别?如果您的代码位于Access加载项中,则它可以枚举它本身(codeDB)或当前数据库中的项目。 – ThunderFrame

+0

我没有考虑使用CodeDB和CodeProject。截至目前,我想避免必须创建一个加载项。但是,这是进一步研究的好信息。 – user2901516

回答

3

的原因,你的For Each无关,通过循环的是,在远程数据库的形式是不公开的。每documentation

“的形式的性质集合在Visual Basic中是指当前打开的形式 。”


试试这个:

Sub GetControlForm() 

    Dim strPath As String 
    Dim obj As AccessObject 
    Dim frm As Form 
    Dim ctrl As Control 
    Dim prop As Property 

    Dim appAccess As New Access.Application 
    Dim dbs As DAO.Database 

    strPath = "C:\Users\Tyrone\Desktop\Test14.accdb" 
    Set appAccess = CreateObject("Access.Application") 
    appAccess.OpenCurrentDatabase (strPath) 

    'MsgBox appAccess.CurrentDb.Name 
    For Each obj In appAccess.CurrentProject.AllForms 

     appAccess.DoCmd.OpenForm obj.Name 
     Set frm = appAccess.Forms(obj.Name) 

     MsgBox frm.Name 

     For Each ctrl In frm.Controls 
      MsgBox ctrl.Name 
      'MsgBox ctrl.ControlType.TypeName 
      MsgBox TypeName(ctrl) 

      For Each prop In ctrl.Properties 
       If prop.Name = "RowSource" Then 
        MsgBox "stop it" 
       End If 
       If (TypeName(ctrl) = "ComboBox" Or TypeName(ctrl) = "TextBox") And (prop.Name = "RowSource" Or prop.Name = "ControlSource") Then 
        MsgBox prop.Value 
       End If 
      Next prop 
     Next ctrl 

     appAccess.DoCmd.Close acForm, frm.Name 
    Next obj 

    Set frm = Nothing 
    appAccess.CloseCurrentDatabase 
    Set appAccess = Nothing 
End Sub 
相关问题