Results 1 to 12 of 12

Thread: [RESOLVED] Converting Text Box to Date

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    6

    Resolved [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?

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    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:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Dim a As String
    5.     Dim b As Date
    6.    
    7.     a = "20110901"  '~~~ Sample string
    8.     b = DateSerial(Mid$(a, 1, 4), Mid$(a, 5, 2), Mid$(a, 7, 2)) '~~~ Converting it into a date format
    9.    
    10.     MsgBox Format$(b, "mm/dd/yyyy") '~~~ Display this converted date in whatever format you want
    11. 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,...

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    6

    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

  5. #5
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Converting Text Box to Date

    I didn't see any problem when tested !
    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Dim a As String
    5.     Dim b As Date
    6.    
    7.     a = "20110519"  '~~~ Sample string
    8.     b = DateSerial(Mid$(a, 1, 4), Mid$(a, 5, 2), Mid$(a, 7, 2)) '~~~ Converting it into a date format
    9.    
    10.    Text1.Text = Format$(b, "mm/dd/yyyy") '~~~ Display this converted date in whatever format you want
    11. 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,...

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    6

    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.

  7. #7
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    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?

  8. #8
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Converting Text Box to Date

    So you were using VB.Net ?

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     '~~~ Method1: Rearranging and appending (suggested by dilettante)
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         Dim a As String = "20110519"
    6.         Dim b As String = String.Concat(a.Substring(4, 2), "/", a.Substring(6, 2), "/", a.Substring(0, 4))
    7.  
    8.         MessageBox.Show(b)
    9.     End Sub
    10.  
    11.     '~~~ Method2: Converting to a date and then displaying it in the appropriate format
    12.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    13.         Dim a As String = "20110519"
    14.         Dim b As Date = New Date(a.Substring(0, 4), a.Substring(4, 2), a.Substring(6, 2))
    15.  
    16.         MessageBox.Show(b.ToString("MM/dd/yyyy"))
    17.     End Sub
    18. 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,...

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Converting Text Box to Date

    Thread moved from the 'VB6 and Earlier' forum to the 'VB.Net' (VB2002 and later) forum (thanks akhileshbc )

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Converting Text Box to Date

    What data type is the field in the database?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  11. #11

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    6

    Re: Converting Text Box to Date

    System.Int32

  12. #12

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    6

    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
  •  



Click Here to Expand Forum to Full Width