Ask Experts Questions for FREE Help !
Ask
    lamlamlamiaaa's Avatar
    lamlamlamiaaa Posts: 28, Reputation: 2
    New Member
     
    #1

    May 7, 2012, 03:07 PM
    Word Finder Help on VB!
    So far, I made a program where the user enters a sentence in one textbox and then the user enters a word they want to find in that sentence in another text box. After clicking the button, the program will show the word in capital letters. But, the problem is that, it only capitalizes the first word it finds. For example, "Hi my name is visual basic and I love VIsual basic" IF the user wants to find "Visual" it only capitalizes the first visual and not the second one. I need it so that it does that. Here is my code so far :


    Dim sentence As String
    Dim word As String
    Dim location As String
    Dim new_sentence As String

    sentence = TextBox1.Text
    word = TextBox2.Text
    location = InStr(sentence, word)
    found = found + 1

    new_sentence = Mid(sentence, 1, location - 1) & UCase(word) & Mid(sentence, location + Len(word))
    Label3.Text = new_sentence


    It would be lots of help!
    Thank you (;
    jsblume's Avatar
    jsblume Posts: 13, Reputation: 2
    New Member
     
    #2

    Sep 28, 2012, 11:21 AM
    The problem is that the way the InStr function is being used you are always finding only the first. What you need is a loop

    You don't need the new_sentence variable. Notice inside the While-Wend loop that I call the InStr function with three arguments instead of two. When the first argument is a number, then it indicates the starting position in the string for the search.

    Dim sentence As String
    Dim word As String
    Dim location As String
    Dim new_sentence As String

    sentence = TextBox1.Text
    word = TextBox2.Text
    location = InStr(sentence, word)
    found = 0

    while location > 0

    found += 1

    sentence = Mid(sentence, 1, location - 1) & UCase(word) & Mid(sentence, location + Len(word))

    location = InStr(location + 1, sentence, word)

    wend

    Label3.Text = sentence

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Horizontal ruler word 13 inches in Word 2004? [ 1 Answers ]

I'm using Microsoft Word 2004 for Mac and just started having troubles with the horizontal ruler showing 13 inches even though the page set up is set at portrait and letter size. I've tried shutting down & restarting, changing margins on the document, making sure that the track changes isn't on. ...

Outlook Attached word document opens after 30 seconds if you have word open [ 0 Answers ]

We have a weird issue on our XenApp 6 test environment using Outlook 2007 to open Word attachments. The behaviour is such that if a Word instance is already open and maximised with a document and we try to open a Word attachment from an e-mail, the attachment doesn't open unless you un-maximise the...

Converting files from Microsoft Word to Word Perfect 11? [ 3 Answers ]

I use a laptop with Microsoft Word for traveling and my home PC has Word Perfect 11. I have since installed Word Perfect on my laptop. When I try to open a file on my laptop that I created with Word, it says file cannot be located. How can I integrate and have all files on both computers open with...


View more questions Search