Greyout a field (textbox on a form) using a checkbox????

  • Thread starter Ben Radford via AccessMonster.com
  • Start date
B

Ben Radford via AccessMonster.com

Hi,

I have read the other posts but I can't seem to get it to work. I'm trying to grey out a textbox on a form by clicking on a checkbox. I can't seem to get it to work. My text box is called EmailAddressBox and the checkbox is named Yesno_email. Can anyone point me in the right direction as to what to do? I would be greatful!! Fairly new to this database design...


Cheers Ben
 
B

Brendan Reynolds

Private Sub Check3_AfterUpdate()

Me.Text0.Enabled = (Me.Check3)

End Sub

Where 'Check3' is the name of the check box and 'Text0' is the name of the
text box.

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
R

Rick B

You say you can't get it to work, what have you tried? What does it do that
you don't want?

You would need code in the "on update" event for the checkbox that says "if
true, then disable some field".
This would take care of checking that box on the current record.

You would need the same code in the "on current" event for the form. This
would take care of scrolling from one record to the next.

Post back your code, where you put it, and what the results were, if you
continue to need help.

Rick B


Ben Radford via AccessMonster.com said:
Hi,

I have read the other posts but I can't seem to get it to work. I'm trying
to grey out a textbox on a form by clicking on a checkbox. I can't seem to
get it to work. My text box is called EmailAddressBox and the checkbox is
named Yesno_email. Can anyone point me in the right direction as to what to
do? I would be greatful!! Fairly new to this database design...
 
J

Jeff Boyce

Ben

In the AfterUpdate event of your checkbox control, add something like:

Me!txtEmailAddress.Enabled = Me!chkEmail_YesNo

This should "set" the "enabled" property to whatever value the checkbox has.

(unproven "aircode" -- your syntax may vary)

--
Good luck

Jeff Boyce
<Access MVP>

Ben Radford via AccessMonster.com said:
Hi,

I have read the other posts but I can't seem to get it to work. I'm trying
to grey out a textbox on a form by clicking on a checkbox. I can't seem to
get it to work. My text box is called EmailAddressBox and the checkbox is
named Yesno_email. Can anyone point me in the right direction as to what to
do? I would be greatful!! Fairly new to this database design...
 
B

Ben Radford via AccessMonster.com

thanks sooo much, thats taken me like 2 days to get right!! awesome.
cheers Ben
 
B

Ben Radford via AccessMonster.com

ah actually there is a slight problem...

It greys out the correct textbox (email address) but it greys out the textbox (email address) period! I would like to be able to grey out the box depending on whether the box has been ticked for that particular individual.

Basically its an unsubscribe feature, but we want to keep individuals email address and thus just grey them out so they don't get picked up if a search was conducted. So when a contact wants their email address unsubscribed we just tick the checkbox and it grey's out just that email address not everyone else's as well. Is this clear enough?

Ben
 
B

Bruce M. Thompson

ah actually there is a slight problem...
It greys out the correct textbox (email address) but it greys out the textbox
(email address) period! I would like to be able to grey out the box depending
on whether the box has been ticked for that particular individual.

Having just a checkbox bound to a field in the table should suffice for this -
greying out the textbox will have no bearing on your record selection at the
time you generate your mailing list. You simply base the criteria in the query
for your mailing list on the desired value of the boolean field to which the
checkbox is bound.

If your only concern is that checking the box affects the textbox in all
records, simply add the appropriate version of the line of code provided by Jeff
to the form's "On Current" event procedure, as well:

'*******
Private Sub Form_Current()
'(Error handling has not been included in this example)
Me!txtEmailAddress.Enabled = Me!chkEmail_YesNo
End Sub
'*******
Basically its an unsubscribe feature, but we want to keep individuals email
address and thus just grey them out so they don't get picked up if a search
was conducted. So when a contact wants their email address unsubscribed we
just tick the checkbox and it grey's out just that email address not everyone
else's as well. Is this clear enough?

As I stated before, you need only specify the desired value of the boolean field
provided for this purpose in your query's criteria for that field in order to
filter out the unwanted records.
 
B

Ben Radford via AccessMonster.com

ok cool, yeah I still want to grey out the indivduals emails so that everyone can clearly see that its been unsubscribed... Yes I will have to set up the query to exclude those fields with a tick. I haven't got to that yet so I will check it out tomorrow..

thanks
Ben
 
B

Ben Radford via AccessMonster.com

Bruce,

I've tried your example and put it in the OnCurrent but and error message comes up and it asks you to de-bug the code. I still have left the other bit of code for the greyout procedure in the Onclick event.

Ben
 
B

Ben Radford via AccessMonster.com

any ideas why it isn't working?

The OnClick works I think its to do with the OnCurrent

Ben
 
B

Bruce M. Thompson

any ideas why it isn't working?

