|
-
Oct 20th, 2011, 08:08 PM
#1
Thread Starter
New Member
[RESOLVED] Converting Text Box to Date
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?
-
Oct 21st, 2011, 12:49 AM
#2
Re: Converting Text Box to Date
Hi... Welcome to the forums 
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. 
BTW, try this code:
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
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Oct 21st, 2011, 05:37 AM
#3
Re: Converting Text Box to Date
Thread moved from the 'CodeBank VB6' forum (which is for you to post working code examples, not questions) to the 'VB6 and earlier' forum
-
Oct 21st, 2011, 07:35 PM
#4
Thread Starter
New Member
Re: Converting Text Box to Date
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
-
Oct 22nd, 2011, 12:48 AM
#5
Re: Converting Text Box to Date
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")
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Oct 22nd, 2011, 05:07 PM
#6
Thread Starter
New Member
Re: Converting Text Box to Date
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.
-
Oct 22nd, 2011, 07:35 PM
#7
Re: Converting Text Box to Date
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?
-
Oct 22nd, 2011, 11:17 PM
#8
Re: Converting Text Box to Date
So you were using VB.Net ?
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
I'll request a mod to move this thread to the VB.Net section.
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Oct 23rd, 2011, 05:58 AM
#9
Re: Converting Text Box to Date
Thread moved from the 'VB6 and Earlier' forum to the 'VB.Net' (VB2002 and later) forum (thanks akhileshbc )
-
Oct 23rd, 2011, 06:14 AM
#10
Re: Converting Text Box to Date
What data type is the field in the database?
-
Oct 23rd, 2011, 12:18 PM
#11
Thread Starter
New Member
Re: Converting Text Box to Date
-
Oct 23rd, 2011, 05:09 PM
#12
Thread Starter
New Member
Re: Converting Text Box to Date
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|