Highlighting record

H

Harmannus

Hallo,

I got the below code for hightlighting a record. Works fine but in stead of
putting it on every form i would like to put in a module and call it from
every form.

How can i extend the below code to achieve this? Tried some coding but I get
an error on the me.controls line. Any suggestions? Is it also possible to
extend the code to work with a combo box? Thanx for any tips.

if the control gets the focus changes the color to yellow
Function CtlGotFocus(ctl As Control)
On Error Resume Next
Set ctl = Screen.ActiveControl
ctl.BackColor = RGB(255, 255, 0)
End Function

' if the control loses the focus changes the color to back to white
Function CtlLostFocus(ctl As Control)
On Error Resume Next
Set ctl = Screen.ActiveControl
ctl.BackColor = RGB(255, 255, 255)
End Function

Function setHighlight(ctl As Control)
On Error Resume Next
For Each ctl In Me.Controls
If ctl.ControlType = 109 Then
ctl.OnGotFocus = "=CtlGotFocus"
ctl.OnLostFocus = "=CtlLostFocus"
End If
Next
End Function

Form_open
call setHighlight

Regards,
Harmannus
 
N

Neil

Hello,

Two things:

1) Your got focus and lost focus do not need to pass a control into them as
you are using Screen.ActiveControl (declare it locally). The format of these
could be changed to:

Function CtlGotFocus()
On Error Resume Next

Dim ctl As Control
Set ctl = Screen.ActiveControl
ctl.BackColor = RGB(255, 255, 0)
' You could even use Screen.ActiveControl.BackColor
' This would mean you dont need the 1st 2 lines of code
End Function

2) In the setHighlight function, again, no control needs to be passed into
the procedure. If you replace Me.Controls with the forms equivelent to the
Screen.ActiveControl property i think everything will work as you intend.
Replace your procedure with the one below to also include combos (i assume
you are checking textboxs to start with....)

Function setHighlight()
On Error Resume Next

Dim ctl As Control
For Each ctl In Screen.ActiveForm.Controls
If TypeOf ctl Is TextBox Or TypeOf ctl Is ComboBox Then
ctl.OnGotFocus = "=CtlGotFocus"
ctl.OnLostFocus = "=CtlLostFocus"
End If
Next

End Function

HTH,

Neil.
 
H

Harmannus

Hallo Neil.

Tried but still stuck in get it to work in "modHighlight": See below code:

' if the control gets the focus changes the color to yellow
Function CtlGotFocus(frmName As String, ctlName As String)
Dim frm As Form
Dim ctl As Control
Set frm = Forms(frmName)
Set ctl = frm.Controls(ctlName)
ctl.BackColor = RGB(255, 255, 0)
Set ctl = Nothing
Set frm = Nothing
End Function

' if the control loses the focus changes the color to back to white
Function CtlLostFocus(frmName As String, ctlName As String)
Dim frm As Form
Dim ctl As Control
Set frm = Forms(frmName)
Set ctl = frm.Controls(ctlName)
ctl.BackColor = RGB(255, 255, 255)
Set ctl = Nothing
Set frm = Nothing
End Function

Function HandleOpenForm(frm As Form)
On Error Resume Next
Dim ctl As Control
For Each ctl In Screen.ActiveForm.Controls
If TypeOf ctl Is TextBox Or TypeOf ctl Is ComboBox Then
ctl.OnGotFocus = "=CtlGotFocus"
ctl.OnLostFocus = "=CtlLostFocus"
End If
Next
End Function


In th on open_form event i use:
On Error Resume Next
Call HandleOpenForm(Me)


Any suggestions on what is wrong. Thanx in advance

Regards,
Harmannus
 
N

Neil

Hello,

If you are passing the form itself into the procedure the change the line
below and see what happens:

For Each ctl In Screen.ActiveForm.Controls

to....

For Each ctl In frm.Controls

If that doesnt work, remove the On Error statement, and advise what message
is desplayed along with the line that is highlighted. Then, I would be able
to pinpoint the problem you are having.

HTH,

Neil.
 

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