Did you make sure that the object names match those in your project? Post your
code (the full procedure including the "Private Sub" and the "End Sub" for both
the "On Click" event procedure and the "On Current" event procedure.
The OnClick works I think its to do with the OnCurrent

There must be a difference in the code between the two procedures
 
B

Ben Radford via AccessMonster.com

This is the code for the 'greyout' of the EmailAddress field on the form:

Private Sub Yesno_email_Click()

Me.EmailAddress_textbox.Enabled = (Not Me.Yesno_email)


End Sub

And this works fine. But this bit dosen't work -

Private Sub Form_Current()

Me!txtEmailAddress.Enabled = Me!chkEmail_YesNo

End Sub

I put it in the OnCurrent for the check box but an error message comes up saying debug etc. Do I have to replace the other code with the new one or use both of them together??

cheers Ben
 
B

Brendan Reynolds

One possibility is that the text box may have the focus when the code in the
current event runs. You can't disable a control while it has the focus. Try
moving the focus to the check box (or another control on the form if you
prefer) before attempting to disable the text box ...

Private Sub Form_Current()

Me!chkEmail_YesNo.SetFocus
Me!txtEmailAddress.Enabled = (Me!chkEmail_YesNo)

End Sub

Note also the parentheses I've placed around the reference to the check box
control. There was a bug in at least some versions of Access that could
cause Access to fail to close properly if you had an implicit reference to
the value of a check box in the code, and using parentheses like this is one
method of avoiding that bug. I don't know if the bug persists in recent
versions, so the parentheses may not be essential, but they certainly won't
hurt.

If this doesn't solve the problem, tell us what the error message says.

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
B

Ben Radford via AccessMonster.com

still no joy I'm afraid, this is what the error message says -

Run-time error '2465':

MS Access can't find the the field 'chkYesno_email' referred to in your expression.

etc etc

Ben
 
J

Jeff Conrad

Ben,

I believe the root of the problem is that you are just copying/pasting the code given
to you by Brendan and Bruce, but not making changes to account for *your* actual control names.

This is exactly what you posted in an earlier message:
Begin Quote:This is the code for the 'greyout' of the EmailAddress field on the form:

Private Sub Yesno_email_Click()

Me.EmailAddress_textbox.Enabled = (Not Me.Yesno_email)

End Sub

And this works fine. But this bit dosen't work -

Private Sub Form_Current()

Me!txtEmailAddress.Enabled = Me!chkEmail_YesNo

End SubEnd Quote

Do you see the difference in CONTROL names?
In one spot you have EmailAddress_textbox and the other txtEmailAddress.
For the check box you have Yesno_email in one spot and chkEmail_YesNo in the other.

Access is totally confused right now!
Brendan then provided this bit of code:
Begin Quote:private Sub Form_Current()

Me!chkEmail_YesNo.SetFocus
Me!txtEmailAddress.Enabled = (Me!chkEmail_YesNo)

End SubEnd Quote

You then said:
Run-time error '2465':

MS Access can't find the field 'chkYesno_email' referred to in your expression.

Do you see the issue?
If you just copied the code, but did not adjust it for your actual control names,
then Access naturally cannot find the control you are referring to.
Step back a minute and take a hard look at the code and make adjustments
for the control names you actually have.

So ***IF*** your two controls are named:
EmailAddress_textbox
Yesno_email

then Brendan's code should look like this in the Current event of the form:

Private Sub Form_Current()

Me!Yesno_email.SetFocus
Me!EmailAddress_textbox.Enabled = (Me!Yesno_email)

End Sub

BUT, double check the control names first!
If you are still having problems, please post back with the
actual control names on the form.

BTW, for future reference, it is usually good practice to avoid having spaces in control names
 
B

Bruce M. Thompson

still no joy I'm afraid, this is what the error message says -
Run-time error '2465':

MS Access can't find the the field 'chkYesno_email' referred to in your
expression.

Jeff hit the nail on the head. You need to make sure that the line of code you
are using is the same in both the "OnClick" event procedure of the control and
the form's "OnCurrent" event procedure. You specified that the following worked
in the control's code:

Me.EmailAddress_textbox.Enabled = (Not Me.Yesno_email)

So, just use that same line in the form's "OnCurrent" event procedure.
 
B

Bruce M. Thompson

Me.EmailAddress_textbox.Enabled = (Not Me.Yesno_email)

Er ... that should be:

Me.EmailAddress_textbox.Enabled = (Not Me.Yesno_email.Value)
....or...
Me.EmailAddress_textbox.Enabled = Not (Me.Yesno_email)

Brendan caught that one earlier and I'm reinforcing his suggestion to avoid the
"boolean bug". <g>
 
B

Ben Radford via AccessMonster.com

Thanks guys you were quite right, I didn't realise that txt and textbox meant the same thing, oh well you learn something new everyday. Now theres a new problem. The EmailAddress field when told to 'greyout' and when you go to the next contact in the form the 'greyout' disapears however when you then return to the original contact the EmailAddress field doesn't greyout again, i.e its not display the greyout even though the check box is ticked. I was hoping to try and make contacts email addresses greyout depending on whether their 'unsubscribe' checkbox had been ticked. This is purley for a visual thing for users to see they have unsubscribed easily but I will set queries up for filtering/searching later. Anyone know what the problem might be? Sorry I'm not very good at VB... Many thanks for all you help up to this point btw, very kind of you....

Ben
 
B

Bruce M. Thompson

Thanks guys you were quite right, I didn't realise that txt and textbox meant
the same
thing, oh well you learn something new everyday. Now theres a new problem. The
EmailAddress field when told to 'greyout' and when you go to the next contact
in the
form the 'greyout' disapears however when you then return to the original
contact the
EmailAddress field doesn't greyout again, i.e its not display the greyout even
though the
check box is ticked. I was hoping to try and make contacts email addresses
greyout
depending on whether their 'unsubscribe' checkbox had been ticked. This is
purley for a
visual thing for users to see they have unsubscribed easily but I will set
queries up for
filtering/searching later. Anyone know what the problem might be? Sorry I'm
not very
good at VB... Many thanks for all you help up to this point btw, very kind of
you....

Is your form displaying single records or continuous records (multiple records
can be seen on the screen at one time)? This technique does not work very well
with continuous forms because the focus can be on a different record than the
one you are viewing. The record with the focus (the "current" record) will
determine the enabled state of the textbox, so in continuous mode all instances
of the textbox will be either enabled or not depending on the value of the
"unsubscribe" checkbox in the "current" record. If you click on the record you
are currently viewing in continuous view, the enabled state should be
appropriate for that record.

If the above doesn't describe what you are experiencing, let us know.
 

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