2016-10-01 5 views
2

私はコードを開始する場所からAccessデータベースにないAccessフォームからフォーム、コントロール、およびプロパティのデータを取得する方法を理解しようとしています。私はデータベース内からデータを取得する方法を考え出しましたが、データベース外のフォームからデータを取得する方法を理解することはできません。異なるデータベースからのフォーム、コントロール、およびプロパティデータの取得

現在のデータベースに外部データベースを設定すると、私のコードがうまくいくと思いました。ただし、 "For Each frm In appAccess.Forms"を実行すると、カーソルは "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 
関連する問題