Results 1 to 8 of 8
  1. #1
    newbieX is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    111

    Compare controls of two forms

    I have two forms which are identical except one form is bound and one is not. I want to update the text that is automatically populated in the “status bar text” in the bound form to the matching field in the unbound form. All the related control names are the same in both fields. Thus I need to construct a VBA script that does basically this:

    Code:
    For each ctl in Forms!BoundForm.Control and Forms!UnboundForm.Controls
        If BoundForm.Control.Name = UnboundForm.Control.Name Then
            For each ctl in Forms
                   Select Case ctl.ControlType
                    Case acCommandButton, acOptionButton, acCheckBox, acOptionGroup, acBoundObjectFrame, acTextBox, acListBox, acComboBox, acSubform, acObjectFrame, acCustomControl, acToggleButton
                    UnBoundForm.Control. StatusBarText = BoundForm.Control. StatusBarText
                End Select
            Next
        End If
    Next
    I know how to search through all the controls of a single form, but do not know how to do this with two forms and compare.



    Any and all help will be appreciated.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Try:

    For each ctl in Forms!BoundForm.Controls
    UnboundForm.Controls(ctl.Name).StatusBarText = ctl.StatusBarText
    Next
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Why you would do this escapes me, but..
    cant you just run a query and the query compared the 2 controls?
    select forms!frm1!combo, forms!frm2!combo, ...

    or in code

    debug.print ctl.name , ctl.value, forms!frm2.controls(ctl.name).value

  4. #4
    newbieX is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    111
    I forgot that there are a few controls on each of the forms that are unique. (I hate it when that happens). I tried to make the search conditional but it bombs on the If line. Also, I have the unbound form open in design view as this is the form I want to write the data to and then save.

    Code:
        Dim ctl As Control
    
        For Each ctl In [Forms]![frmBound].Controls
            Select Case ctl.ControlType
                Case acCommandButton, acOptionButton, acCheckBox, acOptionGroup, acBoundObjectFrame, acTextBox, acListBox, acComboBox, acSubform, acObjectFrame, acCustomControl, acToggleButton
                If Forms!frmUnBound.Controls(ctl.Name) = ctl.Name Then
                    Forms!frmUnBound.Controls(ctl.Name).StatusBarText = ctl.StatusBarText
                Else
                    'Pass'
                End If
            End Select
        Next

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    If the forms aren't identical (controls are 1-to-1) Then need two ctl variables and nested loops.
    Code:
       Dim ctl1 As Control
       Dim ctl2 As Control
        For Each ctl1 In [Forms]![frmUnBound].Controls
            Select Case ctl1.ControlType
                Case acCommandButton, acOptionButton, acCheckBox, acOptionGroup, acBoundObjectFrame, acTextBox, acListBox, acComboBox, acSubform, acObjectFrame, acCustomControl, acToggleButton
                     For Each ctl2 In Forms!frmBound.Controls
                         If ctl1.Name = ctl2.Name Then ctl1.StatusBarText = ctl2.StatusBarText
                     Next
            End Select
        Next
    To use my other suggestion, have an If condition that doesn't look for control on other form for specific controls. How many on Unbound would not be on Bound?
    Code:
    Dim ctl As Control
    For Each ctl in [Forms]![frmUnBound].Controls 
        Select Case ctl1.ControlType
            Case acCommandButton, acOptionButton, acCheckBox, acOptionGroup, acBoundObjectFrame, acTextBox, acListBox, acComboBox, acSubform, acObjectFrame, acCustomControl, acToggleButton    
                 If Not (ctl.Name = "this name" Or ctl.Name = "this other name" Or ctl.Name = "another name") Then
                       ctl.StatusBarText = FormsBoundForm.Controls(ctl.Name).StatusBarText
                 End If
        End Select
    Next
    I wonder why there are two nearly identical forms. Consider one form and code that manages availability of controls dependent on conditions.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    newbieX is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    111
    Quote Originally Posted by June7 View Post
    If the forms aren't identical (controls are 1-to-1) Then need two ctl variables and nested loops.
    Code:
       Dim ctl1 As Control
       Dim ctl2 As Control
        For Each ctl1 In [Forms]![frmUnBound].Controls
            Select Case ctl1.ControlType
                Case acCommandButton, acOptionButton, acCheckBox, acOptionGroup, acBoundObjectFrame, acTextBox, acListBox, acComboBox, acSubform, acObjectFrame, acCustomControl, acToggleButton
                       For Each ctl2 In Forms!frmBound.Controls
                           If ctl1.Name = ctl2.Name Then ctl1.StatusBarText = ctl2.StatusBarText
                       Next
            End Select
        Next
    I wonder why there are two nearly identical forms. Consider one form and code that manages availability of controls dependent on conditions.
    Person who created forms had two different goals. One was to collect data in an unbound form and write the results to the tables when the command button was pushed. The other was to review data to a bound form when certain conditions were met.

    Changed the line below slightly (the ctl's positions after the Then) since it was originally incorrectly populating the bound table with the unbound values.


    Code:
    If ctl1.Name = ctl2.Name Then ctl2.StatusBarText = ctl1.StatusBarText
    Thank-you. At least I got one thing to work today.

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    You must have read my post before all my edits. Might look at again.

    Glad you got it working. But this isn't populating a table, right - you meant form?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    newbieX is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    111
    Quote Originally Posted by June7 View Post
    You must have read my post before all my edits. Might look at again.

    Glad you got it working. But this isn't populating a table, right - you meant form?
    Yes. I meant form. Thanks again.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Forms Controls, Methods and Properties
    By cfwoodbury in forum Access
    Replies: 5
    Last Post: 10-09-2012, 09:35 AM
  2. Referencing Controls on Forms
    By cbh35711 in forum Access
    Replies: 7
    Last Post: 04-05-2012, 09:04 PM
  3. Controls in forms and subforms
    By donnan33 in forum Access
    Replies: 2
    Last Post: 01-05-2012, 10:29 AM
  4. Filling controls on two forms
    By recon2011 in forum Forms
    Replies: 3
    Last Post: 09-01-2011, 01:54 PM
  5. Replies: 1
    Last Post: 06-24-2011, 04:27 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums