|
You should never store dates as strings, use Date or DateTime values, and only apply formatting when it is displayed. For example the date 01/02/03 could be 1st February 2003, 2nd January 2003, or even 3rd February 2001, depending on where you live.
|
|
|
|
|
If dtPick5.Value.ToString("MM/dd/yyyy" isn't the correct way then can you post an example of how to properly do it?
|
|
|
|
|
At the risk of repeating myself again and again: use the Value. That way the date can be formatted for display into any culture.
|
|
|
|
|
Member 11866893 wrote: Heaven forbid you repeat yourself or provide a code example.... The explanation is spot on, but to expand with code;
dim someDateString as String = dtPick5.Value.ToString(.. This creates a text, with the date formatted in some culture.
dim someDate as DateTime = dtPick5.Value Here the value is taken as is, without formatting. It is stored as a decimal value, with "days" in the integer part from a given offset, and the decimal part representing time. It is a format-independant version, and that's how it should be stored. Internally, your application should use DateTime values, and only formatting them as strings when displaying.
Saving the value in DateTime type is important; if you use a localized and formatted version tht is actually a string, the computer will have to convert that to a real DateTime for each operation.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
You decently cannot insult those who try to help.
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
Ah yes, the lazy person's fallback position when you don't get someone else to do your work: abuse.
|
|
|
|
|
It's the VB forum; it looks like a frustrated coder. Don't take it as a personal insult.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Eddy Vluggen wrote: Don't take it as a personal insult. I don't; my shoulders are broad enough. But I do take exception to people being abusive on this site.
|
|
|
|
|
Richard MacCutchan wrote: But I do take exception to people being abusive on this site. Good for me I never did that
It is one of umpteen coders frustrated at how dates don't work like they imagine. Not something unique nor new.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
The trouble with some of these people is that they will not accept advice. So no matter how much you try to help them they insist on doing it their way, without understanding the potential problems that can come back to bite them.
|
|
|
|
|
Richard MacCutchan wrote: But I do take exception to people being abusive on this site. Why is that, Thou sodden-witted lord?
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
Weird since examples seem to work with a single comma.
You could try to escape extra characters (i.e., commas):
dtPick5.CustomFormat = "MM',' dd',' yyyy" or keep using a more common form.
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
I think the issue you may be facing is that you are storing the data as datetime which naturally includes the time component. If you do not need the time component then you should store it as a date data type (not sure if Access has such a data type).
If the is a requirement for the time component then you need to format every instance of the data as it is presented and you need to cater for the time component in all your queries ie you can no longer query for a date you will need to query for the range of datetime records between midnight and midnight the date after.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
I have a textbox that I need to run this query when the value changes for the textbox.
Private Sub tbStudentID2_TextChanged(sender As Object, e As EventArgs) Handles tbStudentID2.TextChanged
mySql = "SELECT DateDiff('d', [DateofRank], [currentDate]) FROM StudentData"
ConnDB()
myCommand = New OleDbCommand(mySql, myConnection)
myCommand.ExecuteNonQuery()
myRead = myCommand.ExecuteReader
Do While myRead.Read = True
tbDays.Text = myRead(0).ToString()
Loop
End Sub
86 is display no matter which record I choose. I think it is just retrieving the first record in the in the table.
I want to display the number of days since a student has tested by subtracting DateofRank from currentDate. I am open to other ideas on how to achieve this.
|
|
|
|
|
It's selecting all records from the table. You then iterate through the results, overwriting the textbox with the next value, until you get to the last record in the table.
You need a WHERE clause to select just the record you're interested in. And you need to make sure you use parameters to avoid a SQL Injection[^] vulnerability.
There's also no need to use ExecuteReader to read a single value from a single row; use ExecuteScalar instead.
Try something like this:
Private Sub tbStudentID2_TextChanged(sender As Object, e As EventArgs) Handles tbStudentID2.TextChanged
Dim studentID As Integer
If Not Integer.TryParse(tbStudentID2.Text, studentID) Then
txtDays.Text = String.Empty
Else
Using command As New OleDbCommand("SELECT DateDiff('d', [DateofRank], [currentDate]) FROM StudentData WHERE StudentID = @StudentID", myConnection)
command.Parameters.AddWithValue("@StudentID", studentID)
ConnDB()
Dim days As Object = command.ExecuteScalar()
txtDays.Text = If(Convert.IsDBNull(days), String.Empty, Convert.ToString(days))
End Using
End If
End Sub Int32.TryParse Method (System) | Microsoft Docs[^]
OleDbCommand.ExecuteScalar Method (System.Data.OleDb) | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
That did the trick. Thank you very much.
|
|
|
|
|
This is how I fixed with help from a post on an another site. I do have one question though. If I comment out the first db connection - ConnDB() I get an error. Why does it need two connection to work? It makes a connection in the using command. I had setup a data table from a class I wrote to make a connection and set it up so I declare Private Access As New DBControl at the top and I can add parameters and do my additions and edits that way. I didn't know how to make that work with the existing connection to the Access DBControl so I just did another connection. Not the best but I am a beginner and don't know how to do things with more efficiency yet.
Private Sub tbStudentID2_TextChanged(sender As Object, e As EventArgs) Handles tbStudentID2.TextChanged
mySql = "SELECT DateDiff('d', [DateofRank], [currentDate]) FROM StudentData"
ConnDB()
Dim studentID As Integer
If Not Integer.TryParse(tbStudentID2.Text, studentID) Then
tbDays.Text = String.Empty
Else
Using command As New OleDbCommand("SELECT DateDiff('d', [DateofRank], [currentDate]) FROM StudentData WHERE StudentID = @StudentID", myConnection)
command.Parameters.AddWithValue("@StudentID", studentID)
ConnDB()
Dim days As Object = command.ExecuteScalar()
tbDays.Text = If(Convert.IsDBNull(days), String.Empty, Convert.ToString(days))
End Using
Access.AddParam("@TIG", tbDays.Text)
Access.AddParam("@SID", tbStudentID2.Text)
Access.ExecQuery("UPDATE StudentData " &
"SET TimeinGrade=@TIG " &
"WHERE StudentID=@SID")
If NoErrors(True) = False Then Exit Sub
End If
End Sub
|
|
|
|
|
I want to fill all the entries in a column with the current date so that I can compare it to a DateofTest column and determine how many days since the last test. I think I can figure out the number of days but I can't figure out how to fill the column with the current date.
Private Sub Reports_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
mySql = "Update Studentdata, SET currentDate = Date()"
ConnDB()
myCommand = New OleDbCommand(mySql, myConnection)
End Sub
|
|
|
|
|
Remove the comma.
UPDATE StudentData SET currentDate = DATE()"
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
No luck. I don't get an error it just doesn't do anything to the column.
|
|
|
|
|
As Richard pointed out, if you have shown all of your code you are never actually executing it.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
You forgot to execute the SQL command.
|
|
|
|
|
doesn't this execute the command? myCommand = New OleDbCommand(mySql, myConnection)
|
|
|
|
|
No, it just creates a OldDbCommand object. You actually have to tell it to execute using one of the Execute... methods.
|
|
|
|
|
That did the trick. I added mycommand.ExecuteNonQuery() and it worked perfect.
|
|
|
|