If Then Not working Properly

J

JamesJ

Using Access 2007.
I'm using the following to set a command button to disabled if the
Calendar control is set to Today and enabled when I select a date
other than today.
The button is disabled properly when I open the form but when I
select another date the button remains disabled.

Private Sub Form_Current()

If Me!ctlCal.Value = Date Then

Me.cmdToday.Enabled = False
Else
Me.cmdToday.Enabled = True

End If

End Sub

Also, in attempting to change colors the values for the color properties
of the different parts of the Calendar control use different value types
than
Access 2007 does for form objects, such as the Detail section and the
like.
How do I change colors in the Calendar control???

Any help will be appreciated
James
 
N

Noëlla Gabriël

Hi james,

your code is placed in the oncurrent event of the form. This occurs each
time another record gets the focus, not when you're updating the record
fields. You have to place the code in the afterupdate event of the calender
control.
 
B

BruceM

The Current event fires only when you first arrive at the record. If you
change the ctlCal value you need the same code in the After Update event of
ctlCal (assuming that is the name of a text box or other control).

You could do this all in one line:

Me.cmdToday.Enabled = (Me.ctlCal <> Date)

Replace <> with = if you are getting the opposite effect from what you
intend.

The parentheses are optional, I think. I use them in an attempt to show the
logic more clearly.
 
B

BruceM

I should have mentioned that if you are updating ctlCal by code rather than
by direct data entry, the After Update event will not run, and you will have
to find another way such as by adding the line of code to the procedure that
updates ctlCal.
 
J

JamesJ

I found no AfterUpdate so I created the property and added the
code. It works initially. In the OnLoad of the form I have: Me!ctlCal.Value
= Date
I set cmdToday to Disabled by default in design view. When I click on
another
date the button becomes enabled but after I click on it to select today,
today gets
selected but the button remains enabled. Don't know why.
I needed to ad Me!cmdToday.Enabled = False to the OnClick of the button.
So far that's the only way it seems to work.

James
 
J

JamesJ

I found no AfterUpdate so I created the property and added the
code. It works initially. In the OnLoad of the form I have: Me!ctlCal.Value
= Date
I set cmdToday to Disabled by default in design view. When I click on
another
date the button becomes enabled but after I click on it to select today,
today gets
selected but the button remains enabled. Don't know why.
I needed to ad Me!cmdToday.Enabled = False to the OnClick of the button.
So far that's the only way it seems to work.

James
 
B

BruceM

If ctlCal is the name of a control (text box?), it has an After Update
event. In form design view, click ctlCal to select it, click View >>
Properties, and click the Events tab on the Property Sheet. If you do not
see an After Update event listed you are in the wrong place.

If so, and if the user is modifying ctlCal directly (rather than by code),
the After Update event will run, and cmdToday will be enabled/disabled.

The form's Load event runs once, just after the form's Open event. It is
not typically where a control would be enabled/disabled except under special
circumstances.

The form's Current event runs when you first go to a record. It does not
run while you are working on a record. That is why you need to use a
different event to enable/disable the command button while you are working
on a record. You need both the After Update event as described, and the
Current event so that the command button is enabled/disabled appropriately
when you first arrive at a record.
 
J

JamesJ

Sorry, I should have been more explicit.
ctlCal is the Calendar Control.
Not sure how I'm going to do this yet. Instead of Enabling and Disabling
the button I might just have a message box pop up in the onclick of the
button if today's date is selected ctlCal otherwise got to today on the
calendar.

Thanks,
James
 
B

BruceM

If you are assuming what you want can't be done you are too quick with that
assumption. Does the calendar control have event procedures or macros?
Something must happen when you click a date in the calendar control.
Details will vary depending on which control you are using, but in any case
that event procedure or macro is where you can enable/disable the command
button, assuming it is a calendar control that lets you modify the code.
For instance, if the calendar control has an OK button or some such thing,
you can add the code there.
 
J

JamesJ

I selected the Calendar control and then clicked 'View Code'
Then created:
Private Sub ctlCal_AfterUpdate(). When I select a date other than today
the button becomes enabled.
Seems things work ok from this.
By default I set the command button to Enabled No and have
created a couple buttons to go to the next month and previous
month using Me!ctlCal.NextMonth and Me!ctlCal.PreviousMonth.
A line after each of the previous re-enables the command button
until I click it and set ctlCal to today and I then disable the command
button.
Seem to work ok

Thanks,
James
 

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