Code not working.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a status field which is auto updated via query. I want to stop users
from changing any data on sub forms (datasheets) when the status is changed
to Completed.
I have tried the following code but with no luck. This code has worked on
another database but only when the status field isnull.
Would be grateful if someone could help and point out where i am going wrong.


Private Sub Status_AfterUpdate()
If (Me!Status) = Completed Then
Me!Appointments.Enabled = False
Else
Me!Appointments.Enabled = True
End If


End Sub

Thanks in anticipation
 
Try encosing completed in speech marks

Private Sub Status_AfterUpdate()
If (Me!Status) = "Completed" Then
Me!Appointments.Enabled = False
Else
Me!Appointments.Enabled = True
End If
End Sub
 
I tried that but with no luck. I have also tried moving the "Completed" to
the beginning of the If statement but again no luck.
 
Have you tried placing the code on another event other than after update?
Maybe try it on the on click action of a button to test it works.

It that works then try placing the code somewhere else. e.g. if your query
is being required/run from a button place it after the line of code that
runs/requerys the query
 
have tried doing that and whilst it locked down the sub form it did it for
records that the status wasnt completed as well. Any ideas?
 
I have a status field which is auto updated via query. I want to stop users
from changing any data on sub forms (datasheets) when the status is changed
to Completed.
I have tried the following code but with no luck. This code has worked on
another database but only when the status field isnull.
Would be grateful if someone could help and point out where i am going wrong.

Private Sub Status_AfterUpdate()
If (Me!Status) = Completed Then
Me!Appointments.Enabled = False
Else
Me!Appointments.Enabled = True
End If


End Sub

Thanks in anticipation

1) A control's AfterUpdate event does NOT fire when that field is
updated using code or from a query update; only when you update the
control manually.

2) What is the datatype of the [Status] field?
If it is a Text datatype, then you must enclose the word Completed
within quotes.
If (Me!Status) = "Completed" Then

If [Status] is a Number datatype, i.e. a CheckBox or a Combo bound to
a number field, then
If Check Box, use If (Me!Status) = -1 Then (assuming checked means
completed. Use 0 if unchecked means completed.
If Combo Box, use If (Me!Status) = n Then (where n is whatever the
number of the bound column of the Combo box is).

3) In either event, see # 1 above.

4) Place the code in a command button (or any other) event that runs
the query, i.e.
DoCmd.OpenQuery "Your Update query"
DoEvents
Me.Refresh
If (Me!Status) = "Completed" Then
Me!Appointments.Enabled = False
Else
Me!Appointments.Enabled = True
End If


5) Also place the same If-Then code in the form's Current event.
After you move to the next record and come back, the code should work.
 
As pointed out, Completed is text and has to be enclosed in quotes. That and
Fred's # 5 is the key
here. Anytime you makes conditional changes, whether formatting the
appearance of controls or changing the properties of controls, you have
always have to include the code in the FormCurrent, otherwise whatever you've
done to one record will be carried over to any subsequent record you view.

If you set Enabled to False on a combobox, say, because it's meets certain
criteria on Record A then move to Record B, Enabled will still be set to
False, unless you place the criteria checking code in FormCurrent as well.
 
Back
Top