Results 1 to 2 of 2

Thread: many similar event procedures

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    1

    many similar event procedures

    I have written a macro/application, which
    contains many userforms. each userform
    contains many comboboxes (in addition to other controls) I want
    several things to happen when the value in the combobox is changed.
    Here are typical examples of the code that i currently have in the combobox
    change event procedure:

    Private Sub ComboBox10_Change()
    Dim ftype As Integer
    Recalc
    ftype = 2 + ComboBox10.ListIndex
    Call PriceToCell(12, ComboBox10, 20, ftype)
    End Sub

    Private Sub ComboBox11_Change()
    Dim ftype As Integer
    Recalc
    ftype = 2 + ComboBox11.ListIndex
    Call PriceToCell(12, ComboBox11, 18, ftype)
    End Sub

    etc....

    I repeat this code many times where everything is almost exactly the
    same, except for the text "ComboBox10" would be the name of the
    combobox, and the the text "20" or "18" is specific for each combobox.

    what i would like to do is make this code generic so that i can copy it
    everywhere i need it without having to change it everytime, or even
    better refer to a procedure in my main module call CBchangeeventprocedure..... so my first
    idea is initializing the combobox so that the value of combobox.tag in
    this case would be 20. Then i could replace the text 20, which is
    specific with something like Cint(ComboBox10.tag). But i still need to
    find some way to replace the specific ComboBox name, not in the name of
    the procedure, but only within the procedure. I have some idea that
    there is a solution to this involving setting up a class module, but i
    am not sure that i understand all the complexity of that. I thought
    the generic "Me" might work, but that gives me an error.

    any advice you have would be a big help.

    the PriceToCell function looks like this:

    Public Sub PriceToCell(ByVal cellpos As Integer, ByVal mycontrol As
    Object, ByVal wType As Integer, ByVal Fintype As Integer)
    Dim Price As Integer
    WindowPrice = Price(wType, finType)
    ActiveCell.Offset(0, cellpos).Value = mycontrol.Name & ":" &
    mycontrol.Value & ":" & Price
    End Sub

    the recalc function is working fine, and irrelevant, i believe.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: many similar event procedures

    Hi natanz, welcome to VBForums!

    To make the change code generic you can create a sub which accepts the two values that vary, and just call it with the values each time, eg:
    VB Code:
    1. Sub GenericChange(TheControl as Object, RefNumber as Integer)
    2. Dim ftype As Integer
    3.   Recalc
    4.   ftype = 2 + TheControl.ListIndex
    5.   Call PriceToCell(12, TheControl, RefNumber, ftype)
    6. End Sub
    7.  
    8. 'usage:
    9. Private Sub ComboBox10_Change()
    10.  Call GenericChange(ComboBox10, 20)
    11. End Sub
    12.  
    13. Private Sub ComboBox11_Change()
    14.  Call GenericChange(ComboBox11, 18)
    15. End Sub

    A class would not help, and Me refers to the form rather than the control (you could work with the ActiveControl property, but this is likely to cause errors).

Posting Permissions

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



Click Here to Expand Forum to Full Width