Code works in one form but not in another

J

John

Hi

I am using the below code in one form and it works fine. I copied the same
code into another form in the same app and I get a "Run-time error '2424':
The expression you entered has a field, control, or property name that <my
project> can not find." error. What is the problem and how can I fix it?
Thanks

Regards

Dim ctl As Control

For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then
'Below line gets the '2424' error
If (IsNull(ctl.OldValue) And Not IsNull(ctl.Value)) Or
(IsNull(ctl.Value) And Not IsNull(ctl.OldValue)) Then

End If
End If
Next ctl
 
A

Allen Browne

When the error occurs, choose Debug and open the Immediate Window (Ctrl+G.)
Enter:
? ctl.Name

Now you know which control it is, try:
? ctl.Value
That should work, but this one will probably fail:
? ctl.OldValue

If that's the case, you have a control that has no OldValue. That could be
an unbound control, or a control bound to an expression, or a control bound
to a read-only field (e.g. if the form's RecordSource is a query where some
fields cannot be edited.)

To fix it, modify the code to test for the conditions that could trigger the
error, or use error handling to recover from the error.
 

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