• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

Iterate databindings for bound controls on a form in VB 2008

I need to iterate through the bound controls on a form and pull out what field each bound control is connected to in an Access mdb. I am using Visual Studio 2008 VB. Any ideas?
1 Solution
Here is an example for you. i have tested it with a BindingSource.

The snippet will cycle throug all controls on the form.
When it has an binding it will store them in the 3 variables
myPropertyName                                 Bound Property
myPropertyBindField                            Bound Field of the Table
myPropertyBindDataSourceMember    Bound Table

you must only change the code to store the variables elswhere because every cycle they will be overwritten.
For Each myControl As Control In Me.Controls
            For i As Integer = 0 To myControl.DataBindings.Count - 1
                Dim myPropertyName As String
                Dim myPropertyBindField As String
                Dim myPropertyBindDataSourceMember As String
                myPropertyName = myControl.DataBindings.Item(i).PropertyName 'Bound Property
                myPropertyBindField = myControl.DataBindings.Item(i).BindingMemberInfo.BindingField 'Bound Field
                If TypeOf myControl.DataBindings.Item(i).DataSource Is BindingSource Then
                    myPropertyBindDataSourceMember = CType(myControl.DataBindings.Item(i).DataSource, BindingSource).DataMember 'Table
                End If

Open in new window

glenn_grubbAuthor Commented:
At first it didn't seem to work, but then I remember that you have to specifically call controls on a tabform (my controls are on pages in a tab form). The guts of the code I needed was this:
myPropertyBindField = myControl.DataBindings.Item(i).BindingMemberInfo.BindingField 'Bound Field
It returned the field bound to the control. Thanks

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now