Iterate databindings for bound controls on a form in VB 2008

Posted on 2008-11-16
Last Modified: 2012-08-14
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?
Question by:glenn_grubb
    LVL 11

    Accepted 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


    Author Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now