Solved

Access 2013 Set Conditional format of multiple field in a form – use with a subform related to ID: 39845510

Posted on 2014-02-12
2
585 Views
Last Modified: 2014-02-12
I asked this question - Access 2013 Set Conditional format of multiple field in a form – use with a subform related to ID: 39845510 and got a great solution that works with my form.

As a related question, I'd like to enter the function into a module and make it available to any for or subform

Help with that would be appreciated.


Code from the 1st question response was :

Private Sub Form_Load()

For Each ctl In Me.Controls

      If ctl.ControlType = acTextBox Then

            ctl.OnGotFocus = "=ChangeColor('" & me.name & "', '" & ctl.Name & "'," & " True)"
            ctl.OnLostFocus = "=ChangeColor('" & me.name & "', '" & ctl.Name & "'," & " false)"

      End If
 Next

 End Sub
 
with this in a module

Function ChangeColor(frmName As String, ctlname As String, booFocus As Boolean)

    If booFocus = True Then
   
       Forms(frmName).Controls(ctlname).BackColor = vbGreen
             
    Else
   
        Forms(frmName).Controls(ctlname).BackColor = vbWhite
       
    End If
End Function
0
Comment
Question by:rogerdjr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 36

Accepted Solution

by:
PatHartman earned 500 total points
ID: 39854361
You need to create a standard module for the code.  it can't be in the class module of a form.

Public Sub SetColor(frm as Form)
For Each ctl In Me.Controls

      If ctl.ControlType = acTextBox Then

            ctl.OnGotFocus = "=ChangeColor('" & frm.name & "', '" & ctl.Name & "'," & " True)"
            ctl.OnLostFocus = "=ChangeColor('" & frm.name & "', '" & ctl.Name & "'," & " false)"

      End If
 Next
End Sub

Open in new window

Then the load event becomes
Private Sub Form_Load()
    Call SetColor(Me)
End Sub

Open in new window


All I did was to change the "Me" reference to "frm".  You have to send the form reference into the sub but once you do, you can access anything with frm. that you could with me.
0
 

Author Closing Comment

by:rogerdjr
ID: 39854646
Perfect - thnks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question