date/time field in access shows nonexistant time in databound textbox.

K

Kyote

I have a textbox that is databound to a table in an access database.
The field only contains month/day/year but my databound textbox is
also showing a time. Is there any way to prevent it from doing this?
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Kyote said:
I have a textbox that is databound to a table in an access database.
The field only contains month/day/year

No, a Date/Time field actually always contains a time component, but the
time may be set to 00:00:00.

The value is read into the .NET type DateTime, which also always
contains a time component.
but my databound textbox is
also showing a time. Is there any way to prevent it from doing this?

Specify the FormatString in the Binding object.
 
G

Guest

Cor,

Here is your example changed to use FormatString and NullValue properties:

Code:
Public Class Form1

Private ds As New DataSet

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim dt As New DataTable
ds.Tables.Add(dt)
dt.Columns.Add("DateField", GetType(System.DateTime))
dt.LoadDataRow(New Object() {New DateTime(2005, 8, 4)}, True)
dt.LoadDataRow(New Object() {Nothing}, True)
dt.LoadDataRow(New Object() {New DateTime(2005, 8, 5)}, True)

Dim Mybinding As New Binding("Text", ds.Tables(0), "DateField", True)
Mybinding.FormatString = "MM/dd/yyyy"
Mybinding.NullValue = "No date"
TextBox1.DataBindings.Add(Mybinding)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

'forward
Me.BindingContext(ds.Tables(0)).Position += 1

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

'backward
Me.BindingContext(ds.Tables(0)).Position -= 1

End Sub
End Class

Kerry Moorman
 
G

Guest

Cor,

Here is your example changed to use FormatString and NullValue properties:

Code:
Public Class Form1

Private ds As New DataSet

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim dt As New DataTable
ds.Tables.Add(dt)
dt.Columns.Add("DateField", GetType(System.DateTime))
dt.LoadDataRow(New Object() {New DateTime(2005, 8, 4)}, True)
dt.LoadDataRow(New Object() {Nothing}, True)
dt.LoadDataRow(New Object() {New DateTime(2005, 8, 5)}, True)

Dim Mybinding As New Binding("Text", ds.Tables(0), "DateField", True)
Mybinding.FormatString = "MM/dd/yyyy"
Mybinding.NullValue = "No date"
TextBox1.DataBindings.Add(Mybinding)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

'forward
Me.BindingContext(ds.Tables(0)).Position += 1

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

'backward
Me.BindingContext(ds.Tables(0)).Position -= 1

End Sub
End Class

Kerry Moorman
 
C

Cor Ligthert[MVP]

Kerry,

Thanks

I will see if I can implement it, a problem with checking for nulls is often
that it makes the sample more difficult to read. It is about to tell how it
can be done.

"Learning to fish not giving the fish".

I started this were I needed it for dates before 1753 and than raised those
by 3000

Maybe I make 2 samples from it.

Cor




Kerry Moorman said:
Cor,

Here is your example changed to use FormatString and NullValue properties:

Code:
Public Class Form1

Private ds As New DataSet

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim dt As New DataTable
ds.Tables.Add(dt)
dt.Columns.Add("DateField", GetType(System.DateTime))
dt.LoadDataRow(New Object() {New DateTime(2005, 8, 4)}, True)
dt.LoadDataRow(New Object() {Nothing}, True)
dt.LoadDataRow(New Object() {New DateTime(2005, 8, 5)}, True)

Dim Mybinding As New Binding("Text", ds.Tables(0), "DateField",
True)
Mybinding.FormatString = "MM/dd/yyyy"
Mybinding.NullValue = "No date"
TextBox1.DataBindings.Add(Mybinding)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

'forward
Me.BindingContext(ds.Tables(0)).Position += 1

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

'backward
Me.BindingContext(ds.Tables(0)).Position -= 1

End Sub
End Class

Kerry Moorman


Cor Ligthert said:
You can at least use format events

http://www.vb-tips.com/DataBindingEvents.aspx

Cor
 
K

Kyote

No, a Date/Time field actually always contains a time component, but the
time may be set to 00:00:00.

The value is read into the .NET type DateTime, which also always
contains a time component.


Specify the FormatString in the Binding object.

Thank you also for your response. I think your pretty much telling me
to do what Ken's example is showing me. With my limited understanding
of databinding and similar concepts this is going to take me quite a
while to understand, I have can understand it in a basic way even now.
So I have strong hopes of figuring out what all is happening to such a
degree that I can then make use of it for my app and any future app as
well.

