Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2003
    Posts
    123

    Unanswered: Changing properties of certain types of controls

    I have a form with a couple dozen controls of which 10 are text boxes, and 6 are combo boxes. How can I cycle through the controls and change perhaps the background color on just the text boxes and combo boxes. I want to ignore the rest of the types of controls.

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Hi

    Loop through the collection and check the controltype property. You can check this against the controltype enums (your relevant system constants are acTextBox and acComboBox)

    HTH
    Testimonial:
    pootle flump
    ur codings are working excelent.

  3. #3
    Join Date
    Apr 2003
    Location
    Alabama, USA
    Posts
    154
    Here's an example if you call it from the form with the controls.
    Code:
    Dim ctrl As Control
    
    For Each ctrl In Me.Controls
        Select Case ctrl.ControlType
            Case acBoundObjectFrame    'Bound object frame
    
            Case acCheckBox    'Check box
    
            Case acComboBox    'Combo box
                ctrl.BackColor = 255
    
            Case acCommandButton    'Command button
    
            Case acCustomControl    'ActiveX (custom) control
    
            Case acImage    'Image
    
            Case acLabel    'Label
    
            Case acLine    'Line
    
            Case acListBox    'List box
    
            Case acObjectFrame    'Unbound object frame or chart
    
            Case acOptionButton    'Option button
    
            Case acOptionGroup    'Option group
    
            Case acPage    'Page
    
            Case acPageBreak    'Page break
    
            Case acRectangle    'Rectangle
    
            Case acSubform    'SubForm / SubReport
    
            Case acTabCtl    'Tab
    
            Case acTextBox    'Text box
                ctrl.BackColor = 255
    
            Case acToggleButton    'Toggle button
        End Select
    Next ctrl
    As pootle flump has already stated, you really only need to use acComboBox and acTextBox in your Case statement, but I included the others just as a reference.

    Hope this helps!
    http://AccessDB.Info

    You live and learn. At any rate, you live. - Douglas Adams

  4. #4
    Join Date
    Aug 2003
    Posts
    123
    This helps a lot. Also, how can cycle through just the Detail section and leave the Header and Footer controls alone?

  5. #5
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Quote Originally Posted by jrn0074
    This helps a lot. Also, how can cycle through just the Detail section and leave the Header and Footer controls alone?
    Code:
    Me.Detail.Controls
    Testimonial:
    pootle flump
    ur codings are working excelent.

  6. #6
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    441
    Small warning - the name of the sections can be altered through the property sheet AND are localized. Where I reside, the name of the Detail section is Detalj. I think I'd recommend something like this

    for each ctl in me.section(acDetail).controls
    Roy-Vidar

  7. #7
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    that's cute Roy!
    i had some issues in the past with Détail - nice fix!

    izy
    currently using SS 2008R2

  8. #8
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    I am very gald I generally don't have any regionality issues to deal with (date formats excluded). It must be extremely frustrating to retrospectively find your code was "correct" but for the wrong region.

    Keep "internationalising" my code Roy - it keeps me aware of an issue I would otherwise be ignorant of!
    Testimonial:
    pootle flump
    ur codings are working excelent.

  9. #9
    Join Date
    Mar 2003
    Location
    The Bottom of The Barrel
    Posts
    6,102
    Provided Answers: 1
    I'm a big fan of using the "tag" property all controls have for this sort of thing. Tag is an arbitrary property that you can populate with whatever tickles your fancy. In your case, maybe you want to stuff an integer into the tag property that indicates whether or not a certain kind of formatting applies. If you use this method, you can then iterate through all of the controls and examine their tag attribute instead of having to trap each individual type of control to be changed.
    oh yeah... documentation... I have heard of that.

    *** What Do You Want In The MS Access Forum? ***

  10. #10
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    441
    Thank you.

    On "internationalization" - most of the issues arise when using dynamic SQL. If you use methods utilizing the parameters collection, you're usually free of such. More stuff with dynamic SQL, when you use numbers having decimals, then some parts of the world use comma as decimalseparator, which makes dynamic SQL barf with a message indicating you're passing more columns than listed in the column list.

    ...mynumeric = " & replace(cstr(mynum), ",", ".") & "...

    Looping the controls collection each time you want to perform something on a subset of the controls can be a slow operation, therefor such ways of reducing the number of controls it loops can come in quite handy. Say for instance looping only controls on one page of a tab control

    for each ctl in me!mytab.pages(0).controls

    Faster still, Would be to use custom collections. Check out this newsgroup posting by David Fenton http://groups.google.com/group/comp....585bce38911afa.
    Roy-Vidar

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •