Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing BackColor of current record

Status
Not open for further replies.

dixxy

Technical User
Mar 4, 2003
220
CA
Hello,

I am trying to change the back color of all the controls in the current record and having trouble.

Here is the code I am trying:

Code:
Private Sub Check64_Click()
    'Dim frm As Form
    Dim txtbox As TextBox
    Dim colec As Collection
    Dim ctr As Controls
    'Set frm = Forms!frmExtras.Form
For Each ctr In colec
If Me.Check64 = True Then
txtbox.BackColor = vbRed
End If
Next
End Sub

Can this be done?

Thanks,

Sylvain
 
Dim Ctrl As Control

For Each Ctrl In Me
If Me.Check64 = True Then
Ctrl.BackColor = vbRed
End If
Next
 
Sort of ...

You say that you want to change "... controls in the current record ...". Do you mean "on the current form?" 'Current record' is a database term and records do not generally contain controls. Perhaps you want something like this (I'm taking some liberal guesses about what you want from your sample code.)

Private Sub Check64_Click()
Dim ctr As Control
if Check64.Checked = vbChecked then
For Each ctr In Me.Controls
If TypeOf(ctr) Is TextBox then Then ctr.BackColor = vbRed
Next
End If
End Sub

This will set the background color on every textbox on the form to Red if the Checkbox 'Check64' is checked. If you want all controls (not just Text Boxes) then remove the "If TypeOf(..)" test. If you do that you will need some error handling like "On Error Resume Next" because some types of controls do not have a BackColor property and you'll generate an error if you try to set it.
 
Golom,
Yes I meant say all text boxes of the CURRENT RECORD. and your code gives me an error with 'if Check64.Checked = vbChecked then' and the 'If TypeOf(ctr) Is TextBox then Then ctr.BackColor = vbRed
' is red when i paste it in vb???

Thanks,

Sylvain
 
If TypeOf Ctrl Is TextBox Then
Ctrl.BackColor = vbRed
End If
 
Im

This is fine but it boes all the text boxes on the hole form...

My form is continious, I would like only the text boxes of the currrent record, the one the the user checks checkbox64...

Thanks,

Sylvain
 
To dixxy

Sorry about that

if Check64.Checked = vbChecked then

Should have been

if Check64.Value = vbChecked then

As to "CURRENT RECORD" ...

I'm not sure what that means. This being the VB Forum ... are you talking about a VB Form or an MS/Access form?

Re: "... is red what I paste? ..."

No. You paste vbRed

vbRed is a system defined constant evaluating to &HFF. Some of the other common system defined constants for colors are vbBlue, vbGreen, vbBlack, vbWhite, etc.
 
Ok. A couple of things. The reason that
"if Check64.Checked = vbChecked then"
barfs is that it should be Check64.Value instead of Check64.Checked.

When you say your form is "continuous" and broken up into "records" I have trouble understanding what you are up to. Perhaps you could explain what a "record" is to you. As golom mentions, it usually means a row in a database table. Do you have a form that represents multiple rows in a database table, or are you talking about something else?

If you want to set certain text boxes to red and not others, try assigning values to the Tag property of the text boxes you're working with, say "Record1", "Record2", etc. Then, you can use golom's technique to determine if the current control is a textbox, and if so, check the tag property to see if it's one of the ones you want to turn red.

HTH

Bob
 
This definately sounds like an Access/VBA continuous form (or subform) and as such should be posted in forum702 .

That being said, it can be done. But it's best that issue be handled in the Access Forms forum.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Sorry guys,

I just now realise that I posted my question in the vb forum, I always go to the Access Forms forum...it's my mistake...thanks for the help anyway.I will post in the other forum..


Thanks,

Sylvain
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top