Enabling / Disabling txt boxes

A

Andy Roberts

I have a chk ox and a txt box on a form. I want the txt box to be disabled
when the chk box is unticked and enabled when the chk box is ticked.

Andy
 
F

fredg

I have a chk ox and a txt box on a form. I want the txt box to be disabled
when the chk box is unticked and enabled when the chk box is ticked.

Andy

Your form is in Single Form View, right?
Code the check box's AfterUpdate event:

Me.[TextControlName].Enabled = Me.[checkBoxName]

Place the same code in the form's Current event.
 
B

Biz Enhancer

Hi Andy,

In the Click Event of the Checkbox place this VBA


If Me.Checkbox = -1 Then
Me.txtbox.Enabled = True
Else
Me.txtbox.Enabled = False
End If

Regards,

Nick
 
A

Andy Roberts

Nick

This workd fine except when the form loads which has the chk box unticked
and the txt box enabled.

Andy
 
T

Tom Lake

Andy Roberts said:
Nick

This workd fine except when the form loads which has the chk box unticked
and the txt box enabled.

So set the desired configuration in the On Load event, too.

Tom Lake
 
A

Andy Roberts

Tom

Tried to sort that on the OnLoad event of the form, but doesn't seem to
work. I have multiple chkboxes which turn on and off txt boxes on the same
form if that makes a difference.

Andy
 
B

Biz Enhancer

Hi Andy,

In the OnLoad event set the condition of the checkbox and textbox.

Private Sub Form_Load

Me.Checkbox = 0
Me.Textbox.Enabled = True

End Sub

You can set all sorts of conditions for your form to open with so that the
user gets the form presented in the correct manner.
Setting the above VBA will always set the controls to those conditions which
is great for a new form but what happens when some existing records are
opened?
Therefore, we must set a conditional statement to take care of the what ifs.

Private Sub Form_Load

If me.Checkbox = -1 then
Me.checkbox = -1
Me.Textbox.Enabled = True
Else
Me.Checkbox = 0
Me.Textbox.Enabled = False
End if

Regards,

Nick.
 
A

Andy Roberts

Nick

Thanks for your time on this. I'm still having problems, but I'm sure its
me. I can get all the various controls to be disabled on form load, but
having trouble with the second bit of your code. My OnLoad event contains
the following code:

Private Sub Form_Load()
Me.cboWho.Enabled = False
Me.txtCostLost.Enabled = False
Me.txtDiff.Enabled = False
Me.txtWhyNot.Enabled = False

If Me.cboWho.Enabled = True Then
Me.txtCostLost.Enabled = True
Me.txtDiff.Enabled = True
Me.txtWhyNot.Enabled = True

Else
Me.cboWho.Enabled = True
Me.txtCostLost.Enabled = True
Me.txtDiff.Enabled = True
Me.txtWhyNot.Enabled = True
End If

End Sub

--
Regards

Andy
___________
Andy Roberts
Win XP Pro
Access 2007
 
B

Biz Enhancer

Hi Andy,

We're just about there.

With the conditional statement we don't want make any declarations of
control state before the If..Then statement. With your current VBA you are
setting the Me.cbowho.Enabled to false which means the If statement that
follows will aways be false. Secondly, because the If statement is always
false, the Else part of the statement reverses the original 4 conditions.
Tip: You can set breakpoints in your code to stop at a certain point to
check what the code is actually doing on the form.

So remove the first 4 conditions to start of with.
Then we need to set the conditions based on whether the form is a new record
or an existing one. This is where the If....Then statement comes in.

Given that in a new record the checkbox state is not set or is set to 0, we
can make the first statement by finding if the record is an existing one - If
me.checkbox = -1. -1 is the ticked state.

If Me.Checkbox = -1 Then
'set conditional states
Me.cboWho.Enabled = True
Me.txtCostLost.Enabled = True
Me.txtDiff.Enabled = True
Me.txtWhyNot.Enabled = True
Else
'conditions if checkbox is not ticked
Me.cboWho.Enabled = False
Me.txtCostLost.Enabled = False
Me.txtDiff.Enabled = False
Me.txtWhyNot.Enabled = False
End if

Hope that explains it a bit better.

Regards,

Nick.
 
A

Andy Roberts

Nick

I see where you are going now and am starting to understand. The problem I
see is that whilst a new record will have checkboxes unticked, some existing
records may also have checkboxes unticked so the way you are deciding
whether its new or exisitng may not work. Or have I missed something again?

How do I set breakpoints?

--
Regards

Andy
___________
Andy Roberts
Win XP Pro
Access 2007
 
D

Douglas J. Steele

To know whether it's a new record, use

If Me.NewRecord = True Then
' It's a new record
Else
' It's an existing record
End If
 
B

Biz Enhancer

Hi Andy,

It is kind of irrelevant whether the record is new or not because if the
existing record is opened and the checkbox is not ticked then you want
certain controls to be in a certain state, and if it is ticked then the
controls are in a different state.
I have however, assumed that a new record would have the checkbox unticked.

To set breakpoints, in the VBA window click on the vertical bar immediately
to the left of your code. It should create a brown spot and highlight that
line of code.
To remove breakpoints, click on the spot or in the menu Debug/clear all
breakpoints. I'm using 2003 but I can't see 2007 being that different.
The code will stop running on the breakpoint and require your input to
continue running the code. F5 should make it continue or click the green
arrowhead on the toolbar.

Regards,

Nick.
 
A

Andy Roberts

Nick

Thanks. You are a genius...all the controls work now and I see what you
mean about whether its a new record or not.

The next stage on is also stumping me. I have the chk boxes enabling
cboboxes (or in some cases a txt boxes) and this is fine. The cbo box has a
list of options as normal to select from. 2 Questions:-

How do I ensure the user selects an option from the cbo when it is enabled
and
How do I ensure the cbo / txt box is cleared of any value when disabled if a
mistake has been made.

--
Regards

Andy
___________
Andy Roberts
Win XP Pro
Access 2007
 
B

Biz Enhancer

Hi Andy,

You can run a check to see if conditions are met when the form is closed.

E.g.
Private Sub Form_Close

If me.textbox.enabled = true then
If Len(me.textbox)<1 then
Msgbox "Enter some text in textbox before_
proceeding",vbCritical,"Missing Data"
Exit Sub
End if
End if
Docmd.close acform, me.name
End sub

The Len function checks to see if the string in the textbox has more
than 0 characters
Or you could use
If me.cbo = 0 then
msgbox
end if
This assumes that the combobox is recording a number and that the
default is set as 0

if me.control.disabled = true
me.control = ""
End if

That one would blank a textbox but wouldn't work for a listbox or
combobox that has a hidden column. For those you would need to specify
the default value.

Regards,

Nick
 
Ã

ãì ÇáÔíÔí

hiiiiiiiiiiiiiiiii


þþßÊÈ "Andy Roberts said:
Nick

Thanks. You are a genius...all the controls work now and I see what you
mean about whether its a new record or not.

The next stage on is also stumping me. I have the chk boxes enabling
cboboxes (or in some cases a txt boxes) and this is fine. The cbo box has
a list of options as normal to select from. 2 Questions:-

How do I ensure the user selects an option from the cbo when it is enabled
and
How do I ensure the cbo / txt box is cleared of any value when disabled if
a mistake has been made.

--
Regards

Andy
___________
Andy Roberts
Win XP Pro
Access 2007
 

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