Max Thorley
Max Thorley

Reputation: 173

Access VBA - Argument not optional - backcolor

I have a table where the user can select a colour scheme for their database

ID | Colour     | SelectedScheme
---|------------|--------------
1  | 136,207,39 | Yes

Then on my form I have the following on the current events tab

Dim RGBcolor As Long
RBGcolor = DLookup("Colour", "SelectedScheme")
Me.boxheader.BackColor = RGB(RGBcolor)

but I'm getting the Argument not optional error. any idea?

Upvotes: 0

Views: 551

Answers (3)

HansUp
HansUp

Reputation: 97101

The RGB() function expects 3 numbers, but your code supplies a single string value. That is the cause of the "Argument not optional" compile error.

Construct a new string which includes the RGB() function plus your string of numbers. Submit that new string to Eval() ...

Me.boxheader.BackColor = Eval("RGB(" & RBGcolor & ")")

Upvotes: 1

fthiella
fthiella

Reputation: 49049

The Dlookup function accepts these arguments:

  • FieldName: a field, calculation, control on a form, or function that you wish to return;
  • TableName: the set of records. This can be a table or a query name;
  • Criteria (Optional): It is the WHERE clause to apply to the TableName.

And the RGB functions accepts these three:

  • red: the red component of the color;
  • green: the green component of the color;
  • blue: the blue component of the color.

I would suggest you to use three different columns for the component color in your table:

ID | ColourR | ColourG | ColourB | SelectedScheme
---|---------|---------|---------|---------------
1  | 136     |207      | 39      | Yes

Your event can be written as this:

Me.boxheader.BackColor = RGB(
  DLookup("ColourR", "SchemaTableName", "SelectedScheme=TRUE"),
  DLookup("ColourG", "SchemaTableName", "SelectedScheme=TRUE"),
  DLookup("ColourB", "SchemaTableName", "SelectedScheme=TRUE")
)

this will select the color from the table SchemaTableName where the field SelectedScheme is set to true.

Upvotes: 1

Sergey S.
Sergey S.

Reputation: 6336

Arguments of RGB function are numeric, not string, and there should be 3 of them:

RGB(136,207,39)

You are trying to pass one string argument. You can use Split function for extracting color values from string:

Me.boxheader.BackColor = RGB(Split(RGBcolor,",")(0),Split(RGBcolor,",")(1),Split(RGBcolor,",")(2))

Upvotes: 2

Related Questions