VBA & Macro Problems

Fix VBA Type Mismatch When Setting an Access Control's BackColor

Tauseeq MagsiTauseeq Magsi Nov 22, 2026 868 views

Question details

Set an Access control's BackColor from RGB color values without a VBA type mismatch.

Fix VBA Type Mismatch When Setting an Access Control's BackColor
Product
Microsoft Access
Device & OS
not provided
Scenario
A VBA procedure needs to apply a color to a form control using red, green, and blue values.
Observed behavior
A string such as "RGB(230, 190, 255)" cannot be assigned directly to the numeric BackColor property.
Before you start

Identify the form control to color and the controls or variables holding its red, green, and blue components. Make sure each component is numeric, is not Null, and is between 0 and 255.

Solution 1Recommended

Call RGB with numeric components and assign the result to BackColor

Use this method when the red, green, and blue values are stored separately and may change.

1
Open the form's VBA code

Open the form in Design View, select the form, and use the Property Sheet's Event tab to open the code for an appropriate event, such as On Load.

2
Calculate and apply the color

In the event procedure, replace the text assignment with: Dim lngRGB As Long lngRGB = RGB(Me.MyColourR.Value, Me.MyColourG.Value, Me.MyColourB.Value) Me.txt1.BackColor = lngRGB

3
Use your control names and test

Replace MyColourR, MyColourG, MyColourB, and txt1 with your actual control names. Open the form in Form View and check that the target control displays the intended color.

Call RGB with numeric components and assign the result to BackColor
Text is not a function call: The quoted string "RGB(230, 190, 255)" is text. Call RGB without quotation marks so it returns the numeric color value required by BackColor.
Free Microsoft Office alternative

Use WPS Office for compatible documents and spreadsheets

WPS Office is a free, lightweight alternative with a familiar interface for working with Microsoft Office documents, spreadsheets, and presentations. It can help you move everyday office files to another suite, but it does not fix Access VBA errors or replace an Access database.

Edit spreadsheets with color and formatting toolsWork with Microsoft Word, Excel, and PowerPoint formats such as DOCX, XLSX, and PPTXUse a familiar interface for everyday office tasksMove compatible office files into a lightweight suite
microsoft office alternative - wps office

Frequently Asked Questions

Why does assigning "RGB(230, 190, 255)" cause a type mismatch?

The quotation marks make it a text string, while Access BackColor expects a numeric color value. Assign RGB(230, 190, 255) without quotation marks instead.

What type of variable should hold the RGB result in VBA?

Use a Long variable, for example: Dim lngRGB As Long. Assign the RGB function's result to it, then set Me.txt1.BackColor = lngRGB.

Can I use a hexadecimal value for BackColor?

Yes. For RGB(230, 190, 255), use the numeric literal &HFFBEE6 in the VBA assignment. Access color values use blue-green-red byte order in hexadecimal.

What if my RGB components are stored as text?

Extract the three components and validate that each is numeric and between 0 and 255, then pass the numeric values to RGB. Do not assign the complete text expression directly to BackColor.

Can WPS Office fix this Access VBA error?

No. Correct the code in Microsoft Access. WPS Office is an alternative for compatible documents, spreadsheets, and presentations, not a replacement for an Access database or its VBA code.