I have a text box in the format of yyyymmdd ie. "20110101" that is derived from an existing access database and want to convert it to mm/dd/yy and place it in a separate text box. How can I accomplish this?
Printable View
I have a text box in the format of yyyymmdd ie. "20110101" that is derived from an existing access database and want to convert it to mm/dd/yy and place it in a separate text box. How can I accomplish this?
Hi... Welcome to the forums :wave:
You have posted this question in the wrong forum. Here, you are supposed to post your code snippets. I'll inform a mod to move this thread to the most appropriate section where you get more replies. :thumb:
BTW, try this code:
:wave:vb Code:
Option Explicit Private Sub Command1_Click() Dim a As String Dim b As Date a = "20110901" '~~~ Sample string b = DateSerial(Mid$(a, 1, 4), Mid$(a, 5, 2), Mid$(a, 7, 2)) '~~~ Converting it into a date format MsgBox Format$(b, "mm/dd/yyyy") '~~~ Display this converted date in whatever format you want End Sub
Thread moved from the 'CodeBank VB6' forum (which is for you to post working code examples, not questions) to the 'VB6 and earlier' forum
Thank you. This has been driving me crazy. This works for the most part but, I want the converted date to show in another text box instead of a message box and the date is not converting correctly. The date in the database is 20110519 and the date in the message box is showing 00/19/2011. Any ideas?
Thank You
I didn't see any problem when tested !
vb Code:
Option Explicit Private Sub Command1_Click() Dim a As String Dim b As Date a = "20110519" '~~~ Sample string b = DateSerial(Mid$(a, 1, 4), Mid$(a, 5, 2), Mid$(a, 7, 2)) '~~~ Converting it into a date format Text1.Text = Format$(b, "mm/dd/yyyy") '~~~ Display this converted date in whatever format you want End Sub
If the issue is still unresolved, then try some of these:
- try trimming the string date read from database: a = trim(a)
- try using MM instead of mm: Text1.Text = Format$(b, "MM/dd/yyyy")
:wave:
ok. that converts the date correctly, but now I have another issue.
Here is the code I have
Private Sub FirmFileComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FirmFileComboBox.SelectedIndexChanged
Dim a As Integer
Dim b As Date
a = SettDateTextBox.Text '~~~ Sample string
b = DateSerial(Mid$(a, 1, 4), Mid$(a, 5, 2), Mid$(a, 7, 2)) '~~~ Converting it into a date format
TextBox23.Text = (Format$(b, "MM/dd/yyyy")) '~~~ Display this converted date in whatever format you
End Sub
The TextBox23 is tied to the FirmFile which is a drop down box that populates my form from a database. So when I choose another file number, I want the text in TextBox23 to be converted to the new date each time I choose a new file. It works, but it is always displaying the date for the previous file number, not the current file number.
Seems like the long way around to convert substrings to a Date and then Format that back to a String. Why not just rearrange the pieces and be done?
So you were using VB.Net ?
I'll request a mod to move this thread to the VB.Net section.vb.net Code:
Public Class Form1 '~~~ Method1: Rearranging and appending (suggested by dilettante) Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim a As String = "20110519" Dim b As String = String.Concat(a.Substring(4, 2), "/", a.Substring(6, 2), "/", a.Substring(0, 4)) MessageBox.Show(b) End Sub '~~~ Method2: Converting to a date and then displaying it in the appropriate format Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim a As String = "20110519" Dim b As Date = New Date(a.Substring(0, 4), a.Substring(4, 2), a.Substring(6, 2)) MessageBox.Show(b.ToString("MM/dd/yyyy")) End Sub End Class
:wave:
Thread moved from the 'VB6 and Earlier' forum to the 'VB.Net' (VB2002 and later) forum (thanks akhileshbc :thumb: )
What data type is the field in the database?
System.Int32
I was able to figure it out, here is what I did:
Thanks for everyones help! :)
Private Sub SettDateTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SettDateTextBox.TextChanged
Dim str As String
str = SettDateTextBox.Text.Trim
For i As Integer = 0 To str.Length - 1 Step 1
TextBox23.Text = (Mid$(str, 5, 2)) & "/" & (Mid$(str, 7, 2)) & "/" & (Mid$(str, 1, 4))
Next
End Sub