Making fields in a form enable/disable when check box selected

  • Thread starter Thread starter Martin Pigott via AccessMonster.com
  • Start date Start date
M

Martin Pigott via AccessMonster.com

I am fairly new to Access and need to make a travel request booking form. I
want to make certain fields enable or disable when for example the
Flight_Req_d check box is selected. I have managed to get the certain
fields to sort of work but have struck a problem that I could do with some
help.

Each field to do with 'Flights' are set so they are disabled on opening the
form initially from the field properties box. Then on clicking the
Flight_Req_d check box they all become enabled and data can then be
entered. Problem is that when I go to the next record all those fields will
be enabled although the check box is not selected. Also if I go back to
the record that had previously had flight data entered in, they are all
disabled. I was quite chuffed that I had actually got it working in the
first instance, but now I am stuck. Having read several threads, theres a
lot of clever people out there, that I am sure this is really easy to
solve.

The code attached to the Flight_Req_d check box I created, which I am sure
is really messy, but it sort of works is as follows:

Private Sub Flight_Req_d_Click()

Airline.Enabled = True
Departure_Airport.Enabled = True
Arrival_Airport.Enabled = True
Flights.Enabled = True
Flight_No_Out.Enabled = True
Flight_No_In.Enabled = True
Flight_Cost.Enabled = True
Flight_Booked.Enabled = True
Book_Ref.Enabled = True
Notes.Enabled = True

If Flight_Req_d = 0 Then
Airline.Enabled = False
Departure_Airport.Enabled = False
Arrival_Airport.Enabled = False
Flights.Enabled = False
Flight_No_Out.Enabled = False
Flight_No_In.Enabled = False
Flight_Cost.Enabled = False
Flight_Booked.Enabled = False
Book_Ref.Enabled = False
Notes.Enabled = False
End If
End Sub

Any help would gratefully be recieved.

Cheers

MP
 
You have to put code into an event to disable the fields for each record.
That's usually done in the "current" event.

The best way to work it is put an if statement in the current even something
like this

if me.newrecord then
code to disable all the appropriate controls (i.e., control.enable =
false)
else
if me.flight_req_d = (whatever value represents checked) then
code to enable all the appropriate controls
else
code to disable all the appropriate controls
endif
endif

me.newrecord in the current event indicates that this is a new record being
added to the database. When the record is first being added, you want the
controls disabled until your user has checked the Flight_Req_d checkbox.
If this is an existing record that you're looking at, then me.newrecord is
false and your code executes the second if statement. If the flight_req_d
checkbox is already checked (presumably you're saving the value of that
checkbox in your record) then you enable the controls. If it is not
checked, then the controls are disabled (until or unless the user clicks on
flight_req_d).

You of course still need the code in the click event of the flight_req_d
checkbox.

I see two problems with your flight_req_d code:

1. This should within an if statement so that the controls are either
enabled or not. As you have it, the controls will be enabled and then will
be disabled if the checkbox is not checked.

2. If the checkbox goes from a checked state to an unchecked state (i.e.,
the user first wanted a flight and now does not want a flight), you may need
to also clear out the values that are in the various controls. For example,
you record a departure airport, etc. and then they decide they don't want a
flight. If the record was previously saved, then you would still have in
your record all that data. I don't know how the rest of your program works,
but that could throw you off if you're doing some sort of simple report of
flight booking requests without looking at the flight_req_d checkbox.
 
Thanks for the help. With what you have said and also the next thread to
this one, I now fully understand where you are coming from. Cheers.
 
Next question is then, if as you said, the user decided that a flight was
no longer required and data had already been entered in any of the flight
detail fields, how would I get it to clear all the data if the flight req'd
check box was cleared?
 
Back
Top