Excel VBA Class Creation

I have a form with a lot of text boxes. all have the same structure, as below:

Private Sub TextBox1_AfterUpdate()
  If TextBox1 <> "" Then
    If Application.WorksheetFunction.IsNumber(TextBox1) Then xDatum(1) = TextBox1 Else TextBox1 = nil
  End If
End Sub
Private Sub TextBox2_AfterUpdate()
  If TextBox2 <> "" Then
    If Application.WorksheetFunction.IsNumber(TextBox2) Then xDatum(2) = TextBox2 Else TextBox2 = nil
End Sub

How do I create a generic subroutine to deal with them?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leo EikelmanDirector, IT and Business DevelopmentCommented:
You can do something like
int counter = 0;

For Each Ctl As Control In Me.Controls
  If TypeOf(Ctl) Is TextBox  Then
  counter = counter + 1;
    If DirectCast(Ctl,TextBox).Text <> "" Then
      If Application.WorksheetFunction.IsNumber(DirectCast(Ctl,TextBox))
        xDatum(counter) = DirectCast(Ctl,TextBox)
        DirectCast(Ctl,TextBox) = nil
      End If
    End If
  End If
Next Ctl

mikeydmAuthor Commented:
Hi Leo,

I don't believe that Excel VBA has DirectCast available.

Besides, this would work after all the text boxes are filled, but I want to key it to the AfterUpdate event, that is, to check each box as it is filled in. I'd like to be able to rename every textbox as Textbox(i), but that syntax isn't allowed.

No nice way to do it in VB6/VBA; just a case of making a separate private function and then calling it from each of the event code blocks.  
.NET rectifies this with more control over the events and their handlers but, to my knowledge, it's not doable in VB6.  

VBA is particulary nasty on this front, as you can't do control arrays, either, as you have discovered.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

mikeydmAuthor Commented:
Thanks, Jim. I guess I'll have to do it that way.

Because VBA doen't support control arrays, you are not able to build a general event handler. But you can do it like this:

Private Sub TextBox1_AfterUpdate()
End Sub
Private Sub TextBox2_AfterUpdate()
End Sub

Private Sub DoUpdate(txt As TextBox)
    If txt.Text <> "" Then
        If Application.WorksheetFunction.IsNumber(txt) Then xDatum(CInt(Right(txt.Name, 1))) = txt Else txt = nil
    End If
End Sub

mikeydmAuthor Commented:
FYI, I made some mistakes in my last post. The additional function now reads:

Private Sub CheckForNumericEntry(i As Integer)
Dim stText As String, vTxt As Variant
  stText = UserForm1.Controls("TextBox" & i)
  If stText <> "" Then
    vTxt = Val(stText)
    If vTxt = stText Then
      xDatum(1) = stText
      MsgBox ("Numerical data required")
      stText = nil
    End If
  End If
End Sub
mikeydmAuthor Commented:
FYI, the "last post" that I mentioned in the previous post apparently didn't go through.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.