Author Topic: Event procedures for Controls created at Run-time  (Read 8172 times)

0 Members and 1 Guest are viewing this topic.

Kheilmann

  • Guest
Event procedures for Controls created at Run-time
« on: August 15, 2005, 09:55:36 AM »
I have checkboxes created from Field Values in a DB.  When these items are checked or unchecked I need another form to Activate.  I tried adding a Checkbox_Click sub for each checkbox, but that does not work, at least for me.  It would not be a good idea anyway, but it was all I knew to do.  
I think my solution may have to do with CLasses or class modules, but I need to read up on those since I am not to familiar with anything about them...

So my first question is can you have event procedures with a run-time created control?
then, how do you do it?

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Event procedures for Controls created at Run-time
« Reply #1 on: August 15, 2005, 10:43:34 AM »
first answer ... yes you can have events with runtime created controls ....

second answer ... by subclassing the control

I'll dig up an example and post here, showing you how to do it ...
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Draftek

  • Guest
Event procedures for Controls created at Run-time
« Reply #2 on: August 15, 2005, 11:10:12 AM »
Since you are creating the control at run-time, you will have a variable for the control. You can simply use the "WithEvents" qualifier when you declare the control as:

Private WithEvents oCheck As CheckBox

You should then have the events show up in your pull-down menu in the vb ide.

Note: WithEvents must be used in a class module - Which is what your form code exists in anyway..

Kheilmann

  • Guest
Event procedures for Controls created at Run-time
« Reply #3 on: August 15, 2005, 04:00:17 PM »
Thanks for everything....

I still have one problem, and I hope there is an easy way through it....

Dim Withevents cbox as msforms.checkbox  is what I had to use to get it to work.
If I used cbox as checkbox I received a Compile Error.  

Now Cbox shows up in my General Pulldown and all my events show up on the Events Pulldown.  The problem is that I use the cbox for all my checkboxes.

The Db where the names are for the checkboxes can be editted by the end user, so I can not use a var for each since I don't know how many will be there.

I really don't know what my options are, especially since I can not create an array with WithEvents.

I guess I could just create a var for each known checkbox, then whenever someone adds one, I can just modify the code to read that var also.

Surely...hopefully..there is a better way..

Draftek

  • Guest
Event procedures for Controls created at Run-time
« Reply #4 on: August 15, 2005, 04:18:29 PM »
An array, oh that is a little different, but easier.

In order to dynamically use control arrays, you need to have the first one defined in your form in design mode and then use the Load statement to add as you need. Make sure you unload when finished.

Then you can do all your coding in the click event of the control using the Index passed to test which one was clicked.

Kheilmann

  • Guest
Event procedures for Controls created at Run-time
« Reply #5 on: August 15, 2005, 05:39:14 PM »
I'm sorry, but...huh?

I apologize if I am missing something, but...
I was under the impression you could not use withevents with arrays...

are you proposing another way?  if so, I am lost.  

I have never used the load statement, and why do I need to have a checkbox already created?  I can do it I am just wondering what the purpose of it is...??

This is all new to me, so I apologize for the ignorance..

Draftek

  • Guest
Event procedures for Controls created at Run-time
« Reply #6 on: August 15, 2005, 07:42:21 PM »
You don't NEED WithEvents or a variable to manipulate control arrays:

In order to use control arrays at run-time you need to do the following:
1. create a base control and give it an index of 0. You can turn visible off if there is an instance where it will not be needed on the form.
2. Using the event of your choice for that control write your code using the Index passed so you know which one was clicked or whatever.
3. In your code that adds the controls use the Load statement as:
Load MyControl(0 + n)
Make sure it's visible and in the right location.
 
You already have the code you need because of the original (Index of zero) control's event.

Here is a function I have used to add some controls and the size of the form depending on how many records I have:

Code: [Select]

Private Sub FlushRecordsetToForm()
    Dim I As Long
    Dim J As Long
    If Not rsEngAdds.BOF Then rsEngAdds.MoveFirst
    For I = 0 To rsEngAdds.Fields.count - 1
        If I <> 0 Then
            Load txtPercentage(I)
            txtPercentage(I).Visible = True
            txtPercentage(I).Top = txtPercentage(I - 1).Top + txtPercentage(I).Height + 150
            Load lblRange(I)
            lblRange(I).Visible = True
            lblRange(I).Top = txtPercentage(I).Top + 50
        End If
        lblRange(I).Caption = rsEngAdds.Fields(I).Name
        txtPercentage(I).Text = fCurrency(CStr(rsEngAdds.Fields(I).Value))
    Next I
    Me.Height = txtPercentage(txtPercentage.count - 1).Top + txtPercentage(0).Height + 1000
End Sub


Hope this helps.

Kheilmann

  • Guest
Event procedures for Controls created at Run-time
« Reply #7 on: August 16, 2005, 09:28:48 AM »
Thanks for your help, but I still have no idea what I am doing.  
I really don't want to waste your time, so .....

I'll keep toying around with your posts until I eventually figure something out.

I think in the mean time I will just create a load of
Dim WithEvents Cbox## as msforms.checkbox
and hope there are enough variables to cover any additonal controls  someone may add to the DB tables.

Thanks again, I really do appreciate the help you have given to me.

Draftek

  • Guest
