-
date input
I have th efollwowing code to enter a record.
when I leave the date field blank and press udpade
I get an error meassage.
is there a way of making my program accept blank entries for the date field? I get the same problem with currency
Private Sub Command1_Click()
Data1.Recordset.AddNew
Data1.Recordset("Name") = Text1
Data1.Recordset("Start_Date") = Text2.Text
Data1.Recordset("End_Date") = Text3.Text
Data1.Recordset("Currency") = Text4.Text
Data1.Recordset.Update
End Sub
-
A "blank entry" in a text box is considered an empty string. You cannot set a numeric or date to an empty string. You will need to add some validation checks when setting your recordset values.
If your database does not allow nulls then you must supply a value.
If Len(Trim$(Text2.Text)) > 0 Then
Data1.Recordset("Start_Date") = Text2.Text
Else
Data1.Recordset("Start_Date") = Null
End If
Data1.Recordset("Currency") = Val(Text4.Text) 'this will equate to 0 if the textbox is empty.
-
Fantastic, Thanks a lot!
just one more question:
I have this error handling statement:
on error goto fix:
fix: data1.Recordset.CancelUpdate
on the same statement i.e. fix I want to add this:
MsgBox "If a field is not filled in please enter N/A. This Record will not be saved", vbExclamation
I want this to come up along with CancelUpdate, but when I put it on the same line as data1.Recordset.CancelUpdate, I get a message saying "Expected end of statement"
Is there a way of doing what I want to do?
-
Type in on a separate line
Code:
fix:
data1.Recordset.CancelUpdate
MsgBox "If a field is not filled in please enter N/A. This Record will not be saved", vbExclamation
-
it doen't work, the msgbox still comes up if there is no blank entry!