Declaring a Declaration?

  • Thread starter faapa via AccessMonster.com
  • Start date
F

faapa via AccessMonster.com

Hi there

I'm trying to create a 'rule' (declaration?) so that when a user has SetFocus
on a textbox, the BorderWidth expands to 2. When the textbox loses focus,
it's gone back to 1. As i have ALOT of textboxes, i want to avoid writitng
code in each textboxes gotfocus/lostfocus - does anyone know if/how this
activity can be carried through ALL forms??

Does anyone have any suggestions?
 
B

Baz

faapa via AccessMonster.com said:
Hi there

I'm trying to create a 'rule' (declaration?) so that when a user has SetFocus
on a textbox, the BorderWidth expands to 2. When the textbox loses focus,
it's gone back to 1. As i have ALOT of textboxes, i want to avoid writitng
code in each textboxes gotfocus/lostfocus - does anyone know if/how this
activity can be carried through ALL forms??

Does anyone have any suggestions?

I would do this by creating a class module (let's call it clsForm) which
takes a form as a property, and another class module (say, clsTextBox) which
takes a text box as a property.

In its Form property code, clsForm loops through the form's controls
collection, and for each text box it finds it instantiates an object of the
clsTextBox class, sets it's TextBox property, and adds it to a module-level
collection.

The clsTextBox class would have event sinks for the text box's GotFocus and
LostFocus events, which is where you would manipulate the borders.

Once you've written the two class modules, all you need to do in each form
is, in its Open event, instantiate a module-level object of the clsForm
class, and set it's Form property.

Sorry, I don't have time to actually write the code.
 
F

faapa via AccessMonster.com

Thanks for the explaination - from what i understand, it seems i need to
create a new Module and call it in the form...im not that bad in coding but i
was hoping someone could give me alittle guidnace on what phrases/words to
use when creating this 'requirement' ? pleeease :)
 
B

Baz

faapa via AccessMonster.com said:
Thanks for the explaination - from what i understand, it seems i need to
create a new Module and call it in the form...im not that bad in coding but i
was hoping someone could give me alittle guidnace on what phrases/words to
use when creating this 'requirement' ? pleeease :)

Hopefully someone has time to give you some more detail, but I'll check back
later if I have more time.
 
B

Baz

faapa via AccessMonster.com said:
Thanks for the explaination - from what i understand, it seems i need to
create a new Module and call it in the form...im not that bad in coding but i
was hoping someone could give me alittle guidnace on what phrases/words to
use when creating this 'requirement' ? pleeease :)

OK, I'm back:

Here's the code for clsForm:

'Code begins here

Option Compare Database
Option Explicit

Dim mfrm As Form
Dim mcolTextBoxes As New Collection

Property Set Form(ByVal Form As Form)

Dim ctl As Control
Dim txt As clsTextBox

Set mfrm = Form

For Each ctl In mfrm.Controls
If ctl.ControlType = acTextBox Then
Set txt = New clsTextBox
Set txt.TextBox = ctl
mcolTextBoxes.add txt, ctl.name
End If
Next

End Property

'Code ends here

Here's the code for clsTextBox:

'Code begins here

Option Compare Database
Option Explicit

Private WithEvents mtxt As TextBox

Property Set TextBox(ByVal txt As TextBox)

Set mtxt = txt
mtxt.GotFocus = "[Event Procedure]"
mtxt.LostFocus = "[Event Procedure]"
 
K

Keith Wilby

Baz said:
OK, I'm back:

Hi Baz,

I've read your code with interest but being a total newbie at class modules
I'm struggling to understand what it does. I'm reasonably comfortable with
VBA generally - is it fair to say that, as an alternative method, you could
loop through the controls in a stored function by calling it from the form
and passing the form object to the function?

Any chance you could give a numpty explanation of how your code works?
Always keen to lean new methods :)

Keith.
 
B

Baz

Keith Wilby said:
Hi Baz,

I've read your code with interest but being a total newbie at class modules
I'm struggling to understand what it does. I'm reasonably comfortable with
VBA generally - is it fair to say that, as an alternative method, you could
loop through the controls in a stored function by calling it from the form
and passing the form object to the function?

Any chance you could give a numpty explanation of how your code works?
Always keen to lean new methods :)

Keith.

Hi Keith,

First of all, try thinking of an object created from a class module as being
like any other object, say a form, except it doesn't have a user interface.
When you "instantiate" an object from a class module, you are simply
creating an object, as in the statement:

Set mobjForm = New clsForm

How the object behaves is determined by the code in the class module. Like
any object, it can have properties (Let and Set denoting properties which
can have a value assigned by a caller, Get denoting a property which is
returned to a caller). In this case, clsForm has a Set property which is a
form, so this code says "OK, gimme a new object and tell it that it's Form
is me":

Set mobjForm = New clsForm
Set mobjForm.Form = Me

