Results 1 to 17 of 17

Thread: Im lost... I cant figure out why this doesnt work

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    WA
    Posts
    74

    Question Im lost... I cant figure out why this doesnt work

    this code will repeat whatever youve typed in. but i want it to replace any "two" in the text with a 2. I thought this would work but it doesnt and i have no idea why, so if anyone knows I would much apreciate an answer.



    Dim num(5020) As String

    Private Sub Command1_Click()
    For syl = 1 To 5000
    num(syl) = Mid$(Text1.Text, (syl), 1)
    Next syl

    For spf = 1 To 5000
    Select Case num(spf)
    Case num(spf) = "t" And num(spf + 1) = "w" And num(spf + 2) = "o"
    num(spf) = "2"
    num(spf + 1) = ""
    num(spf + 2) = ""
    End Select
    Next spf

    For rite = 1 To 5000
    Label1.Caption = Label1.Caption & num(rite)
    Next rite

    End Sub
    §ñéªk¥

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Do you have to do it that way?


    If not, maybe you can use the Replace function, like:
    VB Code:
    1. sBuff = Replace(sBuff, "two", " 2 ", , vbTextCompare)
    Last edited by Bruce Fox; Nov 6th, 2002 at 10:24 PM.

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Off hand I don't see anything wrong, but you might try changing
    num(syl) = Mid$(Text1.Text, (syl), 1)

    to

    num(syl) = Mid$(Text1.Text, syl, 1)

    It would be better however to use the Replace function and look for " two " (note the space before and after the two) and replace it with " 2 ".

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    use the replace function:

    VB Code:
    1. Dim num(5020) As String
    2.  
    3. Private Sub Command1_Click()
    4.   Text1.Text = Replace(Text1.Text, "two", "2", , , vbTextCompare)
    5.   For syl = 1 To 5000
    6.     num(syl) = Mid$(Text1.Text, (syl), 1)
    7.   Next syl
    8.  
    9.   For rite = 1 To 5000
    10.     Label1.Caption = Label1.Caption & num(rite)
    11.   Next rite
    12. End Sub

    Edit: Man, beaten again
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    WA
    Posts
    74

    Thumbs up Oh

    i didnt know about the replace thing becouse im a newb so im always improvising, but the replace thing looks much easier so now i just gotta figure out all about that.
    §ñéªk¥

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    I just saw that I got beaten to the puch on my Replace suggestion, but it's better to look for " two " than "two" because with the latter you would change "two-by-four" to "2-by-four" and you probably don't want that.

  7. #7
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by MartinLiss
    I just saw that I got beaten to the puch on my Replace suggestion, but it's better to look for " two " than "two" because with the latter you would change "two-by-four" to "2-by-four" and you probably don't want that.
    Then you need to account for .'s and ,'s, etc. It could be a mess.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    WA
    Posts
    74

    ok

    thx, do you know were i can find a tutorial about that command or should i just experiment with it?
    §ñéªk¥

  9. #9
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    And................ the problem was with the CASE statement.

    It never occured.

    Although this works, it isn't the way to go:
    VB Code:
    1. For spf = 1 To 5000
    2.     Select Case [b](num(spf) = "t" And num(spf + 1) = "w" And num(spf + 2) = "o")[/b]
    3.         Case True
    4.         num(spf) = "2"
    5.         num(spf + 1) = ""
    6.         num(spf + 2) = ""
    7.     End Select
    8. Next spf
    As I'm guessing ur going to do more letter matches.

    As I mentioned (and others) use the Replace function.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    WA
    Posts
    74

    Exclamation AHA!

    no wonder! o well, now i need to know what vbTextCompare does.
    §ñéªk¥

  11. #11
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Type the word Replace into you IDE, highlight it, and press F1.

    That will give you the MSDN Help on Replace....

    Note: Replace is applicable only to VB6

    Also,
    I extracted this from VBA.


    Description
    Replace
    Returns a string in which a specified substring has been replaced with another substring a specified number of times.

    Syntax

    Replace(expression, find, replace[, start[, count[, compare]]])

    The Replace function syntax has these named arguments:

    Part Description
    expression Required. String expression containing substring to replace.
    find Required. Substring being searched for.
    replace Required. Replacement substring.
    start Optional. Position within expression where substring search is to begin. If omitted, 1 is assumed.
    count Optional. Number of substring substitutions to perform. If omitted, the default value is –1, which means make all possible substitutions.
    compare Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. See Settings section for values.



    Settings

    The compare argument can have the following values:

    Constant Value Description
    vbUseCompareOption –1 Performs a comparison using the setting of the Option Compare statement.
    vbBinaryCompare 0 Performs a binary comparison.
    vbTextCompare 1 Performs a textual comparison.
    vbDatabaseCompare 2 Microsoft Access only. Performs a comparison based on information in your database.



    Return Values

    Replace returns the following values:

    If Replace returns
    expression is zero-length Zero-length string ("")
    expression is Null An error.
    find is zero-length Copy of expression.
    replace is zero-length Copy of expression with all occurences of find removed.
    start > Len(expression) Zero-length string.
    count is 0 Copy of expression.



    Remarks

    The return value of the Replace function is a string, with substitutions made, that begins at the position specified by start and and concludes at the end of the expression string. It is not a copy of the original string from start to finish.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    WA
    Posts
    74

    Lightbulb thx

    i dont have the msdn part so i cant use that but what you gave me was enough.
    §ñéªk¥

  13. #13
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: thx

    Originally posted by original_sneaky
    i dont have the msdn part ...........

    Do you M$ Word, Access or Excell?


    If so, you can do the same (F1) thing in VBA.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    WA
    Posts
    74

    Talking Oh

    some people learn something new every day.

    i learn a million things!
    §ñéªk¥

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    WA
    Posts
    74
    if this works:

    Dim num(5020) As String

    Private Sub Command1_Click()
    Text1.Text = Replace(Text1.Text, "two", "2", , , vbTextCompare)
    For syl = 1 To 5000
    num(syl) = Mid$(Text1.Text, (syl), 1)
    Next syl

    For rite = 1 To 5000
    Label1.Caption = Label1.Caption & num(rite)
    Next rite
    End Sub


    than shouldnt this work? all i do is add spaces before and after the two and 2, or do i have to give starting locations and ending ones now?:


    Dim num(5020) As String

    Private Sub Command1_Click()
    Text1.Text = Replace(Text1.Text, " two ", " 2 ", , , vbTextCompare)
    For syl = 1 To 5000
    num(syl) = Mid$(Text1.Text, (syl), 1)
    Next syl

    For rite = 1 To 5000
    Label1.Caption = Label1.Caption & num(rite)
    Next rite
    End Sub
    §ñéªk¥

  16. #16
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Some basics:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4. Dim strString As String
    5.  
    6.   strString = "Hey guys!"
    7.   MsgBox strString
    8.  
    9.   strString = Replace(strString, "guys", "dudes", , , vbTextCompare)
    10.   MsgBox strString
    11.  
    12.   strString = Replace(strString, "hey", "Yo", , , vbTextCompare)
    13.   MsgBox strString
    14.  
    15.   'notice this one will not replace it, since it's case sensitive
    16.   'when it lacks the vbTextCompare
    17.   strString = Replace(strString, "DUDES", "Fuzz")
    18.   MsgBox strString
    19. End Sub
    My evil laugh has a squeak in it.

    kristopherwilson.com

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    WA
    Posts
    74
    999,982 things learned, 18 things to go.
    §ñéªk¥

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