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?
Who is Participating?
melmersConnect With a Mentor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.