Solved Property to check controlsource

Kayleigh

Member
Local time
Today, 11:57
Joined
Sep 24, 2020
Messages
709
Hi its me again!
I'm looking to iterate through all forms to find where a specific field is located. I have the code below to iterate through controls but couldn't find the required property to check the controlSource. Can anyone help me with this?


Code:
Dim aO As AccessObject
    Dim fm As Access.Form
    Dim ct As Access.Control
    For Each aO In CurrentProject.AllForms
        If aO.Name = sForm Then
            Set fm = Forms(aO.Name)
            For Each ct In fm.Controls
                If isBound(ct) Then
                   'Do stuff here
                   
                End If
            Next ct
            Set fm = Nothing
            If Not bIsLoaded Then
                On Error Resume Next
                DoCmd.Close acForm, aO.Name, acSaveYes
            End If
            Exit For
        End If
    Next
 
Code:
...
...
                If isBound(ct) Then
                   'Do stuff here
                   If ct.ControlSource = "fieldNameHere" Then
                          'Do the stuff
                   End If
                  
                End If
...
....
 
The .ControlSource is bound to the control for which it IS the control's data source. It is not a form or report property but a control property. HOWEVER, as ArnelGP's code suggests, you can only do this if the control is bound.

This is a sometimes-unappreciated factor for Access. When you have something that is not bound (form, report, control...) then the properties that would be associated with the object's data sources are not null, blank, or zero. Instead, the properties DO NOT EXIST. I.e. if you look them up you don't get back a null, blank, or zero - you get back an "Unknown property" trappable error.
 
Access has a built in feature that does this for you and www.FMSINC.com has an excellent dcumentation tool. It isn't free but it is extremely helpful when looking at databases created by other people.
 
on a slightly different point, the query or table to which the form is bound is given by the forms recordsource property, and you can use that property to see the entire field list, and values that are available, with code somewhat similar to the code you are using.
 

Users who are viewing this thread

Back
Top Bottom