UserForm Initializes again when I reference a form control

  • Hey guys

    Long time reader, first time poster. I was hoping somebody could help me with my headache please.

    I have a userform called frmSubscriptionDetails that has a number of controls on it. I initialize the form and successfully populate the options for the combobox drop downs. The next thing I want to do is populate the form based on the record that the user selected on the previous form. The way I am going about it is fairly intricate and I won't confuse matters by going into it unless I need to. The bottom line is that I am trying to loop through the controls on the form and take some action with them. I can do this successfully only if I refer to the form as 'me' (me.MyControl), otherwise, if I reference the form by name (frmSubscriptionDetails.MyControl), it causes the code to jump back to the form's initialize event.

    Below is a simplification of my code, which I currently have in the userform's code...

    In the form...
    Private Sub UserForm_Initialize()

    'Code here to populate the combobox drop down options. (working)

    Call PopulateControls 'See below

    End Sub

    Private Sub PopulateControls

    For Each MyControl In frmSubscriptionDetails.Controls 'this line causes reinitialize

    'For each MyControl in Me.Controls 'this method works as an alternative to the above but it forces me to hold this code in the form instead of in another module.

    strColumnName = Me.Controls(strColumnName).Caption 'this line works
    'strColumnName = frmSubscriptionDetails.Controls(strColumnName).Caption 'this line causes reinitialize of the form

    Next For

    End Sub

    You don't need to bother yourself with why my string variable is called strColumnName but I will tell elaborate if you insist.

    I can see that it has something to do with me referencing the form by name but I don't understand why it should make a difference and I don't know what to do to remedy it. The reason i really want to Remedy it is mainly so that I can successfully reference form's controls from other modules and also just because I really WANT to know the answer at this point. I can provide more code if need be but I'm slightly ashamed of it to be honest as I'm a novice, and I suspect that my roundabout method of trying to achieve things will only confuse matters if I expose it.

    Thanks in advance.


Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!