Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > String Problem


Reply
 
Thread Tools Display Modes
  #1  
Old 08-10-2004, 05:21 PM
DANY DANY is offline
Centurion
 
Join Date: Jun 2001
Posts: 128
Default String Problem


Hello to all,

Assume I have a field length of 10 digits long, a string type and the last four digits are considered to be the YEAR. Sometimes the user will enter it as shown below but i want it to be padded with leading zeros and also to pad the year
Code:
Before          After   (what I am looking for)
234/03        00024/2003
54576/99      54576/1999
567A/02       0567A/2002
78/98         00078/1998
786/2004      00786/2004
I need a funciton to pass the string value and as I type the value in the textbox, it will becomes as shown in the (AFTER) (what I am looking for.

thanks for any help.

Last edited by herilane; 08-11-2004 at 05:35 AM. Reason: [code] tags to line up the columns
Reply With Quote
  #2  
Old 08-10-2004, 06:33 PM
b0b b0b is offline
Contributor
 
Join Date: Jul 2003
Posts: 741
Default

im sure there's a better way but this works
Code:
Dim TmpStr() As String TmpStr = Split("786/2004", "/") TmpStr(0) = Format$(TmpStr(0), "00000") If Len(TmpStr(1)) < 4 Then 'if they've enter full year dont bother formatting If TmpStr(1) >= 0 And TmpStr(1) < 90 Then 'checks year TmpStr(0) = TmpStr(0) & "/" & Format$(TmpStr(1), "2000") Else TmpStr(0) = TmpStr(0) & "/" & Format$(TmpStr(1), "1900") End If Else TmpStr(0) = TmpStr(0) & "/" & TmpStr(1) End If MsgBox TmpStr(0)

Last edited by b0b; 08-10-2004 at 06:39 PM.
Reply With Quote
  #3  
Old 08-10-2004, 08:12 PM
DANY DANY is offline
Centurion
 
Join Date: Jun 2001
Posts: 128
Default

Hi,
Seems to work but if you have something like this,
it does not padd the leading zeros.

TmpStr = Split("786A/2004", "/")

its becomes 0786/2004

should be 0786A/2004

good job. make life easier.

thanks a bunch


Quote:
Originally Posted by b0b
im sure there's a better way but this works
Code:
Dim TmpStr() As String TmpStr = Split("786/2004", "/") TmpStr(0) = Format$(TmpStr(0), "00000") If Len(TmpStr(1)) < 4 Then 'if they've enter full year dont bother formatting If TmpStr(1) >= 0 And TmpStr(1) < 90 Then 'checks year TmpStr(0) = TmpStr(0) & "/" & Format$(TmpStr(1), "2000") Else TmpStr(0) = TmpStr(0) & "/" & Format$(TmpStr(1), "1900") End If Else TmpStr(0) = TmpStr(0) & "/" & TmpStr(1) End If MsgBox TmpStr(0)
Reply With Quote
  #4  
Old 08-11-2004, 04:23 AM
b0b b0b is offline
Contributor
 
Join Date: Jul 2003
Posts: 741
Default

Didn't notice the letters, well if you want to fix that then you can use the IsNumeric function, before you format the first half of the value check to see if it has letters in it. If IsNumeric returns false (its not a number) then use the String$ function to maually format the value by sticking the right number of zeros to the left of the value.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->