cspdmg
asked on
Visual Basic 2010: Read / Write Dates to Access Database
I am using a VB2010 form as a front end to an Access database. I am having two problems when reading/writing date fields.
1) When reading a date from the dataset, it doesn't display properly on the form if the date has a single digit for the month and/or day. Example: 9/23/2011 displays as 92/32/011
I'm using the following to read into a masked text box with the mask set to short date:
mtxtRequestDate.Text = ds.Tables("WorkOrders").Ro ws(inc).It em("Reques tDate").To String
2)When saving a date field, if the field is blank I get the following error:
String was not recognized as a valid DateTime.Couldn't store < / /> in RequestDate Column. Expected type is DateTime.
I'm using the following to write to the dataset:
ds.Tables("WorkOrders").Ro ws(inc).It em("Reques tDate") = mtxtRequestDate.Text
Thanks in advance.
1) When reading a date from the dataset, it doesn't display properly on the form if the date has a single digit for the month and/or day. Example: 9/23/2011 displays as 92/32/011
I'm using the following to read into a masked text box with the mask set to short date:
mtxtRequestDate.Text = ds.Tables("WorkOrders").Ro
2)When saving a date field, if the field is blank I get the following error:
String was not recognized as a valid DateTime.Couldn't store < / /> in RequestDate Column. Expected type is DateTime.
I'm using the following to write to the dataset:
ds.Tables("WorkOrders").Ro
Thanks in advance.
ASKER
Thanks for the response however it did not work. I received the error:
String was not recognized as a valid DateTime.
String was not recognized as a valid DateTime.
what is the value of your textbox?
ASKER
The value of the textbox is empty (null?). It shows only the literals (/). Originally there was a date (10/12/2011), I deleted it and tried to resave.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
emoreau:
It treats 'nothing' as null:
Error: Cannot set Column 'RequestDate' to be null
It looks like I will need to check for null before attempting to save and, if it is, create a blank date?
HooKooDooKu:
That worked for reading from the Access table. The only change I needed to make was to the format definition. I changed 'mm' (minutes) to MM (month). Thanks!
It treats 'nothing' as null:
Error: Cannot set Column 'RequestDate' to be null
It looks like I will need to check for null before attempting to save and, if it is, create a blank date?
HooKooDooKu:
That worked for reading from the Access table. The only change I needed to make was to the format definition. I changed 'mm' (minutes) to MM (month). Thanks!
is your table allows null for this field?
try this:
dim dtmDate as datetime
if DateTime.TryParse(mtxtRequ estDate.Te xt, dtmDate) then
ds.Tables("WorkOrders").Ro ws(inc).It em("Reques tDate") = convert.todatetime( mtxtRequestDate.Text)
else
ds.Tables("WorkOrders").Ro ws(inc).It em("Reques tDate") = datetime.today
endif
try this:
dim dtmDate as datetime
if DateTime.TryParse(mtxtRequ
ds.Tables("WorkOrders").Ro
else
ds.Tables("WorkOrders").Ro
endif
ASKER
That works except that it saves today's date of course.
is your table allows null for this field?
what do you need to do when the textbox does not contain a valid date?
what do you need to do when the textbox does not contain a valid date?
ASKER
No, it doesn't except null. How to handle it through VB is where the problem is.
again, what do you need to do when the textbox does not contain a valid date?
ASKER
If the textbox is empty (i.e blank), I need to accept it as valid. If it is not a valid date, then reject it with a message.
But your date cannot be null. Which value will you set if it is empty?
ASKER
I have it working. Similar to what you suggested but I need to take the mask literals into account.
If DateTime.TryParse(mtxtRequ estDate.Te xt, dtmDate) Then
ds.Tables("WorkOrders").Ro ws(inc).It em("Reques tDate") =
Convert.ToDateTime(mtxtReq uestDate.T ext)
ElseIf Trim(mtxtRequestDate.Text) = "/ /" Then
ds.Tables("WorkOrders").Ro ws(inc).It em("Reques tDate") = Nothing
Else
MsgBox("Invalid Request Date. Please reenter")
mtxtRequestDate.Select()
mtxtRequestDate.Clear()
Exit Sub
End If
Thank you for your assistance.
If DateTime.TryParse(mtxtRequ
ds.Tables("WorkOrders").Ro
Convert.ToDateTime(mtxtReq
ElseIf Trim(mtxtRequestDate.Text)
ds.Tables("WorkOrders").Ro
Else
MsgBox("Invalid Request Date. Please reenter")
mtxtRequestDate.Select()
mtxtRequestDate.Clear()
Exit Sub
End If
Thank you for your assistance.
have you tried: ds.Tables("WorkOrders").Ro