Use one field to enable or disable another field (1 Viewer)

MarcMendel

Registered User.
Local time
Today, 04:00
Joined
Jun 10, 2013
Messages
11
I have a form with a checkbox (Is a Work Permit required?)and a date field (Work Permit Expiration date). I want the date field to be dimmed when the form opens. I want the focus to go to the checkbox first. The checkbox asks whether a youth is required to have a Work Permit. if the user checks the box (the youth needs a Work Permit), then I want the date field to be undimmed and available to the user to put in the Work Permit Expiration date.

I've tried using the checkbox afterupdate property to enable/disable the Work Permit Expiration date field, but no luck. What am I doing wrong? How do I make this work?

Thank you in advance!

Private Sub WorkPermit_AfterUpdate()
If Me.WorkPermit.Value = -1 Then
Me.WorkPermitExpiration.Enabled = True
Else: Me.WorkPermitExpiration.Enabled = False
End If
End Sub
 

Isskint

Slowly Developing
Local time
Today, 12:00
Joined
Apr 25, 2012
Messages
1,302
What you have should do the job - just lose the :

First thing is to set the WorkPermitExpiration.Enabled property to false with the form open in edit mode. Also make WorkPermit the first control on the form (right click the form>>Tab Order, then move the control order as necessary).

Then your afterupdate event should look like;

Private Sub WorkPermit_AfterUpdate()
If Me.WorkPermit.Value Then
Me.WorkPermitExpiration.Enabled = True
Else
Me.WorkPermitExpiration.Enabled = False
End If
End Sub

You will also need to include some code to reset WorkPermitExpiration.Enabled to false after entering the current record.
 

missinglinq

AWF VIP
Local time
Today, 07:00
Joined
Jun 20, 2003
Messages
6,420
You can do all of this from code; there's no need to set the Enabled Property in the Properties Pane. As Isskint said, in your original code, lose the Colon after Else:

Code:
Private Sub WorkPermit_AfterUpdate()
 If Me.WorkPermit.Value = -1 Then
   Me.WorkPermitExpiration.Enabled = True
 Else
   Me.WorkPermitExpiration.Enabled = False
 End If
End Sub

You'll also need the same thing in the Form_Current event, so that the formatting stays appropriate as you move from Record-to-Record.

Setting the Tab Order of WorkPermit to 1 will only insure that this Control is the first to get Focus when the Form first opens! If you're on, say, LastControl, and move to another Record, new or existing, the Focus is going to be set to LastControl, not to the Control that is set as Tab 1.

To insure that WorkPermit gets the Focus first, when moving to a Record, you need more code in the aforementioned Form-Current event. So, to do both of theses things, you'll need:

Code:
Private Sub Form_Current()
 
 WorkPermit.SetFocus
 
 
 If Me.WorkPermit.Value = -1 Then
   Me.WorkPermitExpiration.Enabled = True
 Else
   Me.WorkPermitExpiration.Enabled = False
 End If
 
End Sub


Linq ;0)>
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 21:00
Joined
Jan 20, 2009
Messages
12,866
What you have should do the job - just lose the :

Nothing wrong with the colon. It is legitimate throughout Access to combine lines into one. Indeed Access will add it automatically if you put anything on the line after the Else.

Personally though I prefer the Else to be on a line by itself for readibility.

Note that the sub can be reduced to a single line:
Code:
Me.WorkPermitExpiration.Enabled = Me.WorkPermit
 

missinglinq

AWF VIP
Local time
Today, 07:00
Joined
Jun 20, 2003
Messages
6,420
...Personally though I prefer the Else to be on a line by itself for readibility....

Code:
Me.WorkPermitExpiration.Enabled = Me.WorkPermit

I prefer not using this kind of code for the same reason! In the days, when a big hard drive was 20 mb, and we counted every character we used in code, that would have been a cracker-jack-hack! But with today's cheapest boxes having more memory than NASA had on-board Apollo 13, I really prefer clarity! OF course, your mileage may vary! :D

Marc: Glad we could help!

Linq ;0)>
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 21:00
Joined
Jan 20, 2009
Messages
12,866
I prefer not using this kind of code for the same reason! In the days, when a big hard drive was 20 mb, and we counted every character we used in code, that would have been a cracker-jack-hack!

Me.WorkPermitExpiration.Enabled = Me.WorkPermit

It isn't a "hack". The code is legitimate and perfectly clear. It avoids the clutter on two levels.

This is what the longwinded code is saying:
Code:
If {True/False} = True Then
    whatever = True
Else
    whatever = False

Why bother when the task is to set the property of a control to the value of another?

My code gets right to the point and says:
Code:
whatever = {True/False}

But with today's cheapest boxes having more memory than NASA had on-board Apollo 13, I really prefer clarity! OF course, your mileage may vary! :D

Never mind a PC. The computer system in a 1990s family car had more power than Apollo.
 

MarcMendel

Registered User.
Local time
Today, 04:00
Joined
Jun 10, 2013
Messages
11
What would cause an Access DB to ignore any and all code that I write?? Is there a switch or something that tells it to read the code I've written? A line of code inserted in the "Declarations" perhaps? My DB is not recognizing any of the code I've written!?
 

dpelizzari

Registered User.
Local time
Today, 07:00
Joined
Jun 10, 2010
Messages
26
Marc, kind of a nebulous question... are you adding VB code to a field, writing a module, or... if you are adding something to a fields or buttons event, is that event ever being done? Generally when I add code to an event, I use the wizard so it automatically creates the event (OnClick, Afterupdate, etc...). If you wrote a module, you will need to call it somewhere, again probably from an event... Might be a good idea to start a new thread about it too...
 

Users who are viewing this thread

Top Bottom