Background color

mdcory

Registered User.
Local time
Yesterday, 19:02
Joined
Sep 28, 2004
Messages
73
I have kind of a stupid question. Is it possible to have a form that gets it values for the background colors of like textboxes and such from a table? Kind of working like a css in web pages. I kinda played around with it but not able to get it to work, but that could be my shear incompetence in VB, I can edit someone else's code to get it to work for me, but am totally lost starting from scratch.
Thanks,
Matt
 
I think so...

Sure - shouldn't be a problem. Provided that you've either determined what RGB values you want or the absolute value, you can place them into a table and open it up using a recordset. Something like this:

Code:
dim rstColor as recordset
dim strColorSQL as string

strColorSQL = "SELECT red, green, blue " & _
                   "FROM colors_tbl " & _
                   "WHERE colorname = " & strColorName & ";"

set rstColor = currentdb.openrecordset(strColorSQL)

me!ControlName.backcolor = rgb(rstColor!Red, rstColor!Blue, rstColor!Green)

Provided, of course, that you're inputting this code in the form module, otherwise the "Me" reference won't work. Also, I assumed you were talking about controls on a form, as a form doesn't have any property to alter backcolor. You could theoretically call up pictures that are all one color, but that's a different job.
 
Here's what I have:

Dim rstColor As Recordset
Dim strColorSQL As String

strColorSQL = "SELECT red, green, blue " & _
"FROM tblColorPref " & _
"WHERE HeaderBox = " & strColorName & ";"

Set rstColor = CurrentDb.OpenRecordset(strColorSQL)

Me!SampleName.BackColor = RGB(rstColor!Red, rstColor!Blue, rstColor!Green)


I get an error about a missing operator in the query
 
Last edited:
Yes, my apologies, here's a code correction:

Code:
strColorSQL = "SELECT red, green, blue " & _
              "FROM tblColorPref " & _
              "WHERE HeaderBox = " & """" & strColorName & """" &  ";"

The four sequential quotes designate a " character in the string. Obviously, just placing a " charater in a string that you're defining will signal an end to the text.

Something else that I should explain: The variable "strColorName" is a string variable that you'll have to pass to the function somehow. I had assumed it was partly built, but here's an example of what I would envision you using:

Code:
Public Sub BackgroundColor(strColorName as string)

dim rstColor as recordset
dim strColorSQL as string

strColorSQL = "SELECT red, green, blue " & _
              "FROM tblColorPref " & _
              "WHERE HeaderBox = " & """" & strColorName & """" &  ";"

Set rstColor = CurrentDb.OpenRecordset(strColorSQL)

Me!SampleName.BackColor = RGB(rstColor!Red, rstColor!Blue, rstColor!Green)

End Sub

You'll need to call this subroutine as BackgroundColor("Red") or something of the like. This subroutine will limit you, however, becaue you'll only be able to change the background color on one control. You could always add another variable to the parameters (the variables in the parenthesis) to recieve a control name, and you could then create a more flexible function:

Code:
Public Sub BackgroundColor(strColorName as String, _
                           strControlName as String)

dim rstColor as recordset
dim strColorSQL as string

strColorSQL = "SELECT red, green, blue " & _
              "FROM tblColorPref " & _
              "WHERE HeaderBox = " & """" & strColorName & """" &  ";"

Set rstColor = CurrentDb.OpenRecordset(strColorSQL)

Me(strControlName).BackColor = RGB(rstColor!Red, rstColor!Blue, rstColor!Green)

End Sub

Let me know if I've gone off on the wrong tangent.
 
More Help on the subject

Note for the above posts.
Quotes are only necessary to to vb where to connect/reconnect the string together. The query information IE what color name to use can be enclosed in '' apostraphe's. This saves time & aggrevation in searching down the 'Missing Operator' error.

EXAMPLE:


Since HTML uses named values for codes, a table could similarly be constructed and then use the dlookup() function. It would be simpler and more readable later on.

IE:
Text0.BackColor = DLookup("[HexValueAsLong]", "tblColorCodes", "[ColorNameAsString]='RED'")

If you have alot of controls that would require this change or don't want to explicitly state each control's name in the form's module, let me know. I have run into a similar issue before and have written a couple of routines for just such an occasion. It saves me hours and hundreds of lines of coding the same control over and over again.
 
Last edited:
Thanks for your help!!! I guess when I was imagining this I was hoping it would be simplier than what it appears it will be. Being as dumb as I am when it comes to VB, I was thinking I would be able to just pull the rgb number out of a table and stick that behind the .backcolor =. I guess I should kind of explain this a little bit more.
What I invisioned was having a form that I would be able to select different colors from a combobox for different controls. In my forms I have in my header with about 20 labels that the back color is transparent, and have a box behind that gives it the background color and the border around all of them. I thought that that way I could give the end user a way to easily change the colors of portions of the form to thier liking.
Again thank you for your help, I learn mre this way then I could ever learn from reading a book.
Matt
 
That's more along the lines of what I was thinking cpuSmoker! I am going to try that and see what happens.
Thanks!
 
Tell me about it.

I have been working with a guy from Texas who works for a major company in the rental industry. Using his software to run our business. Not only is it buggy, but you can barely read the coding to make a change where needed, not to mention explaining it back to them.

hang on a few minutes and I will devise an example for you.
 
Access Version

What version of access are you using?
 
2003 Thanks for your help, this has been bugging me all night
 
I've uploaded my work in progress. It is full of rookie mistakes and is no where near being completed. The chemist at work that I am doing this for can not make up his mind and we are designing this on the fly. Niether of us have much for experience in access. I have implemented alot from reading this and other forums, so I owe a big THANK YOU to alot of people for all the help.

Sign in as User with the password 1234, then click "Enter Data" then click the "Zinc Effluent" Icon and click the button on the next form and that will bring up one of the forms. That will kind of give you an idea of what I am working on.

Again Thanks to everyone!!!

download here
 
Last edited:
Try this example

this should help.
Will sign in as requested and check it out.
 

Attachments

Thanks again, playing around with it right now...
 
Did It Help?

Did what I send to ya help?

Who designed the rest of the database? It looks pretty damn good to me. :confused:
 
Yes it did help, thank you very much. I have been playing around with it today. I have done all of the database, with help form everyone here, either directly or indirectly. This is my first real database, I have played around alittle bit with access but never really made a finished product that work. I am starting to think I have jumped in with both feet with this database. It seems to get more and more complex everyday.

I can't thank you enough, and everyone else for the help. I will be sure to upload the finished product for everyone to see.

Thank you again,
Matthew
 

Users who are viewing this thread

Back
Top Bottom