?
Solved

Excel VBA Class Creation

Posted on 2006-04-25
7
Medium Priority
?
509 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:mikeydm
7 Comments
 
LVL 8

Expert Comment

by:Leo Eikelman
ID: 16536783
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
 

Author Comment

by:mikeydm
ID: 16538793
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
 
LVL 16

Accepted Solution

by:
jimbobmcgee earned 1000 total points
ID: 16539167
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mikeydm
ID: 16539383
Thanks, Jim. I guess I'll have to do it that way.

Mike
0
 
LVL 6

Assisted Solution

by:junglerover77
junglerover77 earned 1000 total points
ID: 16540099
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
 

Author Comment

by:mikeydm
ID: 16540308
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
 

Author Comment

by:mikeydm
ID: 16540316
FYI, the "last post" that I mentioned in the previous post apparently didn't go through.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question