Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Visual Basic (https://www.askmehelpdesk.com/forumdisplay.php?f=469)
-   -   Find and Replace text on text box in vb6 (https://www.askmehelpdesk.com/showthread.php?t=488441)

  • Jul 14, 2010, 08:26 PM
    andirst
    Find and Replace text on text box in vb6
    Hi All,

    I have some problem with vb6 code.
    I have a data contain :

    :25:0186350587
    :25:186350587
    :25:86350587
    :25:6350587
    :25:350587
    :25:50587
    :25:587
    :25:87
    :25:7

    I want it to insert zero number after :25: , but the number after :25: is always has to be 10 digit. So it would be :

    :25:0186350587
    :25:0186350587
    :25:0086350587
    :25:0006350587
    :25:0000350587
    :25:0000050587
    :25:0000000587
    :25:0000000087
    :25:0000000007

    I have a code in vb6 but it only insert two digit of zero number, so it would be :

    :25:000186350587 (more than 10 digit)
    :25:00186350587 (more than 10 digit)
    :25:0086350587 (10 digit)
    :25:006350587 (less than 10 digit)
    :25:00350587 (less than 10 digit)
    :25:0050587 (less than 10 digit)
    :25:00587 (less than 10 digit)
    :25:0087 (less than 10 digit)
    :25:007 (less than 10 digit)

    this is my code with textbox and command :

    Private Sub cmdProses_Click()
    Dim txt
    txt = TextBox.Text
    If InStr(1, TextBox.Text, ":25:") > 0 Then
    TextBox.Text = Replace(txt, ":25:", ":25:00")
    End If
    End Sub

    Would you like to help me with the new code please

    Thank you
    Andi.
  • Jul 14, 2010, 11:56 PM
    LTheobald
    You need to figure out how long the text is and use that to create the 0's. I've not done much VB so this code may not be correct but try this. Instead of:

    Code:

    TextBox.Text = Replace(txt, ":25:", ":25:00")
    Insert this:
    Code:

    dim toAdd = 10 - (Len(txt)-4)
    dim zeros
    For i = 0 To toAdd
      zeros = zeros & "0"
    Next i
    TextBox.Text = Replace(txt, ":25:", ":25:" & zeros)

    That could be wrong but it gives you something to go from. Here's what it does line by line.
    Line 1: Create a variable containing how many zeros we need to add. It's the desired amount of numbers in total (10) minus the length of the string minus 4 (the length of ":25:").
    Line 2: Create a variable to hold the text containing our extra 0's
    Line 3: Loop for the amount of zeros we want to add
    Line 4: Append an extra 0 to the end of the zeros variable
    Line 5: Quit the loop
    Line 6: Perform the replacement with our string containing the extra zero's

    Hope that helps (and works!)
  • Jul 14, 2010, 11:57 PM
    LTheobald
    Comment on LTheobald's post
    Better formatted code:

    dim toAdd = 10 - (Len(txt)-4)
    dim zeros
    For I = 0 To toAdd
    zeros = zeros & "0"
    Next I
    TextBox.Text = Replace(txt, ":25:", ":25:" & zeros)
  • Jul 15, 2010, 12:24 AM
    andirst
    Thanks LThebald
    It give me some new perspective.

    Andi

    FYI
    This is the link of my complete program and sample data that I want to change
    http://docs.google.com/leaf?id=0B8HOcoYLae3IMTU5YjMxNjUtNjg3MS00MjQwLTkwZ mUtNjI2M2ZiMDNkMmM2&sort=name&layout=list&num=50

  • All times are GMT -7. The time now is 06:27 PM.