Fix VBA Type Mismatch When Setting an Access Control's BackColor
Question details
Set an Access control's BackColor from RGB color values without a VBA type mismatch.

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

Assign a numeric hexadecimal color value
Use a hexadecimal literal when the color is fixed and you do not need separate RGB components.
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.

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.




