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?

TIA,
Mike
mikeydmAsked:
Who is Participating?
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)
      Else
        DirectCast(Ctl,TextBox) = nil
      End If
    End If
  End If
Next Ctl

Leo
0
 
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.

Mike
0
 
jimbobmcgeeCommented:
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.

J.

0

Experts Exchange Solution brought to you by ConnectWise

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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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

Mike
0
 
junglerover77Commented:
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()
    DoUpdate(TextBox1)
End Sub
Private Sub TextBox2_AfterUpdate()
    DoUpdate(TextBox2)
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

Jungle
0
 
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
    Else
      MsgBox ("Numerical data required")
      stText = nil
    End If
  End If
End Sub
0
 
mikeydmAuthor Commented:
FYI, the "last post" that I mentioned in the previous post apparently didn't go through.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.