Using VBA to disable fileds in a form

A

apex77

I have a form called frmEvents
Text boxes called EventType which is a drop down, Cost, StartDate and
EndDate.
I need to grey out or disable Cost, StartDate and EndDate whenever EventType
= "Anniversary". I think VBA is the way, but if there is a simpler way I am
game.
Thanks.
 
D

Dirk Goldgar

apex77 said:
I have a form called frmEvents
Text boxes called EventType which is a drop down

<quibble>
If it's a "drop down" -- that is, a combo box -- then it's not a text box.
A text box is one type of control, a combo box is another.
, Cost, StartDate and
EndDate.
I need to grey out or disable Cost, StartDate and EndDate whenever
EventType
= "Anniversary". I think VBA is the way, but if there is a simpler way I
am
game.

You could do it with macros, but I find VBA easier to work with.

This is what I'd recommend. Create a function in the General section of the
form's code module, with code like this:

'------ start of code ------
Public Function EnableDisableControls()

On Error GoTo Err_Handler

If Me!EventType = "Anniversary" Then
Me!Cost.Enabled = False
Me!StartDate.Enabled = False
Me!EndDate.Enabled = False
Else
Me!Cost.Enabled = True
Me!StartDate.Enabled = True
Me!EndDate.Enabled = True
End If

Exit_Point:
Exit Function

Err_Handler:
If Err.Number = 2164 Then
' "You can't disable a control while it has the focus."
' Move the focus and try again.
Me!EventType.SetFocus
Resume
Else
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_Point
End If

End Function
'------ end of code ------

You'll need to call this function from both the combo box's AfterUpdate
event (where the value might have been changed) and from the form's Current
event (so that the controls are properly enabled/disabled as you move from
record to record). If you aren't using those events for anything else, you
can use an expression to call the function directly from the event's
property line, without actually creating an event procedure. Just set both
properties, on the Event tab of the relevant property sheet, to:

=EnableDisableControls()

If, on the other hand, those events already have event procedures, you can
just call the function from inside the event procedure. For example,

Private Sub Form_Current()

' ... do something else ...

' Enable controls according to EventType
EnableDisableControls

End Sub
 

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