Change textbox color based on matching value in different textbox

C

CompleteNewb

Hi all. After some useless experimentation, here I am AGAIN. I swear,
every time I think I'll be able to work my way through something, I get
NOwhere.

I'm using Access 2003 SP2 on a Windows XP Home OS.

I have a form with several hundred textboxes on it. They are currently
unbound, but will eventually be bound, I think, depending on whether I can
do what I'm trying to do right now.

I have 1 specific textbox (named "checker" in its properties), and upon
clicking a button, I want to cycle through all the other textboxes, and if
the 2 leftmost characters in the box are the same as the 2 leftmost
characters in checker, I want to turn that textbox's back color to black.

I tried doing a For Each, as below:

Dim txtbx As TextBox
For Each txtbx In Me
If Left(txtbx.Value, 2) = Left(Me.checker.Value, 2) Then
txtbx.BackColor = black
Else
txtbx.BackColor = white
End If
Next txtbx

This actually turns all of the textboxes black, regardless of their values
as they compare to checker's values, then ends with a type mismatch. I
thought using .text instead of .value would fix the type mismatch, but then
I get a "can't refer to a property of a control unless it has the focus"
error, without anything changing color.

I can't figure out why everyhting would turn black, even if the 2 left
characters DON'T match what's in checker, and then I also don't understand
why I'd get a type mismatch (they're all just unbound textboxes, and like I
said, I tried using .text instead of .value, but that obviously was even
worse).

Can anyone tell me how I can do this?

Any help appreciated, and thanks for taking the time to read and (hopefully)
help.
 
G

Guest

I dont see how this code works at all, surely you need variables vbWhite and
vbBlack.
Then you also have the problem that your checker control is also a textbox
so will be hit in the loop.
Do any of the textboxes have less than 2 characters in them?
Do you know how to use debug to step through one statement at a time?

Its a weird thing you are doing, what is the point of it?

-Dorian
 
C

CompleteNewb

I appreciate your response.

I don't know much, but I'm curious as to why, if declaring the vbwhite and
vbblack variables are the problem, the presented code did indeed turn all
the textboxes' background color to black.

It seems if needing the colors to be declared as variables was the problem,
wouldn't I get an error or something before the textboxes turned black?

What I'm trying to do is turn the back color of all the textboxes on the
form (and I see your point here about how the checker control is ALSO a
textbox, and so I need to exclude it from the loop) to black if the leftmost
2 characters of their contents match the leftmost two characters in the
checker textbox.

Some of the textboxes may indeed have less than 2 characters. In fact, some
of them could be null (or ""). But in the past with similar projects I have
tested for that eventuality, and in the checking of whether or not the left
2 characters match, having null doesn't error out the code, it just seems to
recognize that Null or "" don't match what's in checker, and act accordingly
(ie. if a textbox is blank, its left 2 characters DON'T match, so it
continues).

Thanks again.
 
D

Dirk Goldgar

In
CompleteNewb said:
Hi all. After some useless experimentation, here I am AGAIN. I
swear, every time I think I'll be able to work my way through
something, I get NOwhere.

I'm using Access 2003 SP2 on a Windows XP Home OS.

I have a form with several hundred textboxes on it. They are
currently unbound, but will eventually be bound, I think, depending
on whether I can do what I'm trying to do right now.

I have 1 specific textbox (named "checker" in its properties), and
upon clicking a button, I want to cycle through all the other
textboxes, and if the 2 leftmost characters in the box are the same
as the 2 leftmost characters in checker, I want to turn that
textbox's back color to black.
I tried doing a For Each, as below:

Dim txtbx As TextBox
For Each txtbx In Me
If Left(txtbx.Value, 2) = Left(Me.checker.Value, 2) Then
txtbx.BackColor = black
Else
txtbx.BackColor = white
End If
Next txtbx

This actually turns all of the textboxes black, regardless of their
values as they compare to checker's values, then ends with a type
mismatch. I thought using .text instead of .value would fix the type
mismatch, but then I get a "can't refer to a property of a control
unless it has the focus" error, without anything changing color.

I can't figure out why everyhting would turn black, even if the 2 left
characters DON'T match what's in checker, and then I also don't
understand why I'd get a type mismatch (they're all just unbound
textboxes, and like I said, I tried using .text instead of .value,
but that obviously was even worse).

Can anyone tell me how I can do this?

Any help appreciated, and thanks for taking the time to read and
(hopefully) help.

Not every control on the form is a text box -- that's why you get the
type mismatch. Try this:

Dim txtbx As Access.Control

With Me.checker
For Each txtbx In Me.Controls
If txtbx.ControlType = acTextBox Then
If txtbx.Name <> .Name Then
If Left(txtbx.Value, 2) = Left(.Value, 2) Then
txtbx.BackColor = vbBlack
Else
txtbx.BackColor = vbWhite
End If
End If
End If
Next txtbx
End With
 
C

CompleteNewb

Worked FLAWLESSLY, Dirk, I thank you very much.

I have not used the With method (if method is the right word) before, and I
can see from it that I can trim down a lot of my previous code snippets in
other projects as well. Thanks for teaching me to fish!

The Complete Newb
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top