Results 1 to 10 of 10

Thread: Text To Number question?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    269

    Text To Number question?

    Hi,

    In my MS table, I have a field defined as TEXT field like "1/05/2004" and I would like convert it as number field like "20040501"

    What is the best way to do?

    Thanks!

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Text To Number question?

    You want to convert the field itself, or the data in the field when you bring it out?

    Are there backslashes in all entries?

  3. #3
    Lively Member ComITSolutions's Avatar
    Join Date
    Feb 2008
    Location
    Bangalore
    Posts
    94

    Re: Text To Number question?

    Code:
    Dim TmpStr as string
    Dim SplitVal() as string
    Dim RsltStr as string
    TmpStr="1/05/2004"  
    
    SplitVal=Split(TmpStr,"/")
    If Ubound(SplitVal)=2 then
    
        RsltStr = SplitVal(2) & iif( val(SplitVal(1)) <10 ,"0","") & val(SplitVal(1)) & iif( val(SplitVal(0)) <10 ,"0","") & val(SplitVal(0))
    else
          MsgBox "invalid string"
    Endif
    Encourage the fellow member’s efforts by rating

    - ComIT Solutions

  4. #4
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Text To Number question?

    I created a form with two labels on it and then applied this code:
    Code:
    Dim MyText As String, MyNumber As String, Dim VarSplit() As String
    Private Sub Form_Load()
    MyText = "1/05/2004"
    VarSplit = Split(MyText, "/")
    Label1.Caption = "Text Field is " & MyText
    MyNumber = VarSplit(UBound(VarSplit)) & VarSplit(1) & Format$(VarSplit(0), "00")
    Label2.Caption = "Number Field is " & MyNumber
    End Sub
    Doctor Ed

  5. #5
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Text To Number question?

    Assuming you are working with dates, this should work.
    Code:
    Private Sub Command1_Click()
    Dim strDate As String
    
        strDate = "1/05/2004"
        
        MsgBox Format(CDate(strDate), "yyyyddmm")
    End Sub

  6. #6
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Text To Number question?

    Quote Originally Posted by MarkT
    Assuming you are working with dates, this should work.
    Code:
    Private Sub Command1_Click()
    Dim strDate As String
    
        strDate = "1/05/2004"
        
        MsgBox Format(CDate(strDate), "yyyyddmm")
    End Sub
    Shucks, Mark, assume he is not working with dates. Your code still works like a charm.
    Doctor Ed

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Text To Number question?

    The date functions are regional settings dependent when converting from dates in string to datetime variables. It'll only work like a charm assuming no one messes with these settings.

  8. #8
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Text To Number question?

    Code:
    Public Function RightOf(ByVal SearchIn As String, ByVal StartRight As String) As String
        RightOf = Right$(SearchIn, Len(SearchIn) - (Val(InStr(1, SearchIn, StartRight)) + Len(StartRight) - 1))
    End Function
    Public Function LeftOf(ByVal SearchIn As String, ByVal StartLeft As String) As String
        LeftOf = Replace$(SearchIn, Right$(SearchIn, Len(SearchIn) - Val(InStr(1, SearchIn, StartLeft) - 1)), vbNullString)
    End Function
    
    Function DoThing(ByVal SEntry As String) As String
    Dim rS As String
        DoThing = RightOf(RightOf(SEntry, "/"), "/") & LeftOf(RightOf(SEntry, "/"), "/")
        rS = LeftOf(SEntry, "/")
        If LenB(rS) = 2 Then rS = "0" & rS
            DoThing = DoThing & rS
    End Function

  9. #9
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Text To Number question?

    Quote Originally Posted by Zach_VB6
    Code:
    Public Function RightOf(ByVal SearchIn As String, ByVal StartRight As String) As String
        RightOf = Right$(SearchIn, Len(SearchIn) - (Val(InStr(1, SearchIn, StartRight)) + Len(StartRight) - 1))
    End Function
    Public Function LeftOf(ByVal SearchIn As String, ByVal StartLeft As String) As String
        LeftOf = Replace$(SearchIn, Right$(SearchIn, Len(SearchIn) - Val(InStr(1, SearchIn, StartLeft) - 1)), vbNullString)
    End Function
    
    Function DoThing(ByVal SEntry As String) As String
    Dim rS As String
        DoThing = RightOf(RightOf(SEntry, "/"), "/") & LeftOf(RightOf(SEntry, "/"), "/")
        rS = LeftOf(SEntry, "/")
        If LenB(rS) = 2 Then rS = "0" & rS
            DoThing = DoThing & rS
    End Function
    For parsing a fixed format with slashes as delimiter, try using Split() instead.

  10. #10
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Text To Number question?

    Another way:
    Code:
    Dim strString As String
    Dim strOut As String
    Dim strBit As String
    Dim intI As Integer
    Dim intPos As Integer
    strString = "1/5/2004"
    intI = Len(strString)
    Do
        intPos = InStrRev(strString, "/", intI)
        strBit = Mid(strString, intPos + 1, intI - intPos)
        If Len(strBit) = 1 Then strBit = "0" & strBit
        strOut = strOut & strBit
        intI = intPos - 1
    Loop Until intI <= 0
    Debug.Print strOut

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