Results 1 to 4 of 4
  1. #1
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    1,560
    Thanks
    37
    Thanked 1 Time in 1 Post

    Code for multiple controls (2003/XP)

    A form uses two search boxes that sit in the form header. One cbo lets the user search by LastName; the other by FirstName. I've borrowed code from Helen Feddema's new book, Expert one-on-one Access Application Development, to set one of the combobox's value to Null with the form's Current procedure. The code works very nicely, and I'd like to apply the same code to the second combobox.

    I'm sure I can simply duplicate the event procedure for the other control, but might there be a more elegant or efficient way to make one block of code work for both controls? For one control, the line of code reads:

    <font face="Georgia">Me![Combo16] = Null</font face=georgia>

    How can I make it read something like, "This applies to both Combo16 and Combo20"? (Yeah, I know, I shoulda renamed those combo boxes to something more descriptive, but oh well....)

  2. #2
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    Vancouver, Br. Columbia, Canada
    Posts
    632
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code for multiple controls (2003/XP)

    check out the ActiveControl property -- it refers to the control that currently has focus.
    --------------------------------------------------
    Jack MacDonald
    Vancouver, Canada

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Code for multiple controls (2003/XP)

    In the On Current event of the form, you will simply have to create a separate line for each combo box

    Private Sub Form_Current()
    Me.Combo16 = Null
    Me.Combo20 = Null
    ' (other code for the On current event goes here)
    End Sub

    If you had oodles of combo boxes, all of which must be set to Null, it would become more efficient to use a loop:

    Private Sub Form_Current()
    Dim ctl As Control
    For Each ctl In Me.Controls
    If ctl.ControlType = acComboBox Then
    ctl.Value = Null
    End If
    Next ctl
    Set ctl = Nothing
    End Sub

  4. #4
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    1,560
    Thanks
    37
    Thanked 1 Time in 1 Post

    Re: Code for multiple controls (2003/XP)

    Now that's smooth! I like that loop thing, although I can't say I have "oodles of combo boxes." Thanks again, Hans!

Posting Permissions

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