gotfocus / lostfocus change control color

B

balu

dear friends.
i want the code that can change the control backcolor and font color on
gotfocus and lostfocus events on the form
at present im writing code to each and every control got / lost focus event
on the form which is doing well
but i want an common code which can do the job for whole form controls so
such code can be written only once and kindly advise to which form event such
code to
be attached.
 
B

Baz

A simple solution is to put the following code in a module:

'Module code begins

Option Compare Database
Option Explicit

Function GotFocus()

On Error GoTo HandleError

Screen.ActiveControl.BackColor = vbRed

EndOfSub:
Exit Function

HandleError:
If Err = 438 Then
Resume Next
Else
Err.Raise Err
Resume EndOfSub
End If

End Function
Function LostFocus()

On Error GoTo HandleError

Screen.ActiveControl.BackColor = vbWhite

EndOfSub:
Exit Function

HandleError:
If Err = 438 Then
Resume Next
Else
Err.Raise Err
Resume EndOfSub
End If

End Function
Sub FocusChanges(ByVal frm As Form)

On Error GoTo HandleError

Dim ctl As Control
For Each ctl In frm
ctl.OnGotFocus = "=GotFocus()"
ctl.OnLostFocus = "=LostFocus()"
Next

EndOfSub:
Exit Sub

HandleError:
If Err = 438 Then
Resume Next
Else
Err.Raise Err
Resume EndOfSub
End If
End Sub

'Module code ends

And put the following in the Open event for each form:

FocusChanges Me

This relatively simple solution suffers from the problem that it will
override any custom event handlers in a form for the GotFocus and LostFocus
events. A more complete solution (without this drawback) can be constructed
using class modules and event sinks, but this is an advanced technique which
many people struggle to understand.
 
B

balu

respected jaz,
kindly help me with this problom
table items
field item(pk)
''''''''''''''''''''''''''''''''''''''''''''
table entrycheck
field date
" item(fk)
" openingbalence
''''''''''''''''''''''''''''''''''''''''''''''''''
now i got an form(items) in form view and subform(entrycheck) in datasheet
view
i must confirm the user has taken the openingbalence for all the (items ) in
the field
item of the table items before the usercould press any command button .
i wnat to attach the code to the command button
how to do it please
 

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