[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

Microsoft Access , End Sub Expected

I wrote a very basic VB program to change the color of a field upon click in. The code is as simple as below :


Private Sub Full_Name_Click()
Function SetActiveControlColour()
Dim ctl As Control

    For Each ctl In Me.Controls
        ctl.BackColor = vbWhite
    Next ctl
    Me.ActiveControl.BackColor = vbRed

End Function
End Sub

When I try to compile it, I receive an error message : End Sub Expected .
Any assistance will be very much appreciated.
0
user_expert
Asked:
user_expert
1 Solution
 
Boyd (HiTechCoach) Trimmell, Microsoft Access MVPCommented:
You have a function dim'ed inside a Sub.  Which one are you trying to use?

**WARNING*** You code will try to set the back color of all controls on the form including labels, etc. Is that what you really want?

Are you sure you do not want to use the On Enter event?

I think this is what you want:

Private Sub Full_Name_Click()

Call SetActiveControlColour

End Sub

Function SetActiveControlColour()

Dim ctl As Control

'* set all controls to White
    For Each ctl In Me.Controls
        ctl.BackColor = vbWhite
    Next ctl

'* Set Active Control Red

    Me.ActiveControl.BackColor = vbRed

End Function

Open in new window


Better would probably be something like this:

Function SetActiveControlColour()

Dim ctl As Control

'* set all Combo box, Text box, and List box controls to White
    For Each ctl In Me.Controls
 
        Select Case ctl.ControlType
            Case acComboBox, acListBox, acTextBox
                 ctl.BackColor = vbWhite
        End Select

    Next ctl

'* Set Active Control Red

    Me.ActiveControl.BackColor = vbRed

End Function

Open in new window

0
 
user_expertAuthor Commented:
Thank you very much theHighTechCoach. I will try it this evening and give you a feedback.
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now