Within the object, there is a loop which, as I'm sure you can see, finds all
of the text boxes on the supplied form. For every text box, another object
is created, this time based on the class clsTextBox. Because I want all of
these objects to stay in existence at the same time (I don't want them going
out of scope) each one is added to a user-defined collection.

So, what I finish up with is an object of the class clsForm, and within that
a collection of objects of the class clsTextBox, representing all of the
text boxes on the form.

Now the clever bit: if you create an object variable of a specific type
(e.g. TextBox), and declare it "WithEvents", you can "sink" it's events i.e.
as well as, say, the text box's regular GotFocus event procedure, you can
write another procedure (an event sink) which also responds to that event
even though it isn't in the Access form's module. You can see this in the
class module clsTextBox. By this method, I can write a generic event
handler which will fire for ANY text box on a form. It doesn't replace the
regular event (e.g. GotFocus), it runs in addition to it.

I hope that helps. For these kinds of advanced techniques, I can't
recommend highly enough the Access Developers' Handbook and the VBA
Developers' Handbook from Sybex: expensive, but worth every penny.
 
K

Keith Wilby

Baz said:
mtxt.GotFocus = "[Event Procedure]"
mtxt.LostFocus = "[Event Procedure]"

Think I found a typo here.

mtxt.OnGotFocus = "[Event Procedure]"
mtxt.OnLostFocus = "[Event Procedure]"

Keith.
 
B

Baz

Keith Wilby said:
Baz said:
mtxt.GotFocus = "[Event Procedure]"
mtxt.LostFocus = "[Event Procedure]"

Think I found a typo here.

mtxt.OnGotFocus = "[Event Procedure]"
mtxt.OnLostFocus = "[Event Procedure]"

Keith.

Very likely, I didn't actually test it!
 
K

Keith Wilby

<snip>

Thanks very much indeed for posting that response Baz, I will ponder it over
the next day or so and hopefully the penny will drop.

Regards,
Keith.
 
B

Baz

Keith Wilby said:
Baz said:
Keith Wilby said:
mtxt.GotFocus = "[Event Procedure]"
mtxt.LostFocus = "[Event Procedure]"

Think I found a typo here.

mtxt.OnGotFocus = "[Event Procedure]"
mtxt.OnLostFocus = "[Event Procedure]"

Keith.

Very likely, I didn't actually test it!

Posted for info, not criticising! :)

That's OK Keith, I didn't read any criticism into it :)
 
K

Keith Wilby

Hi again Baz ... I think I understand most of what you've said, just one
question so far:
First of all, try thinking of an object created from a class module as
being
like any other object, say a form, except it doesn't have a user
interface.
When you "instantiate" an object from a class module, you are simply
creating an object, as in the statement:

Set mobjForm = New clsForm

So are we saying that clsForm is effectively a template from which you can
create instances of the object at run-time, ie mobjForm?

Thanks.

Keith.
 
B

Baz

Keith Wilby said:
Hi again Baz ... I think I understand most of what you've said, just one
question so far:


So are we saying that clsForm is effectively a template from which you can
create instances of the object at run-time, ie mobjForm?

Thanks.

Keith.

Yes Keith, that's exactly it. The biggest mistake people make trying to
understand object-oriented programming (or at least object-based
programming, since VBA is not truly object-oriented) is to confuse classes
with objects, but you've got it in one.

It's exactly the same with Access objects, e.g. forms, but most of the time
you don't really notice it. A form's module IS a class module, and when you
open a form you are creating an object (or "instance") of that class. And
you can create lots of them at the same time if you want. DoCmd.OpenForm
just does it once, but you can do this instead:

Dim frm1 As Form_frmSomeForm
Dim frm2 As Form_frmSomeForm

Set frm1 = New Form_frmSomeForm
Set frm2 = New Form_frmSomeForm

Which gives you two instances of the same form.
 
K

Keith Wilby

Baz said:
Yes Keith, that's exactly it.

Way-hey, there's a first! ;-)
The biggest mistake people make trying to
understand object-oriented programming (or at least object-based
programming, since VBA is not truly object-oriented) is to confuse classes
with objects, but you've got it in one.

It's exactly the same with Access objects, e.g. forms, but most of the
time
you don't really notice it. A form's module IS a class module, and when
you
open a form you are creating an object (or "instance") of that class. And
you can create lots of them at the same time if you want. DoCmd.OpenForm
just does it once, but you can do this instead:

Dim frm1 As Form_frmSomeForm
Dim frm2 As Form_frmSomeForm

Set frm1 = New Form_frmSomeForm
Set frm2 = New Form_frmSomeForm

Which gives you two instances of the same form.

Thanks again Baz. I did a bit of digging at the weekend and one of my VBA
books goes into great detail on the subject so it's time I did a little
reading.

Regards,
Keith.
 

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

Top