Maccus
Maccus

Reputation: 31

Textbox ForeColor change in MS Access

Situation: I have a form in MS Access 2010 containing around 50 textboxes and combo-boxes that have their ForeColor set to 11250603 Gray on Load. What I want to do is create a single piece of succinct code that changes the ForeColor of the to vbBlack on individual selection.

I could use the GotFocus event for each text/combo but this is clumsy. I have seen other threads that are vaguely similar that have used DirectCast() but it's not something I know and I'm not even sure it will work in MS Access 2010. I have tried YouTube and searching threads here but haven't found a solution so far.

If somebody could point me in the right direction as to where I could find code or where I could adapt code, it would be much appreciated.

Upvotes: 0

Views: 1275

Answers (1)

Gustav
Gustav

Reputation: 55816

You would use WithEvents. An example is here:

Create Windows Phone Colour Palette and Selector using WithEvents

It contains this simple code:

Private Sub ClassTextBox_Click()

    ' Select full content.
    ClassTextBox.SelStart = 0
    ClassTextBox.SelLength = Len(ClassTextBox.Value)
    ' Display the clicked value.
    ClassTextBox.Parent!CopyClicked.Value = ClassTextBox.Value
    ' Copy the clicked value to the clipboard.
    DoCmd.RunCommand acCmdCopy

End Sub

to select and copy a value for any textbox clicked. You would modify that to instead set the ForeGround colour:

Private Sub ClassTextBox_Click()

    ' Set foreground colour.
    ClassTextBox.ForeColor = vbBlack

End Sub

In the Load event, you would set the foreground colour to your grey value.

Upvotes: 1

Related Questions