Reputation: 11
I have a MonthCalendar Control on my form. When you select the date it fills a textbox with the selected date. It appears in the textbox as the format that I want (Short Date).
When I add the new record to the database from my text box it loads in my DataGridView control as (Short Date and Time). I open the Access file and the record shows the correct format of Short Date. The data type in Access is selected as Date/Time with the format of Short Date.
Where is the time coming from and how do I get rid of it?
Public Class Form1
Dim i As Integer
Dim con As New OleDb.OleDbConnection 'THE CONNECTION OBJECT
Dim dbProvider As String 'HOLDS THE PROVIDER
Dim dbSource As String 'HOLDS THE DATA SOURCE
Dim MyFolder As String 'HOLDS THE DATABASE FOLDER
Dim TheDatabase As String 'HOLDS THE DATABASE NAME
Dim FullDatabasePath As String 'HOLDS THE DATABASE PATH
Dim ds As New DataSet 'HOLDS THE DataSet OBJECT
Dim da As OleDb.OleDbDataAdapter 'HOLDS THE DataAdapter OBJECT
Dim sql As String 'HOLDS THE SQL STRING
Dim a As Control
Dim str_pipedate As String
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.PV_TableTableAdapter.Fill(Me.PVdbDataSet.PV_Table)
'SET UP THE PROVIDER
dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
'SET THE DATABASE AND WHERE THE DATABASE IS
TheDatabase = "/PVdb.accdb"
MyFolder = "C:\Pipe Vault"
FullDatabasePath = MyFolder & TheDatabase
'SET THE DATA SOURCE
dbSource = "Data Source = " & FullDatabasePath
'SET THE CONNECTION STRING
con.ConnectionString = dbProvider & dbSource
'OPEN THE DATABASE
con.Open()
'STORE THE SQL STRING AND CONNECTION OBJECT TO THE DATA_ADAPTER
sql = "SELECT * FROM PV_table"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "PV_Table")
'Close THE DATABASE '
con.Close()
'MessageBox.Show("Database is now closed")
End Sub
Private Sub DataGridView1_RowHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.RowHeaderMouseClick
'LOADS TEXTBOXS WITH DATA FROM DATAGRID OBJECT
i = DataGridView1.CurrentRow.Index
txtLocation.Text = DataGridView1.Item(0, i).Value
txtProjectName.Text = DataGridView1.Item(1, i).Value
txtPipeDate.Text = DataGridView1.Item(2, i).Value
txtPipeNumber.Text = DataGridView1.Item(3, i).Value
txtPipeSize.Text = DataGridView1.Item(4, i).Value
txtAproxFootage.Text = DataGridView1.Item(5, i).Value
txtInstallDate.Text = DataGridView1.Item(6, i).Value
End Sub
Private Sub TxtPipeDate_Click(sender As Object, e As EventArgs) Handles txtPipeDate.Click
'MonthCalendar1.Visible = True
End Sub
Private Sub MonthCalendar1_DateSelected(sender As Object, e As DateRangeEventArgs) Handles MonthCalendar1.DateSelected
txtPipeDate.Text = MonthCalendar1.SelectionStart
MonthCalendar1.Visible = False
End Sub
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("PV_table").Rows(i).Item(1) = txtLocation.Text
ds.Tables("PV_table").Rows(i).Item(2) = txtProjectName.Text
ds.Tables("PV_table").Rows(i).Item(3) = txtPipeDate.Text
ds.Tables("PV_table").Rows(i).Item(4) = txtPipeNumber.Text
ds.Tables("PV_table").Rows(i).Item(5) = txtPipeSize.Text
ds.Tables("PV_table").Rows(i).Item(6) = txtAproxFootage.Text
ds.Tables("PV_table").Rows(i).Item(7) = txtInstallDate.Text
da.Update(ds, "PV_table")
'MessageBox.Show("Data updated")
End Sub
Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles btnAddNew.Click
'ENABLES TEXT BOXES
For Each a In Me.Controls
If TypeOf a Is TextBox Then
a.Enabled = True
End If
Next
'CLEARS CONTENTS OF TEXT BOXES
txtLocation.Clear()
txtProjectName.Clear()
txtPipeDate.Clear()
txtPipeNumber.Clear()
txtPipeSize.Clear()
txtAproxFootage.Clear()
txtInstallDate.Clear()
'SETS STATES OF BUTTONS
btnCommit.Enabled = True
btnAddNew.Enabled = False
btnUpdate.Enabled = False
btnDelete.Enabled = False
End Sub
'Private Sub Clear_Click(sender As Object, e As EventArgs) Handles Clear.Click
' btnCommit.Enabled = False
' btnAddNew.Enabled = True
' btnUpdate.Enabled = True
'btnDelete.Enabled = True
'End Sub
Private Sub btnCommit_Click(sender As Object, e As EventArgs) Handles btnCommit.Click
Dim newRow As DataRow = ds.Tables("PV_Table").NewRow()
Dim cb As New OleDb.OleDbCommandBuilder(da)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
newRow("Location") = txtLocation.Text
newRow("Project Name") = txtProjectName.Text
newRow("Pipe Date") = txtPipeDate.Text
newRow("Pipe Number") = txtPipeNumber.text
newRow("Pipe Size") = txtPipeSize.Text
newRow("Aprox Footage") = txtAproxFootage.Text
newRow("Install Date") = txtInstallDate.Text
ds.Tables("PV_Table").Rows.Add(newRow)
da.Update(ds, "PV_Table")
Me.PV_TableTableAdapter.Fill(Me.PVdbDataSet.PV_Table)
'SET UP THE PROVIDER
dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
'SET THE DATABASE AND WHERE THE DATABASE IS
TheDatabase = "/PVdb.accdb"
MyFolder = "C:\Pipe Vault"
FullDatabasePath = MyFolder & TheDatabase
'SET THE DATA SOURCE
dbSource = "Data Source = " & FullDatabasePath
'SET THE CONNECTION STRING
con.ConnectionString = dbProvider & dbSource
'OPEN THE DATABASE
con.Open()
'STORE THE SQL STRING AND CONNECTION OBJECT TO THE DATA_ADAPTER
sql = "SELECT * FROM PV_table"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "PV_Table")
'Close THE DATABASE '
con.Close()
'ENABLES TEXT BOXES
For Each a In Me.Controls
If TypeOf a Is TextBox Then
a.Enabled = False
End If
Next
'CLEARS CONTENTS OF TEXT BOXES
txtLocation.Clear()
txtProjectName.Clear()
txtPipeDate.Clear()
txtPipeNumber.Clear()
txtPipeSize.Clear()
txtAproxFootage.Clear()
txtInstallDate.Clear()
'SETS STATES OF BUTTONS
btnCommit.Enabled = False
btnAddNew.Enabled = True
btnUpdate.Enabled = False
btnDelete.Enabled = False
End Sub
Private Sub txtPipeDate_GotFocus(sender As Object, e As EventArgs) Handles txtPipeDate.GotFocus
MonthCalendar1.Visible = True
End Sub
End Class
Upvotes: 1
Views: 6204
Reputation: 123419
Where is the time coming from [in the DataGridView]
Date/Time values in Access always have both a Date and Time component. The default formatting behaviour in Access itself is to suppress the time component if it is midnight, so a Date/Time value of 2015-09-28 00:00:00
will by default display as 2015-09-28
in Access.
and how do I get rid of it?
.NET controls do not share that default formatting behaviour for Date/Time values, so you just need to set the formatting for the particular column(s) of the DataGridView to a date-only format like d
for "short date". You could do that in the design view of the form ...
... or with code something like this
DataGridView1.Columns(3).DefaultCellStyle.Format = "d"; ' short date
For other formatting codes, see
Standard Date and Time Format Strings
Upvotes: 2