Event procedures for Controls created at Run-time
« Reply #8 on: August 16, 2005, 09:53:52 AM »
I really wish you wouldn't do that.. Take a deep breath.

This is frustrating to you because it's a advanced for a beginnner but you can learn something useful for later with some patience.

What exactly are you trying to accomplish? I can't quite discern from your first post.

What does "I have checkboxes created from Field Values in a DB" actually mean? You have a form that you've added check boxes mimicking a row in a database table?

Kheilmann

  • Guest
Event procedures for Controls created at Run-time
« Reply #9 on: August 16, 2005, 11:09:23 AM »
My "adding a load of variables" idea seems to be not such a great idea, man what was I thinking....

OK...
I have RoomNames that need values like CFMS, Grills, and Boots.
I have one Form with these 3 boxes (text and combo) to input the information from each room.  So whether I am entering data for Kitchen or Bedroom1 the same form loads to input the data.  That form loads whenever a Checkbox is clicked TRUE.
On my main UserForm there are no checkboxes.  They are all created at runtime.  
In the Activate Event of my UserForm VBA is creating Checkboxes for each roomname in one of my 5 Tables(Bed, Bath, Living, Recreation, and OPEN).  Whenever the Checkboxes are clicked TRUE, then a form to input data such as CFM, Grills, and Boots Activates and waits for input.

So every room in all five Tables is used to create a Checkbox on a particular Page.  But only the rooms clicked TRUE will activate the Room Data Userform.

I need each newly created CheckBox to have it's own Variable.

Kheilmann

  • Guest
Event procedures for Controls created at Run-time
« Reply #10 on: August 16, 2005, 11:12:04 AM »
Here is what I have so far:
Code: [Select]

Option Explicit
Dim HVACdb As DAO.Database
Dim HVACrec As DAO.Recordset
Dim SQLquery As String
Dim SQLstr(0 To 4) As String
Dim Room As String
Dim WithEvents cbox As MSForms.CheckBox


Private Sub cbox_click()

If cbox.Value = True Then
    RoomData_UserForm.Show
End If


End Sub

Private Sub OK_CmdBtn_Click()

Me.Hide

End Sub

Private Sub UserForm_Activate()

Dim Tctrl As Control
Dim intTop As Integer
Dim X As Integer

SQLstr(0) = "Select Bed.RoomName From Bed;"
SQLstr(1) = "Select Bath_Utility.RoomName From Bath_Utility;"
SQLstr(2) = "Select Living.RoomName From Living;"
SQLstr(3) = "Select Recreation.RoomName From Recreation;"
SQLstr(4) = "Select Open.RoomName From Open;"

If Start.StrUser = "KEVIN" Then
    Set HVACdb = DAO.OpenDatabase("C:\akadsolution\Programs\AKS HVAC Design Package\Qbid.mdb")
ElseIf Start.StrUser = "JENNIFER" Then
    Set HVACdb = DAO.OpenDatabase("F:\akadsolution\Programs\AKS HVAC Design Package\Qbid.mdb")
Else:
    'Set HVACdb = DAO.OpenDatabase("\customerpath\Qbid.mdb")
End If

For X = 0 To MultiPage1.Pages.Count - 1
    intTop = 22
    SQLquery = SQLstr(X)
    Set HVACrec = HVACdb.OpenRecordset(SQLquery, dbOpenDynaset)
   
    If HVACrec.RecordCount > 0 Then
        HVACrec.MoveFirst
            Do Until HVACrec.EOF
                Room = HVACrec.Fields("RoomName").Value
                Set cbox = MultiPage1.Pages(X).Controls.Add("Forms.Checkbox.1", Room & "_ChkBox", Visible)
                    With cbox
                        .Left = 6
                        .Height = 15
                        .Top = intTop
                        .Width = 75
                        .Caption = Room
                        .Enabled = True
                        .Value = False
                        .WordWrap = False
                    End With
                intTop = intTop + 22
                HVACrec.MoveNext
            Loop
    End If
Next X
           
HVACrec.Close
Set HVACrec = Nothing
HVACdb.Close
Set HVACdb = Nothing
SQLquery = ""
Room = ""
   
End Sub


[/code]

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Event procedures for Controls created at Run-time
« Reply #11 on: August 16, 2005, 11:35:02 AM »
One of the issues you may be experiencing is that with MSForms2 there isn't an index option in VBA, however with a Check control in VB there is.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Draftek

  • Guest
Event procedures for Controls created at Run-time
« Reply #12 on: August 16, 2005, 11:51:39 AM »
ahh! I see. As Keith pointed out, vba does not allow control arrays.

Sorry, I should have asked that first, I just assumed you were using VB.

Hang on, I think I have something that may work..

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Event procedures for Controls created at Run-time
« Reply #13 on: August 16, 2005, 01:20:47 PM »
Quote from: Draftek
... vba does not allow control arrays.


Actually VBA does allow control arrays by way of groups, and you must specify the group to put your CheckBox control into for it to be part of a group.

In runtime, it is a subclass that is required for VBA
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Kheilmann

  • Guest
Event procedures for Controls created at Run-time
« Reply #14 on: August 16, 2005, 01:35:21 PM »
Does anyone have any examples for putting controls into groups
and
anything about subclasses?

That would probably help me out a lot....