Thanks Sprinks. Your code and description is very clear and easy to follow.
Everything works well except this one thing with your general procedure that
I adopt -- the Version AfterUpdate event triggers a run-time error 94 Invalid
use of Null, with the line [VersionStatus] = [Version] + "." +
Left$([Status], 1)
I was thinking is it because Status is still blank (as it should) after
filling Version textbox so there's no Status value to update? Can you see any
way around this? Thanks
--- start of my code ---
Private Sub Version_AfterUpdate()
Call UpdateVersionStatus
End Sub
Private Sub Status_AfterUpdate()
If Left$([Status], 1) = "J" Then
[JobNo].Enabled = True
[JobNo].Visible = True
End If
Call UpdateVersionStatus
End Sub
Private Sub JobNo_AfterUpdate()
Call UpdateVersionStatus
End Sub
Private Sub UpdateVersionStatus()
If ([Status] = "J-Job with #") Then
[VersionStatus] = [Version] + "." + Left$([Status], 1) + [JobNo]
Else
' this line triggers a run-time error
[VersionStatus] = [Version] + "." + Left$([Status], 1)
End If
End Sub
--- end of my code ---
Sprinks said:
Hi, Sam.
Firstly, IIf is a function that provides a simple way to assign a value
without VBA event code. If you're going to use a VBA procedure anyway, your
code will be more readable and flexible by using an If..Then..Else...End If
construct. You are also missing a call to the Left$ function in the Status
AfterUpdate event. Rewriting:
If ([Status] = "J-Job with #") Then
[VersionStatus] = [Version] + "." + Left$([Status],1) + [JobNo]
Else
[VersionStatus] = [Version] + "." + Left$([Status],1)
End If
The parentheses are just a reminder that what's enclosed evaluates to a
single boolean value.
Re: toggling on [JobNo] when [Status] = "J": set the JobNo textbox'
Enabled property (and, optionally, its Visible property) to No in Form Design
View. Then toggle them on in the Status AfterUpdate event.
If Left$([Status],1) = "J" Then
[JobNo].Enabled = True
[JobNo].Visible = True
End If
If this form is being used in Data Entry mode only, this is all you'll need.
If the user can scroll between records, however, you'd want [JobNo] to
reflect what's ever in the [Status] field. In that case, add the following
to the form's On Current event:
If Left$([Status],1) = "J" Then
[JobNo].Enabled = True
[JobNo].Visible = True
Else
[JobNo].Enabled = False
[JobNo].Visible = False
End If
One final note: since you'd really like [VersionStatus] to update its value
regardless of which of the other 3 fields change, this suggests writing a
general procedure,
Private Sub UpdateVersionStatus()
' Put code here
End Sub
and calling it in the AfterUpdate event of each control:
Call UpdateVersionStatus
Hope that solves it for you.
Sprinks