Enabling A Button

F

Fakhruddin Zavery

Hello,

I want to have a button on a form (which would basically run an update
query) only enabled when I particular field on the form is entered,
otherwise it should be diasabled. This field is a simple value of Yes and
No. If its Yes then the button should come up else it should be Diasabled.

Will appreciate all the help.
Thanks and Regards
 
W

Warrio

Set your button by deafault to enabled = False.
and on the event change of your field, write:

Sub myField_Change()
myButton.enabled = (myField.Text = "Yes")
End Sub

'this means, when the user modifys the content of your field, the button
enables only if the text type is equal to Yes.
 
A

Allen Browne

Use the AfterUpdate event procedure of the check box to set the Enabled
proeprty of the command button.

You will also need to use the Current event of the form so that this happens
each time you move record. In this case, disabling the button won't work if
the button has focus, so you need error handling to avoid that case.

Aircode:
------------------------code starts-------------------------
Private Sub chk_AfterUpdate()
On Error Goto Err_Handler
Dim bEnabled As Boolean

bEnabled = Nz((Me.chk = True), False)

With Me.MyButton
If .Enabled <> bEnabled Then
.Enabled = bEnabled
End If
End With

Exit_Handler:
Exit Sub

Err_Handler:
Select Case Err.Number
Case 2164&, 2165& 'Can't disable/hide the control with focus.
Me.[SomeOtherField].SetFocus
Resume
Case Else
MsgBox "Error " & Err.Number & " - " & Err.Description
Resume Exit_Handler
End Select
End Sub

Private Sub Form_Current
Call chk_AfterUpdate
End Sub
------------------------code ends-------------------------
 
A

Allen Browne

I should have pointed out that you need to use the Undo event of the form as
well, so it gets reset correctly. The code is exactly the same, but must
read the OldValue of the check box, i.e.:
bEnabled = Nz((Me.chk.OldValue = True), False)

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Allen Browne said:
Use the AfterUpdate event procedure of the check box to set the Enabled
proeprty of the command button.

You will also need to use the Current event of the form so that this
happens each time you move record. In this case, disabling the button
won't work if the button has focus, so you need error handling to avoid
that case.

Aircode:
------------------------code starts-------------------------
Private Sub chk_AfterUpdate()
On Error Goto Err_Handler
Dim bEnabled As Boolean

bEnabled = Nz((Me.chk = True), False)

With Me.MyButton
If .Enabled <> bEnabled Then
.Enabled = bEnabled
End If
End With

Exit_Handler:
Exit Sub

Err_Handler:
Select Case Err.Number
Case 2164&, 2165& 'Can't disable/hide the control with focus.
Me.[SomeOtherField].SetFocus
Resume
Case Else
MsgBox "Error " & Err.Number & " - " & Err.Description
Resume Exit_Handler
End Select
End Sub

Private Sub Form_Current
Call chk_AfterUpdate
End Sub
------------------------code ends-------------------------

Fakhruddin Zavery said:
Hello,

I want to have a button on a form (which would basically run an update
query) only enabled when I particular field on the form is entered,
otherwise it should be diasabled. This field is a simple value of Yes and
No. If its Yes then the button should come up else it should be
Diasabled.
 

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