Setting date to something else

PedroA

Active member
Joined
Oct 14, 2013
Messages
26
Programming Experience
Beginner
Hi,

In VB6, I can set a date variable to 0. In VB.Net, this cannot be done. The field storing the date is 8 characters long.

How can I do something equivalent in VB.Net?

Thanks for any help
 
In .Net a non-value can be expressed as Nullable(Of T) where T is a value type, as here the Date data type.

If by "field" you mean a database field, then it can be set to allow nulls, which means when the value is not set it is DBNull.

Storing a Date value as "characters" is usually wrong, though.
 
Sorry I was unclear.

The field type to store the date is text and the field size is 8. The date value is stored in a field in a MS Access Database. Unfortunately, the field is set to allow zero length and this can not be changed (same for the field type and size). I was wondering whether there is something I can assign to the date variable to overcome this.

Thanks
 
If the data type is not date then it's not a date. It's text. If you want to store a text value that represents no date then pick whatever you want.

When it comes to representing the data in your VB code, JohnH has already told you what to do. Use a Nullable(Of Date), for which the shorthand is Date?, and then you can assign the appropriate Date to it if the data represents a valid date and Nothing otherwise.
 
Thanks for your responses.

What can I do if there are MS Access fields that I write to that store dates, but one type is a text type and the other is set as date/ time and this cannot be changed? The text type field cannot be zero length and its field size is 8, which cannot be changed. In vb6, I could set the date to 0.

Any suggestions are greatly appreciated
 
A Date is a Date. It can only have a date value. If you want to represent something that can be a date or have no value in VB.NET then use a Date? If you want to save 0 to the database when there's no value then do so. If the field is text then you can save whatever text you want. That said, if you want to represent no value then you should be using NULL.
 
Back
Top