2016-10-01 44 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 
相關問題