![]() |
Grouping Event handlers
I have a data entry form with numerous fields. Certain fields are
required and need a non-blank input. I am trying to create a class module for a text box where non-blank inputs are not allowed. I have the following code: Public WithEvents Required As MSForms.TextBox Public notComplete As Integer Private Sub Required_Change() If (Required.Text < "") Then notComplete = 0 Required.BackColor = &HFF& Required.ForeColor = &H8000000E Else notComplete = 1 Required.BackColor = &HFF& Required.ForeColor = &H8000000E End If MsgBox "In Event" End Sub Required is the text box object and notComplete is a flag that is 0 if the text box has a value and 1 otherwise. The logic here is that all "RequiredField" objects on a field should have the notComplete set to 0 for the entire form to be complete. Once I have this code, written, as a test, I set one of my existing text boxes to be an object of this type like so: Dim req As RequiredField Set req = New RequiredField Set req.Required = frmDataEntry.txtClaimantName However, when I run the form, changing txtClaimantName does not trigger the event. Is there something wrong with my process? Alternatively, is there a better way to do this? Any help is appreciated. --- Message posted from http://www.ExcelForum.com/ |
Grouping Event handlers
Ripan
Dim req As RequiredField Set req = New RequiredField Set req.Required = frmDataEntry.txtClaimantName The problem is that the variable req falls out of scope when this procedure ends and the class is un-instantiated (is that even a word?) Dim req at the top of whatever module sets it so that it persists beyond the scope of that procedure. For instance, if you wanted to instantiate the class in your Userform_Initialize event, the module would look like this: Option Explicit Dim req As RequiredField Private Sub UserForm_Initialize() Set req = New RequiredField Set req.Required = frmDataEntry.txtClaimantName End Sub Having req as a module level variable will hold the class and it will work as you expect. -- Dick Kusleika MVP - Excel www.dicks-clicks.com Post all replies to the newsgroup. |
Grouping Event handlers
Dick,
Multiposts, don't you just love 'em: http://groups.google.com/groups?selm...g .google.com -- "Dick Kusleika" wrote in message ... Ripan Dim req As RequiredField Set req = New RequiredField Set req.Required = frmDataEntry.txtClaimantName The problem is that the variable req falls out of scope when this procedure ends and the class is un-instantiated (is that even a word?) Dim req at the top of whatever module sets it so that it persists beyond the scope of that procedure. For instance, if you wanted to instantiate the class in your Userform_Initialize event, the module would look like this: Option Explicit Dim req As RequiredField Private Sub UserForm_Initialize() Set req = New RequiredField Set req.Required = frmDataEntry.txtClaimantName End Sub Having req as a module level variable will hold the class and it will work as you expect. |
Grouping Event handlers
OK, that's it. I'm done answering old posts. At least I got it right.
-- Dick Kusleika MVP - Excel www.dicks-clicks.com Post all replies to the newsgroup. "onedaywhen" wrote in message om... Dick, Multiposts, don't you just love 'em: http://groups.google.com/groups?selm...g .google.com -- "Dick Kusleika" wrote in message ... Ripan Dim req As RequiredField Set req = New RequiredField Set req.Required = frmDataEntry.txtClaimantName The problem is that the variable req falls out of scope when this procedure ends and the class is un-instantiated (is that even a word?) Dim req at the top of whatever module sets it so that it persists beyond the scope of that procedure. For instance, if you wanted to instantiate the class in your Userform_Initialize event, the module would look like this: Option Explicit Dim req As RequiredField Private Sub UserForm_Initialize() Set req = New RequiredField Set req.Required = frmDataEntry.txtClaimantName End Sub Having req as a module level variable will hold the class and it will work as you expect. |
Grouping Event handlers
Hmmm. Maybe I should be blaming Onedaywhen for interrupting my blissful
ignorance. DK "Dave Peterson" wrote in message ... But the problem is you never know unless someone warns you (just in time, whew!) or you see it in a different thread (too late, grrrrr!). Dick Kusleika wrote: OK, that's it. I'm done answering old posts. At least I got it right. -- Dick Kusleika MVP - Excel www.dicks-clicks.com Post all replies to the newsgroup. "onedaywhen" wrote in message om... Dick, Multiposts, don't you just love 'em: http://groups.google.com/groups?selm...g .google.com -- "Dick Kusleika" wrote in message ... Ripan Dim req As RequiredField Set req = New RequiredField Set req.Required = frmDataEntry.txtClaimantName The problem is that the variable req falls out of scope when this procedure ends and the class is un-instantiated (is that even a word?) Dim req at the top of whatever module sets it so that it persists beyond the scope of that procedure. For instance, if you wanted to instantiate the class in your Userform_Initialize event, the module would look like this: Option Explicit Dim req As RequiredField Private Sub UserForm_Initialize() Set req = New RequiredField Set req.Required = frmDataEntry.txtClaimantName End Sub Having req as a module level variable will hold the class and it will work as you expect. -- Dave Peterson |
All times are GMT +1. The time now is 10:07 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com