Yeah, it needs to be edited, and that's just what happens!
After a bit of testing, the workround is a little bit longer than I
anticipated. You might be better off just using a date picker control
(look up
http://ccrp.mvps.org/download/ccrpdownloads.htm for example)
If you want to stick to two textboxes, then you'll need to use unbound
text boxes, and take over all the usual Access functions yourself. Try
this for a partially-tested example. TimeReceived is the name of the
database field that contains the datetime value; txtDateBox and
txtTimeBox are the textboxes, which have the format property set to
"dd/mm/yyyy" and "hh:nn:ss" respectively.
Option Compare Database
Option Explicit
Private Sub Form_Current()
' this gets called as soon as the form lands on a new
' record. If there's no value then empty the boxes,
' otherwise put the new values in
If IsNull(Me.TimeReceived) Then
txtDateBox.Value = Null
txtTimeBox.Value = Null
Else
' update the UNBOUND textboxes from the database value
txtDateBox.Value = DateValue(Me.TimeReceived)
txtTimeBox.Value = TimeValue(Me.TimeReceived)
End If
End Sub
Private Sub txtDateBox_BeforeUpdate(Cancel As Integer)
' do some validation checking before moving on
' if you want to allow nulls then put that here
'
' This has to be cascading If..ElseIfs because Or and And
' evaluate both their arguments and there will be Invalid
' Use of Null errors otherwise
' If IsNull(txtDateBox) Then
' Cancel = False
' ElseIf Not IsDate(... etc
If Not IsDate(txtDateBox) Then
Cancel = True
ElseIf Not TimeValue(txtDateBox) = 0 Then
Cancel = True
Else
' redundant: could put range checking in here
Cancel = False
End If
End Sub
Private Sub txtDateBox_AfterUpdate()
' echo the new value to the database
UpdateDateValue
End Sub
Private Sub txtTimeBox_BeforeUpdate(Cancel As Integer)
' see comments on txtDateBox_BeforeUpdate
If Not IsDate(txtTimeBox) Then
Cancel = True
ElseIf Not DateValue(txtTimeBox) = 0 Then
Cancel = True
Else
' redundant: could put range checking in here
Cancel = False
End If
End Sub
Private Sub txtTimeBox_AfterUpdate()
' echo the new value to the database
UpdateDateValue
End Sub
Private Sub UpdateDateValue()
' this creates a single datetime value out of the two boxes
' and relays that value back to the underlying recordset
If IsDate(txtDateBox.Value) And _
IsDate(txtTimeBox.Value) Then
Me!TimeReceived = _
DateValue(CDate(txtDateBox.Value)) + _
TimeValue(CDate(txtTimeBox.Value))
Else
Me.TimeReceived = Null
End If
Call Form_Current
End Sub