Grouping event handlers

  • Thread starter Thread starter Ripan
  • Start date Start date
R

Ripan

I have a data entry form with numerous fields. Certain fields ar
required and need a non-blank input. I am trying to create a clas
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 i
the text box has a value and 1 otherwise. The logic here is that al
"RequiredField" objects on a field should have the notComplete set to
for the entire form to be complete.

Once I have this code, written, as a test, I set one of my existin
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 trigge
the event.

Is there something wrong with my process? Alternatively, is there
better way to do this?

Any help is appreciated
 
Perhaps your variable RequiredField has gone out of scope?

The solution most suitable to your situation would appear to be a
collection object, declared in your form's General Declarations
section. This way the collection will survive the lifetime of the
instance of the form and you can plug many RequiredField objects into
it.

Even better, create a RequiredFields collection class to organize your
RequiredField objects. Better still, also create a parent class that
can loop through all the child RequiredFields objects in the
collection to verify that *all* have been completed.

Aside: boolean vaiables should have positive names to avoid confusing
code e.g.

If notComplete = False Then ...

If Not notComplete Then ...

Consider renaming to IsBlank. Also 'Required' sounds like a boolean
property itself rather than a textbox.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top