cannot change date in a feild

rainbows

Registered User.
Local time
Today, 15:43
Joined
Apr 21, 2017
Messages
428
hi,

I have a field called "target date " after the date has been entered I would like to lock that field so it cannot be updated or change

i have looked at many threads but cannot find one to help me
any help aprreciated

steve
 
in the form design, form properties, events, ON CURRENT event
add this code to lock the field if it is filled:

Code:
sub Form_OnCurrent()
   txtTargDate.Locked = Not IsNull(txtTargDate)
end sub
 
hi ,

thanks for your help

not sure if I have done correct but I have put copied this into the on current event

Code:
Sub Form_OnCurrent()
   txtTarget Date.Locked = Not IsNull(txtTarget Date)
End Sub

and I get a message saying invalid syntax


thanks for you help

steve
 
As you have a space in your control name, you need to wrap it in [] brackets
See if this works

Code:
Sub Form_OnCurrent()
   me.[txtTarget Date].Locked = Not IsNull(Me.[txtTarget Date])
End Sub

OR much better, remove the space renaming it as txtTargetDate
If so, no [] needed
 
hi,
I have gone to design in my form
went to ON current
then not sure if I am doing it correctly or not
i then select event procedure and pasted you marco in but change the subheading

see below

Code:
Private Sub Form_Current()
'Sub Form_OnCurrent()
   Me.[txtTarget Date].Locked = Not IsNull(Me.[txtTarget Date])
End Sub

but this did not work
i get a message saying micro soft cannot find this object

thanks for you help
 
hi

this is the error message I get now

error 2645
cannot find the field '|' 1 refered to in your expression

steve
 
Well done - didn't notice the Form_Current was wrong

I've just tried this on a bound date field - even named it 'txtTarget Date' with a space (BAD IDEA) and added colour formatting to confirm it worked

Code:
Private Sub Form_Current()
    Me.txtTarget_Date.Locked = Not IsNull(Me.txtTarget_Date)
    If Me.txtTarget_Date.Locked Then
        Me.txtTarget_Date.BackColor = vbRed
    Else
        Me.txtTarget_Date.BackColor = vbYellow
    End If
End Sub

2 points:
1. Access handled the space using an underscore instead of [] brackets.
Both work, though I still say get rid of spaces in control names.

2. When you changed from OnCurrent to Current, have you checked the form property sheet to check it shows [Event Procedure] for the on current event. If its not there, add it
 
hi ridders

I have added your last one you helped me with . I did take all the spaces except that one which I have now done
please see attached
steve
 

Attachments

Rainbows . i am just a interested observer and notice your txt target date is named "Date Due " and your code should reflect this field name not the control name which differs

Regards Ypma
 
HI

YES ridders kindly help me with another problem when the field was called " due date "
I was asked to change it to target date

but no matter what I change it to it still errors

steve
 
Ah I remember this now from a previous thread!
ypma has hit this on the head - the control name is 'Due Date' (with a space)
So the code needs to be:

Code:
Private Sub Form_Current()

    Me.Due_Date.Locked = Not IsNull(Me.Due_Date)
    
  [COLOR="DarkGreen"]  'remove the following once you've observed its effect or it will confuse you[/COLOR]
    If Me.Due_Date.Locked Then
        Me.Due_Date.BackColor = vbRed
    Else
        Me.Due_Date.BackColor = vbYellow
    End If
  
End Sub

Because its a continuous form, the effect of the back color formatting is going to confuse you.
As stated above, watch it in action then delete that part of the code
 
hi

thank you for your help
it is working with due _ date but not target _date . even if I change the table name

Code:
Sub Form_Current()

 Me.Due_Date.Locked = Not IsNull(Me.Due_Date)
   ' Me.Target_Date.Locked = Not IsNull(Me.Target_Date)
    End Sub

as I am new to Access I cannot understand why it don't work with target date

thank you
steve
 
You are trying to lock a control Due_Date based on whether the control value is null or not.

If you had named your control the same as the field name,Target_Date, then it would have worked ... but you didn't ... so it doesn't! :D
 
yes , I have found where you mean now

learn everyday

thanks for your help
steve
 
You MUST toggle the lock property. Otherwise, once it is locked, it will STAY LOCKED for all subsequent rows.

So the expression you are given looks "slick" but you really need a more mundane If statement.

Code:
If IsNull(Me.somecontrolname) = True  Then
    Me.somecontrolname.Locked = True
Else
    Me.somecontrolname.Locked = False
End If

When you are referring to a control property such as .Locked, use the name of the CONTROL.

PS - it is poor practice to use embedded spaces or special characters in your object names. Access is meant to be accessible to novices and so it tolerates certain poor practices that more professional tools will not.
 
You MUST toggle the lock property. Otherwise, once it is locked, it will STAY LOCKED for all subsequent rows.

@Pat
On this occasion, you are incorrect - it doesn't happen often ...!
I had already tested the code using both single & continuous forms
The lock/unlock code I gave works perfectly

Please see updated example attached
It contains 3 forms:
- single form & continuous form #1 both use my code
- continuous form #2 uses your code

However, back in post #6, I wrote
Because its a continuous form, the effect of the back color formatting is going to confuse you. As stated above, watch it in action then delete that part of the code

Try it on the continuous form #1 and you'll see why I wrote that

Continuous form #2 uses your If ...Else..End If code
Interestingly it gives exactly the opposite of the results the OP wants
Whilst its fixable, the results are counter-intuitive
 

Attachments

Last edited:
hi

to try and learn I have tried to use both of the codes you have given me in my single form and the continues one

Code:
Sub Form_Current()



    cmdQuit.SetFocus
    
    If IsNull(Me.Target_Date) = True Then
        Me.Target_Date.Locked = True
        Me.Target_Date.BackColor = vbRed
    Else
        Me.Target_Date.Locked = False
        Me.Target_Date.BackColor = vbYellow
    End If

both codes stops at this part of the code 

[CODE]cmdQuit.SetFocus

    
    End Sub


steve
 
However, for a continuous form, you do need the If ...Else..End If version

Colin, why is this?
Why wouldn't a Me.CtrlName.Locked = ISNULL(Me.OtherCtrlName) or NOT ISNULL work just as well.?
 
Colin, why is this?
Why wouldn't a Me.CtrlName.Locked = ISNULL(Me.OtherCtrlName) or NOT ISNULL work just as well.?

@Gasman
Before seeing your post, I realised I'd confused myself testing Pat's code in the original example.
I had applied her code to the single form & tested the continuous form. Doh!

My original code works perfectly for both single & continuous form #1.

I've just edited my previous post & added a new continuous form #2 using Pat's code.
Interestingly Pat's suggestion (which looks correct) actually gives exactly the wrong results!

@rainbows
I'm confused reading your post, partly because I'm no longer clear what your control is called.
Sounds like you don't have a cmdQuit button so the code throws an error.

Please read my previous post again (which has been rewritten) & try my updated example
 
Last edited:

Users who are viewing this thread

Back
Top Bottom