By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,336 Members | 2,315 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,336 IT Pros & Developers. It's quick & easy.

class

P: n/a
'class module
Public WithEvents clsTxt As TextBox
Private Sub clsTxt_Enter()
clsTxt.BackColor = VBA.ColorConstants.vbCyan
End Sub

'form module
Dim txt(1) As New Class1
Private Sub Form_Load()
Set txt(0).clsTxt = Me.Text0
Set txt(1).clsTxt = Me.Text1
End Sub

i want to change the backcolor of the textbox control when the user
focusing in the textbox control.
but the upper source code doesnot work.

*** Sent via Developersdex http://www.developersdex.com ***
Nov 20 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
I understand you want to change the BackColor of a Control in a Form
displayed in Single Form View to vbCyan when that Control receives the Focus
and revert to its original BackColor when it loses the Focus. If that is
not correct, post back and clarify.

You don't have to get "fancy" with class modules... besides which, I see
nothing to be triggered by the control receiving the focus in yours.

In the General Declarations section of the Form's module, put:

Dim lngSavedColor As Long

Then, still in the General section, add:

Private Function CyanBack()
lngSavedColor = Me.ActiveControl.BackColor
Me.ActiveControl.BackColor = vbCyan
Function
Private Function OldBack()
Me.ActiveControl.BackColor = lngSavedColor
End Function

And in the GotFocus event of each Control where you want this behavior:

= CyanBack()

And in the LostFocus event of each Control where you want the behavior:

= OldBack()

That said, a more common color for highlighting controls would be Yellow, or
Light Yellow, but, I admit, your cyan does show up.

Larry Linson
Microsoft Access MVP
"x taol" <to******@yahoo.comwrote in message
news:45*********************@news.qwest.net...
'class module
Public WithEvents clsTxt As TextBox
Private Sub clsTxt_Enter()
clsTxt.BackColor = VBA.ColorConstants.vbCyan
End Sub

'form module
Dim txt(1) As New Class1
Private Sub Form_Load()
Set txt(0).clsTxt = Me.Text0
Set txt(1).clsTxt = Me.Text1
End Sub

i want to change the backcolor of the textbox control when the user
focusing in the textbox control.
but the upper source code doesnot work.

*** Sent via Developersdex http://www.developersdex.com ***

Nov 21 '06 #2

P: n/a
On 21 Nov 2006 00:08:15 GMT, x taol <to******@yahoo.comwrote:

I'm not sure I fully follow your code, but it appears you think that
as long as you write the Form_Load code, once Text0.OnEnter occurs,
your code would automatically be called.

That's creative thinking, but it doesn't work like that. Read up on
WithEvents: it only works for ActiveX controls, not for a regular
textbox. Unfortunately Access doesn't have a way to break into the
event model like you want (unlike .Net).

Write your code in the Text0_Enter and Text1_Enter event procedures.
There of course you could call a central function to set the BackColor
and whatever else you want to do.

-Tom.

>'class module
Public WithEvents clsTxt As TextBox
Private Sub clsTxt_Enter()
clsTxt.BackColor = VBA.ColorConstants.vbCyan
End Sub

'form module
Dim txt(1) As New Class1
Private Sub Form_Load()
Set txt(0).clsTxt = Me.Text0
Set txt(1).clsTxt = Me.Text1
End Sub

i want to change the backcolor of the textbox control when the user
focusing in the textbox control.
but the upper source code doesnot work.

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '06 #3

P: n/a
You have to arm the objects on the form to run an event procedure when
the Enter event fires. You may want to give the class object a Control
property (any other property name should be fine too) that sets up the
wrapper control to handle the event. Something like:

-Air code, use with caution-

'*********Class module*********
Private WithEvents mTxt as TextBox

Public Property Set Control(txt as TextBox)
Set mTxt = txt
With mTxt
.OnEnter = "[Event Procedure]"
.....whatever other props you want to set....
End With
End Property

Public Property Get Control as TextBox
Set Control = mTxt
End Property

Private Sub mTxt_Enter()
mTxt.BackColor = vbCyan
End Sub

'*********Form declarations*********
Private mTxt(1) as TextBox

'*********Form module*********
Private Sub Form_Load()
For i = 0 to 1
Set mTxt(i) = New Class1
Next i

Set mTxt(0).Control = Me.Text1
Set mTxt(1).Control = Me.Text2
End Sub

Private Sub Form_Unload()
For i = 0 to 1
Set mTxt(i) = Nothing
Next i
End Sub

'*********End Code*********

You can obviously still use the Enter event in the form's module, but
remember that any code that runs in the form's module will execute
ahead of the code that executes in the custom class object that it
wraps.

More info here:
http://msdn.microsoft.com/library/de...sesobjects.asp

Nov 21 '06 #4

P: n/a
Works fine, here. I do this sort of thing with all sorts of little
widgets I've set up to do things standard controls don't do.

Tom van Stiphout wrote:
On 21 Nov 2006 00:08:15 GMT, x taol <to******@yahoo.comwrote:

I'm not sure I fully follow your code, but it appears you think that
as long as you write the Form_Load code, once Text0.OnEnter occurs,
your code would automatically be called.

That's creative thinking, but it doesn't work like that. Read up on
WithEvents: it only works for ActiveX controls, not for a regular
textbox. Unfortunately Access doesn't have a way to break into the
event model like you want (unlike .Net).

Write your code in the Text0_Enter and Text1_Enter event procedures.
There of course you could call a central function to set the BackColor
and whatever else you want to do.

-Tom.

'class module
Public WithEvents clsTxt As TextBox
Private Sub clsTxt_Enter()
clsTxt.BackColor = VBA.ColorConstants.vbCyan
End Sub

'form module
Dim txt(1) As New Class1
Private Sub Form_Load()
Set txt(0).clsTxt = Me.Text0
Set txt(1).clsTxt = Me.Text1
End Sub

i want to change the backcolor of the textbox control when the user
focusing in the textbox control.
but the upper source code doesnot work.

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.