Matt Hall
Matt Hall

Reputation: 2412

MS-Access error on BeforeUpdate event when switching to Design Mode

I'm getting the following error when I change something on my form and then switch to design mode:

The expression you entered refers to an object that is closed or doesn't exist

Debug is pointing to the code in my form's BeforeUpdate event; see below:

Private Sub Form_BeforeUpdate(Cancel As Integer)

    Me.JobID = "Job" & Format(Me.ID, String(12 - Len("Job"), "0"))

End Sub

This code simply creates a custom primary key from an autonumber field; it was one of the first things I did and I have been switching between Form and Design View for weeks without issue.

Anyone know what might be causing this?

Upvotes: 0

Views: 394

Answers (1)

HK1
HK1

Reputation: 12210

I can offer a couple suggestions that I think will work around the problem, but I admit I'm not exactly sure of the real cause. By working around it, you may be able to discover the cause yourself.

When referring to underlying fields (as opposed to controls), I suggest you use "bang" instead of "dot". This might be enough to solve this problem.

Private Sub Form_BeforeUpdate(Cancel As Integer)
    Me!JobID = "Job" & Format(Me!ID, String(12 - Len("Job"), "0"))
End Sub

The other thing you might try is eliminating running of that code if it isn't needed:

Private Sub Form_BeforeUpdate(Cancel As Integer)
    If IsNull(Me!JobID) = True Then
        Me!JobID = "Job" & Format(Me!ID, String(12 - Len("Job"), "0"))
    End If
End Sub

Upvotes: 1

Related Questions