Again, thank you for the help.
 
C

Cor Ligthert[MVP]

Kerry,

The new Tip is here

http://www.vb-tips.com/DataBindingFormatFields

Cor

Kerry Moorman said:
Cor,

Here is your example changed to use FormatString and NullValue properties:

Code:
Public Class Form1

Private ds As New DataSet

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim dt As New DataTable
ds.Tables.Add(dt)
dt.Columns.Add("DateField", GetType(System.DateTime))
dt.LoadDataRow(New Object() {New DateTime(2005, 8, 4)}, True)
dt.LoadDataRow(New Object() {Nothing}, True)
dt.LoadDataRow(New Object() {New DateTime(2005, 8, 5)}, True)

Dim Mybinding As New Binding("Text", ds.Tables(0), "DateField",
True)
Mybinding.FormatString = "MM/dd/yyyy"
Mybinding.NullValue = "No date"
TextBox1.DataBindings.Add(Mybinding)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

'forward
Me.BindingContext(ds.Tables(0)).Position += 1

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

'backward
Me.BindingContext(ds.Tables(0)).Position -= 1

End Sub
End Class

Kerry Moorman


Cor Ligthert said:
You can at least use format events

http://www.vb-tips.com/DataBindingEvents.aspx

Cor
 
C

Cor Ligthert[MVP]

It does not show up in the link, we will have a look at the problem.

By hand it is there.

Cor

Cor Ligthert said:
Kerry,

The new Tip is here

http://www.vb-tips.com/DataBindingFormatFields

Cor

Kerry Moorman said:
Cor,

Here is your example changed to use FormatString and NullValue
properties:

Code:
Public Class Form1

Private ds As New DataSet

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim dt As New DataTable
ds.Tables.Add(dt)
dt.Columns.Add("DateField", GetType(System.DateTime))
dt.LoadDataRow(New Object() {New DateTime(2005, 8, 4)}, True)
dt.LoadDataRow(New Object() {Nothing}, True)
dt.LoadDataRow(New Object() {New DateTime(2005, 8, 5)}, True)

Dim Mybinding As New Binding("Text", ds.Tables(0), "DateField",
True)
Mybinding.FormatString = "MM/dd/yyyy"
Mybinding.NullValue = "No date"
TextBox1.DataBindings.Add(Mybinding)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

'forward
Me.BindingContext(ds.Tables(0)).Position += 1

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

'backward
Me.BindingContext(ds.Tables(0)).Position -= 1

End Sub
End Class

Kerry Moorman


Cor Ligthert said:
You can at least use format events

http://www.vb-tips.com/DataBindingEvents.aspx

Cor

"Kyote" <[email protected]> schreef in bericht
I have a textbox that is databound to a table in an access database.
The field only contains month/day/year but my databound textbox is
also showing a time. Is there any way to prevent it from doing this?
 
C

Cor Ligthert[MVP]

This should do it now.

http://www.vb-tips.com/DataBindingFormatFields.aspx

Cor

Cor Ligthert said:
It does not show up in the link, we will have a look at the problem.

By hand it is there.

Cor

Cor Ligthert said:
Kerry,

The new Tip is here

http://www.vb-tips.com/DataBindingFormatFields

Cor

Kerry Moorman said:
Cor,

Here is your example changed to use FormatString and NullValue
properties:

Code:
Public Class Form1

Private ds As New DataSet

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim dt As New DataTable
ds.Tables.Add(dt)
dt.Columns.Add("DateField", GetType(System.DateTime))
dt.LoadDataRow(New Object() {New DateTime(2005, 8, 4)}, True)
dt.LoadDataRow(New Object() {Nothing}, True)
dt.LoadDataRow(New Object() {New DateTime(2005, 8, 5)}, True)

Dim Mybinding As New Binding("Text", ds.Tables(0), "DateField",
True)
Mybinding.FormatString = "MM/dd/yyyy"
Mybinding.NullValue = "No date"
TextBox1.DataBindings.Add(Mybinding)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

'forward
Me.BindingContext(ds.Tables(0)).Position += 1

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

'backward
Me.BindingContext(ds.Tables(0)).Position -= 1

End Sub
End Class

Kerry Moorman


:

You can at least use format events

http://www.vb-tips.com/DataBindingEvents.aspx

Cor

"Kyote" <[email protected]> schreef in bericht
I have a textbox that is databound to a table in an access database.
The field only contains month/day/year but my databound textbox is
also showing a time. Is there any way to prevent it from doing this?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top