cleaner code

teiben

Registered User.
Local time
Today, 15:19
Joined
Jun 20, 2002
Messages
462
I'm trying to validate two fields, hours and descriptionofTask. They can't be blank, there seem like it could be done better. Also the message when the user tries to close out is YOu can't save this record at this time, can you change this? if so how?

Private Sub Form_BeforeUpdate(Cancel As Integer)
Const strTitle As String = "Time Tracking - Program Development"

If IsNull(Me.DescriptionofTask) Then
MsgBox "You must select an activity from the list", vbOKOnly, strTitle
Cancel = True
DescriptionofTask.SetFocus
DescriptionofTask.ForeColor = vbRed

If IsNull(Me.Hours) Then
Hours.SetFocus
Hours.ForeColor = vbRed

End If
End If
End Sub
 
Can you just do this from the table level?

kh
 
This should work for you...
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

    If IsNull(DescriptionofTask) Or DescriptionofTask = "" Then
        MsgBox "You must select an activity from the list", vbOKOnly, "Time Tracking - Program Development"
        DescriptionofTask.SetFocus
        DescriptionofTask.ForeColor = vbRed
        DoCmd.CancelEvent
    End If
    
    If IsNull(Hours) Or Hours = "" Then
        MsgBox "You must select a hour.", vbOKOnly, "Time Tracking - Program Development"
        Hours.SetFocus
        Hours.ForeColor = vbRed
        DoCmd.CancelEvent
    End If

End Sub
You have to put some code in the OnClose event to trap if the record is Dirty for then you need to alert the user and ask if they want to undo their changes and close the form of do not close the form so that they can finish their changes and save the record. You should add a Save Record button for that.
 
KenHigg said:
Can you just do this from the table level?

Yes. All teiben has to do is set the Required property to Yes.
 

Users who are viewing this thread

Back
Top Bottom