Runtime error 2108 (1 Viewer)

Malcy

Registered User.
Local time
Today, 16:36
Joined
Mar 25, 2003
Messages
586
Hi
I have put some validation code in the BeforeUpdate event of a textbox, and the code is
Code:
    If IsNull(Me.txtEndT) Or Me.txtEndT = "" Then
        MsgBox "You must enter an end time for the activity", vbOKOnly, "Data validation"
            Me.txtEndT.SetFocus
        Exit Sub
    End If
        
' If an entry check that it is valid
    If CDate(Me.txtDate & " " & Me.txtStartT) > CDate(Me.txtDate1 & " " & Me.txtEndT) Then
        MsgBox "The end time for the activity cannot be before the start time", vbOKOnly, "Data validation"
            Me.txtEndT.SetFocus
        Exit Sub
    End If
    
    If Me.txtStartT = Me.txtEndT And Me.cboActivity <> 4 Then
        MsgBox "You have set the time for this activity to Zero. Please correct.", vbOKOnly, "Data validation"
            Cancel = True
            Me.txtEndT.Undo
            Me.txtEndT.SetFocus
        Exit Sub
    End If
If I deliberately create an activity (other than = 4) of zero time I get the warning message as anticipated but as soon as I click OK I get a run time error 2108 saying I must save the record before using SetFocus.
I have checked other threads and got the Cancel and Undo to put between the message box and the SetFocus but I am still getting the error.
This seems bonkers to me since surely the Cancel and the Undo set the scene for the SetFocus to kick in.
If anyone can help it would be much appreciated.
Thanks and best wishes
 

RuralGuy

AWF VIP
Local time
Today, 10:36
Joined
Jul 2, 2005
Messages
13,826
Me.txtEndT.Undo sets the TextBox back to whatever is in the field of the record to which the control is bound.
Cancel = True cancels the update and holds the cursor in the same control. No SetFocus is required.
 

Malcy

Registered User.
Local time
Today, 16:36
Joined
Mar 25, 2003
Messages
586
Thanks Rural Guy
That was it. I thought it needed both belt and braces but I was wrong.
Works fine now.
Do you stay at 8000' or go up into hills?
 

RuralGuy

AWF VIP
Local time
Today, 10:36
Joined
Jul 2, 2005
Messages
13,826
Glad you got it working. The town is at 7000 feet and I live 25 miles outside of town, and higher.
 

Users who are viewing this thread

Top Bottom