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
583 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
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

831 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