Google Answers Logo
View Question
 
Q: Excel 2000 VBA code question - Class as TextBox doesn't receive enter/exit event ( No Answer,   2 Comments )
Question  
Subject: Excel 2000 VBA code question - Class as TextBox doesn't receive enter/exit event
Category: Computers
Asked by: davidparks21-ga
List Price: $5.00
Posted: 25 Dec 2002 09:34 PST
Expires: 27 Dec 2002 10:37 PST
Question ID: 133291
Hi, I am writing a custom VBA macro which pops up a UserForm.
In this user form there are aprox. 20 text boxes which I create
programatically. Since there are a variable number of text boxes
created based on the state of the underlying worksheet, in order for
events to work for the text boxes I needed to create a class as
MsForms.TextBox. I have the following code (this is a snippit of what
I have, I have ommited the non-relavant code):


Public WithEvents MyTextBox As MSForms.TextBox
 
 <non-relavant code here>

Private Sub MyTextBox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
ByVal Shif As Integer)
 'This event DOES work!
 <non-relavant code here>

end sub

Private Sub MyTextBox_Enter()
 'this event does NOT work
 MsgBox "I've made it here"
end sub

Private Sub MyTextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
 'This event does NOT work
 Msgbox "I've made it here also"
end sub


The above code is all within a class module named MyTextBox. I use the
following code in a module to create the ~20 instances of the text
boxes. This all works properly as all 20 text boxes are created, and
keyboard (key down & up) events are passed, and handled properly, into
the event handler of the MyTextBox class (as illustrated in the code
above).

The following code is in a Module, and is used to create the text
boxes, and populates an array of MyTextBox's in order to keep
reference to those text boxes:

Set textboxarray(x).MyTextBox = CategoryForm.category.Controls.Add_
("Forms.TextBox.1", "txtbox", True)
            textboxarray(x).MyTextBox.Height = textBoxHeight
            textboxarray(x).MyTextBox.Width = textBoxWidth
            textboxarray(x).MyTextBox.left = textBoxPositionLeft
            textboxarray(x).MyTextBox.top = positionTop
            textboxarray(x).MyTextBox.Name = "txtbox" & x


The above code (as you probably guessed) is in within a loop which
populates the textboxarray, the textboxarray is initialized globaly
as:

 Dim textboxarray() As New MyTextBox


The problem, and question I now have is simple. Why, or more
importantly how can I receive the Enter/Exit events in my class. Other
events work in my class, but not the enter/exit events (when focus is
given and taken from the MyTextBox control). I have created a standard
text box and the enter/exit events work for that text box fine, but I
can't figure out why I don't get enter/exit events for the MyTextBox
class. Also in the VBA editor in excel, it has a pulldown list of all
the events when I'm editing the MyTextBox class. it seems to list all
events except for the Enter/Exit events. The excel VBA documentation
illustrates the use of the Enter/Exit events for a text box, and I use
it as illustrated in the documentation. Just it doesn't work with my
class.

Thank you,
David

Request for Question Clarification by mathtalk-ga on 26 Dec 2002 04:02 PST
Hi, davidparks21-ga:

Are you plannning to use the change of focus events (on enter, on
exit) to perform validation?  I think that Excel 2000 uses VBA6 and
incorporates the new Validate event (and a corresponding
CauseValidation property) to mange this.

regards, mathtalk-ga

Request for Question Clarification by mathtalk-ga on 26 Dec 2002 18:00 PST
Another request: Please post your class Init() code for MyTestBox (if any).

-- mathtalk-ga

Clarification of Question by davidparks21-ga on 27 Dec 2002 10:36 PST
Thanks everyone for the excellent comments. This is exactly what I was
looking for. I see now that these events don't work and I need work
around this by creating equivlent events using _click and _keypress
events.

Thanks everyone!
Answer  
There is no answer at this time.

Comments  
Subject: Re: Excel 2000 VBA code question - Class as TextBox doesn't receive enter/exit e
From: hammer-ga on 26 Dec 2002 05:20 PST
 
Hi davidparks21,

The Enter/Exit events are provided by the container class (the
UserForm), so WithEvents can't supply them to a custom class. You will
have to use a workaround using either Validation or catching the Enter
and Tab keys (which won't help if someone clicks to get to a different
control). Do a search on Google Groups using:
"MSForms.TextBox" event enter exit

You'll find some threads discussing this issue.

- Hammer
Subject: Re: Excel 2000 VBA code question - Class as TextBox doesn't receive enter/exit event
From: kennyh-ga on 26 Dec 2002 17:05 PST
 
From my experience of VB, you can try to use the event of 

Private Sub MyTextBox_KeyPress(KeyAscii as long)
 if keyascii = 10 or keyAscii = 13 then ' means the enter key or Vb?enter
  MsgBox "I've made it here" 
 end   'exit

 end
end sub 

 I am not quite sure about what you want to do. 
 Hope that the above piece of coding would be helpful for you.

 Kenny

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy