Results 1 to 25 of 25

Thread: [RESOLVED] How to check if a Word in a TextBox contains "+" Dosnt work For me

  1. #1

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Resolved [RESOLVED] How to check if a Word in a TextBox contains "+" Dosnt work For me

    i want to check if a text box contains "+" but what ever i wright in the code it just passes straight to end if
    Code:
           If ChkProcess.Value = True Then
             If InStr(TxtServiceName.Text, "+") <> 0 Then
                TxtServiceName.SetFocus
                MsgBox "the text Must Contain This Character before proceeding", vbInformation
                Exit Sub
            End If
          End If
    tnx for any help
    salsa31

  2. #2

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: How to check if a Word in a TextBox contains "+" Dosnt work For me

    i think i got it
    Code:
    If Not InStr(TxtServiceName.Text, "+") > 0 Then

  3. #3
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to check if a Word in a TextBox contains "+" Dosnt work For me

    InStr(TxtServiceName.Text, "+") <> 0 is correct. So is InStr(TxtServiceName.Text, "+") > 0

    If Not InStr(TxtServiceName.Text, "+") > 0 is to check for the absence of the + mark


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  4. #4

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: How to check if a Word in a TextBox contains "+" Dosnt work For me

    hey james i didnt quite understand what you said

    to use if not <> 0 or If Not InStr(TxtServiceName.Text, "+") > 0?

  5. #5
    Hyperactive Member Daniel Duta's Avatar
    Join Date
    Feb 2011
    Location
    Bucharest, Romania
    Posts
    396

    Re: How to check if a Word in a TextBox contains "+" Dosnt work For me

    I wonder why InStr(TxtServiceName.Text, "+") <> 0 or InStr(TxtServiceName.Text, "+") > 0 or If Not InStr(TxtServiceName.Text, "+") > 0 when InStr(TxtServiceName.Text, "+") = 0 does the same thing directly?

  6. #6
    Member
    Join Date
    Mar 2015
    Posts
    63

    Re: How to check if a Word in a TextBox contains "+" Dosnt work For me

    if instr(1,Text1.Text,"+") then
    'Your code
    else
    'Your code
    end if

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to check if a Word in a TextBox contains "+" Dosnt work For me

    Quote Originally Posted by salsa31 View Post
    i want to check if a text box contains "+" but what ever i wright in the code it just passes straight to end if
    Code:
           If ChkProcess.Value = True Then
             If InStr(TxtServiceName.Text, "+") <> 0 Then
                TxtServiceName.SetFocus
                MsgBox "the text Must Contain This Character before proceeding", vbInformation
                Exit Sub
            End If
          End If
    tnx for any help
    salsa31
    Basically you are telling it the opposite of what you want
    Code:
    If InStr(TxtServiceName.Text, "+") <> 0 Then
    Is the same as saying
    If + exists in the string then do this
    The value will be 0 if it does not exist and will be >0 if it does

    Code:
    If Not InStr(TxtServiceName.Text, "+") > 0 Then
    This is functional but not how I would do it

    I would use either
    Code:
    If Not InStr(TxtServiceName.Text, "+") Then
    or
    Code:
    If InStr(TxtServiceName.Text, "+")=0 Then
    I wonder why InStr(TxtServiceName.Text, "+") <> 0 or InStr(TxtServiceName.Text, "+") > 0 or If Not InStr(TxtServiceName.Text, "+") > 0 when InStr(TxtServiceName.Text, "+") = 0 does the same thing directly?
    They do not all do the same thing 1 and 2 will be true if the + exists in the string 3 and 4 will be false if it exists

  8. #8
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: How to check if a Word in a TextBox contains "+" Dosnt work For me

    Just to butt in and help simplify (although DM did a good job) furthur....
    the function Instr() returns the location (integer) where the search string (in your case "+") starts.
    If the string is not found, it returns zero (0).

    If you have a textbox with "HairDry + Manicure" you could did this:

    Code:
    if instr(text1.text,"+") = 0 then
          'code here would NOT run because instr() returned a 9 (9th character, including the space)
    else
          'code here WOULD run because 9 is not equal to 0
    end if
    OR

    Code:
    if NOT instr(text1.text,"+") then  'same as if instr(text1,text,"+") <> 0
          'run code here
    end if
    Last edited by SamOscarBrown; Mar 14th, 2015 at 08:49 AM.

  9. #9
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: How to check if a Word in a TextBox contains "+" Dosnt work For me

    ...and, for example, if you are searching for a longer string, like "salsa"..
    In this example, you tell me what value would be returned to x:

    Dim x as integer
    x=instr("Where in the world is salsa sandiego?","salsa")

  10. #10

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: How to check if a Word in a TextBox contains "+" Dosnt work For me

    i see your point the problem is if i have 3 + then what?
    i mean like this
    Code:
    HairdDry + Manicure + HairCut
    i will get a error again and again
    the combobox can contain up to 3 names in 1 line

  11. #11
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: How to check if a Word in a TextBox contains "+" Dosnt work For me

    It returns the FIRST occurrence.
    Let me ask you...why are you now trying to use Instr()....the Split() command all have described to you is more applicable for your issue.

    Dim strParts() as String
    strParts = Split(combobox.text,"+")

    then, just check for ubound of strParts to find out how many treatments there are

    in this case, strParts(0) would equal HairDry
    strParts(1) would equal Manicure
    and strParts(2) would equal Haircut

    and ubound of strParts would be 2...so number of treatments would be 3 (0 plus 1 plus 2)

    YOu don't need instr() function at all here.

  12. #12

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: How to check if a Word in a TextBox contains "+" Dosnt work For me

    Let me ask you...why are you now trying to use Instr()....the Split() command all have described to you is more
    beacuse i am making inserts if the combobox contains then 1 more name
    e.x
    if combobox has 1 name only with out this + then i use only 1 insert
    if combobox has 2 names [name1+name2] then i insert twice
    if combobox has 3 names[name1+name2+name3] then insert 3 times

  13. #13
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to check if a Word in a TextBox contains "+" Dosnt work For me

    I showed you how to use a loop on the Ubound.
    You do not need to know how many are there.
    You do not need to use Instr() at all

    You need to follow instructions and ask questions if you do not understand

  14. #14

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: How to check if a Word in a TextBox contains "+" Dosnt work For me

    You need to follow instructions and ask questions if you do not understand
    i did sir and i dont understand
    i am trying to learn from you amigos but what seems to you easy is hard for me with lack of experience i have
    self learning is very hard then school learning sir

  15. #15
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to check if a Word in a TextBox contains "+" Dosnt work For me

    Quote Originally Posted by salsa31 View Post
    i did sir and i dont understand
    What don't you understand? As I said you need to ask questions if you do not understand.

    i am trying to learn from you amigos but what seems to you easy is hard for me with lack of experience i have
    self learning is very hard then school learning sir
    Trust me I know what it is like to learn from self study. This is how I learned to do what I do and I did it when there was no internet to search and no one to ask questions. I literally learned by myself using just the book that came with the PC and a lot of trial and error or at least that is how I started. Later I did buy additional books and newer versions of languages came with online help and then eventually I got a job where there were other experienced programmers that I could talk to and of course the internet came to be a great resource.

    Learning is much easier if you pay close attention to examples and make sure you understand them before moving on. If someone gives you code that works but you do not understand and you just move on then when you face the same issue again you still will not know how to deal with it.

    Ask direct, detailed questions when you do not understand something. Give full detailed answers when someone asks you a question.

  16. #16
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: How to check if a Word in a TextBox contains "+" Dosnt work For me

    Look at this simple program....

    Attachment 124701

  17. #17

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: How to check if a Word in a TextBox contains "+" Dosnt work For me

    tnk you very much sami
    Learning is much easier if you pay close attention to examples and make sure you understand them before moving on. If someone gives you code that works but you do not understand and you just move on then when you face the same issue again you still will not know how to deal with it.
    you are right DM sir i need to pay more attention i appreciate your help and time

  18. #18
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to check if a Word in a TextBox contains "+" Dosnt work For me

    Quote Originally Posted by Daniel Duta View Post
    I wonder why InStr(TxtServiceName.Text, "+") <> 0 or InStr(TxtServiceName.Text, "+") > 0 or If Not InStr(TxtServiceName.Text, "+") > 0 when InStr(TxtServiceName.Text, "+") = 0 does the same thing directly?
    You might want to re-think this


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  19. #19
    Hyperactive Member Daniel Duta's Avatar
    Join Date
    Feb 2011
    Location
    Bucharest, Romania
    Posts
    396

    Re: How to check if a Word in a TextBox contains "+" Dosnt work For me

    Quote Originally Posted by jmsrickland View Post
    You might want to re-think this
    What should I re-think? Let's take the string s="One+two". If InStr(s, "+")=0 then my text doesn't contain the "+" character. The question was simply "How to check if a Word in a TextBox contains "+". Sorry but I cannot get other meaning.

  20. #20
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] How to check if a Word in a TextBox contains "+" Dosnt work For me

    I wonder why
    InStr(TxtServiceName.Text, "+") <> 0 or
    InStr(TxtServiceName.Text, "+") > 0 or
    Not InStr(TxtServiceName.Text, "+") > 0 when
    InStr(TxtServiceName.Text, "+") = 0 does the same thing directly?

    Because you are making it sound like the last statement does the same thing as the first three statements and that is not correct. Actually, the first two are the same and the last two are the same

    Also, since the question was "How to check if a Word in a TextBox contains "+" then you should use <> 0 or > 0 to see if the word contains a + sign where you do just the opposite using = 0 otherwise OP's If statement will not be executed and OP wants to enter the If clause if it does where your InStr using = 0 will not enter the If clause.
    Last edited by jmsrickland; Mar 14th, 2015 at 02:09 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  21. #21
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: [RESOLVED] How to check if a Word in a TextBox contains "+" Dosnt work For me

    Actually in the OP it is entering the IF clause when the + exists where it then tells the user it does not exist

    In post 2 it enters when it does not exist

  22. #22
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] How to check if a Word in a TextBox contains "+" Dosnt work For me

    I went by the If statement; not what was in the clause although I would say OP wants to test if + does not exists and tell user


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  23. #23
    Hyperactive Member Daniel Duta's Avatar
    Join Date
    Feb 2011
    Location
    Bucharest, Romania
    Posts
    396

    Re: [RESOLVED] How to check if a Word in a TextBox contains "+" Dosnt work For me

    Quote Originally Posted by jmsrickland View Post
    Actually, the first two are the same and the last two are the same
    That is true. But as Datamiser has noted, in the second post Salsa has hinted he actually wants to find out if that character is missing from that string
    i think i got it Code: If Not InStr(TxtServiceName.Text, "+") > 0 Then
    that is the same thing with If InStr(TxtServiceName.Text, "+") = 0 Then . I thought there is no confusion about it. On the other hand, it is sufficient to prove any value of a boolean variable even you are interested of other because of its dichotomy : False= nonTrue and vice versa. Finally, let's avoid a debate on such a trivial matter.
    Last edited by Daniel Duta; Mar 14th, 2015 at 04:53 PM.

  24. #24
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] How to check if a Word in a TextBox contains "+" Dosnt work For me

    OK, but that's not what it appears that you posted


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  25. #25
    Hyperactive Member Daniel Duta's Avatar
    Join Date
    Feb 2011
    Location
    Bucharest, Romania
    Posts
    396

    Re: [RESOLVED] How to check if a Word in a TextBox contains "+" Dosnt work For me

    James, you are a valuable poster of this forum. It is obvious it was just a little misunderstanding. Sorry for any inconvenience caused.

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