Discussion Locked

Can Access 97 code apply to whole form?

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have a form in access 97 with many fields that all need the same code.

Basically, I have code to make zero's blend into the background color, on Form_Current.

Then I have an If Then Statement to make sure only zero's blend in. This same as in Form_Current, but is in AfterUpdate of the specific field.

Then I have a GotFocus for the field that changes the background while you are in the box so you can see your typing, if the number had been a zero.

And finally I have a LostFocus to change the background color back.

Before I add the code for the other 120 or so fields, is there and easier way?

I just want zeros to be invisible on the form.

(Actually, my users do, I could care less. ;) )

Loyd

5 replies

by sl-campbell

I would create a public sub proceedure in a Module that gets passed the control in question, and formats it accordingly. Then I would call this proceedure in the AfterUpdate and On Current events.

'Use this in the AfterUpdate event

'Begin Code---------------
Public Sub HideZeros(ByRef txt As TextBox)

If IsNumeric(txt.Text) Then
If CDbl(txt.Text)=0 Then
txt.ForeColor = txt.Backcolor
Else
txt.ForeColor=vbBlack

end If

End If
End Sub
'End Code---------------

Call this on the textbox event like:
HideZeros TextBox1 (replace name with correct one)

In the OnCurrent event use a For/Each loop:

'Begin Code---------------
Dim c As Control

For Each c In Me.Controls
If TypeOf c Is TextBox Then
HideZeros c
End If
Next c
'End Code---------------

Then on the KeyPress event just add something like:

'Begin Code---------------
TextBox1.ForeColor=vbBlack
'End Code---------------

replacing the name TextBox1 with the actual name that you have.

You could change this a little such as:

'Begin Code---------------
Public Sub HideZeros(ByRef c As Control)
If TypeOf c Is TextBox Then

If IsNumeric(c.Text) Then
If CDbl(c.Text)=0 Then
c.ForeColor = c.Backcolor
Else
c.ForeColor=vbBlack

endIf

End If
End IF
End Sub
'End Code---------------

This way in the TextBox event that this is called from you only need to call it the same for all text boxes and not have to type in the name, as in:
'Begin Code---------------
HideZeros Me.ActiveControl
'End Code---------------

If you do it this way then change the For/Each to:

'Begin Code---------------
Dim c As Control

For Each c In Me.Controls
HideZeros c
Next c
'End Code---------------

PS. I typed this out of my head with-out testing, but you should get the idea and can adjust accordingly to your needs.

This discussion is closed to new replies.