0

I'm creating a chatbot using VB6 and the whole basis of this chatbot is that whenever a user writes a 'trigger word', such as "good", "amazing", etc., the chatbot will reply "that's great". However, whenever I don't write the trigger word, only the msgbox which says "Word is in this" appears, and I don't know what I'm doing wrong. I've tried messing around with the >, <, = signs to no avail. Any help will be appreciated.

  Private Sub conversation()
    Dim imput As String 'where user types to chatbot

    Dim arrWords As Variant, aWord As Variant
    arrWords = Array("good", "great", "bad")

    Dim wordFound As Boolean
    wordFound = False
    For Each aWord In arrWords
        If InStr(imput, aWord) = 0 Then
            wordFound = True And MsgBox "Word is in this"
        ElseIf InStr(imput, aWord) > 0 Then
            wordFound = False And MsgBox "Word is not in this"
        End If
    Next
End Sub
4
  • Please provide a Minimal, Complete, and Verifiable example and explain exactly what is not working as expected along with any errors you might be getting. Commented Aug 18, 2018 at 9:59
  • "However, when writing the trigger word, only the msgbox which says "Word is in this" appears" - That sounds like exactly what you want - a message box that says "words is in this" when the trigger word is in the input. Commented Aug 19, 2018 at 7:00
  • 1
    However, wordFound = True And MsgBox "Word is in this" is a syntax error. Your code does not even run. Commented Aug 19, 2018 at 7:05
  • My bad, I'll edit the question. I meant that even if I write or don't write the trigger word, msgbox "word is in this" comes up. I've tried messing around with the >, <, = signs, to no avail. Commented Aug 19, 2018 at 9:13

3 Answers 3

1

Other answers have already explained that your logic is backwards, which it is.

But also, I don't know whether you have accurately copied your code or not, but when I copy it into VB, I get — as I expected — syntax errors on the lines with MsgBox in them. I can "fix" this by adding parentheses to the MsgBox arguments, so: wordFound = True And MsgBox("Word is in this"), etc. But that isn't good code, for reasons I will explain, and I have a few other suggestions.

Consider these changes to your code:

Private Sub conversation(theInput As String)

    Dim arrWords As String, aWord As String
    arrWords = Array("good", "great", "bad")

    Dim wordFound As Boolean
    wordFound = False
    For Each aWord In arrWords
        If InStr(theInput, aWord) = 0 Then
            wordFound = False
            MsgBox """" & aWord & """ is in this"
        Else
            wordFound = True 
            MsgBox """" & aWord & """ is not in this"
        End If
    Next
End Sub

Private Sub SendButton_Click()
    conversation(myChatTextBox.Text)
End Sub

Ok. Here are some points.

  1. Don't use Variant unless you have a compelling reason to do so. It's the least efficient way to store information, because it has to allocate extra memory to tell internally what type of variable it is, and it also has to allocate enough memory to contain the largest possible type it could be. (A String variable has 10 bytes plus one per character in the string, while a Variant type allocated as a string has 22 bytes plus one per character in the string.)
  2. I changed imput to theInput. input is a reserved word in VB, so you can't use it, but it's clearer to other people if you don't misspell the word. Better to find some prefix to put on it.
  3. As others have said, when InStr returns zero, that means that the string in argument 2 isn't in the string in argument 1. So, it means that the "word isn't in this," not that it is. (That's the answer to the trouble you're having; the rest of this is just to improve your code overall.)
  4. wordFound = True And MsgBox("Word is in this") only works by coincidence. MsgBox returns a value of 1 when it runs successfully without arguments. (Who knew? I had to try it out for myself. Probably because it can be set up to return a number of different values for different types of ms) So your logic is wordFound = True And 1. And is a logical comparison operator: True And 1 evaluates to True, while False And 1 evaluates to False. So, you get what you want, but pretty much by accident. Put the two code bits on two different lines. You don't need to logically compare them; in fact it doesn't make sense to do so. (If you want to actually put two lines of code on the same line, put a colon between them: wordFound = True : MsgBox "etc", but this isn't generally considered good practice as the code is less readable. I have the feeling you thought you were using And to do this, and as you can see it does something quite different.)
  5. I changed your message to include the word you're looking for in quotes, for example "good" is in this. To get a literal quotation mark in a string, use two of them: "". (You have to put the two quotes in quotes themselves, since they are a quoted string; that's why there are four of them at the beginning and three later on.)
  6. You don't need an ElseIf here, because if your If condition is false, your ElseIf condition is true. You only need ElseIf if you are evaluating more than two possible conditions.
  7. I've set up the basic idea of how to send the chatbox user's input to your conversation subroutine. When the user clicks a Send button, you send the contents of the text box to conversation as the input argument. If you set it up as a local variable, you have to write some sort of code to grab the user's input. This is the cleaner way to do the job.

All that said, you can further simplify your For Each loop like this:

For Each aWord In arrWords
    wordFound = InStr(input, aWord) > 0 
    MsgBox """" & aWord & """ is" & IIf(wordFound, "", " not") & " in this"
Next

Explanations:

  1. InStr(input, aWord) <> 0 is either true or false. You assign whichever it is to wordFound. This is a more concise way of doing your If...Else. (A simpler example of the idea: x = 1 = 1 will set x equal to True, while x = 1 = 0 will set x equal to false. You can use parentheses to make it easier to understand: x = (1 = 1), etc.)
  2. IIf ("instant if") takes the form of IIf(condition, valueIfConditionIsTrue, valueIfConditionIsFalse).
  3. Since wordFound is a boolean value, saying wordFound is the same as saying wordFound = True, so you can omit the = True (you can also say Not wordFound to mean the same thing as wordFound = False).

EDIT: If you want your chatbot to reply "that's great" when any one of the words is encountered, change conversation() from a Sub to a Function and return true or false. Like this:

Private Function conversation(theInput As String) As Boolean
    Dim arrWords As String, aWord As String
    arrWords = Array("good", "great", "bad")

    'Get rid of your wordFound variable — you don't need it any more
    conversation = False
    For Each aWord In arrWords
        If InStr(theInput, aWord) > 0 Then
            conversation = True
        End If
    Next   
End Function

Private Sub SendButton_Click()
    If conversation(myChatTextBox.Text) 
        MsgBox "That's great!"
    Else
        MsgBox "That's not so great."
    End If
End Sub     
Sign up to request clarification or add additional context in comments.

Comments

1

Jim Mack's answer is correct; I would make the comparison more robust by adding (before the for loop):

  imput = UCase(imput)

and making the array of test words all uppercase. Fundamentally, there was just a logic problem with the original code (probably due to a misunderstanding of the language).

The ElseIf clause is somewhat worrisome, too; in general, it's wise to include an explicit else clause to handle any test that falls through all the previous conditions.

2 Comments

@SelcouthTurner This is a good idea as well. It has the effect of making the input case-insensitive.
In this particular case, the OP doesn't need an ElseIf anyway. Better to just use Else instead, since there isn't any test that falls through all the previous conditions.
0

I didn't try to run your code -- it doesn't even look like it would run -- but at least one error jumps out.

If InStr(imput, aWord) = 0 Then
  wordFound = True And MsgBox "Word is in this"'

...doesn't do what you seem to think it does. You're saying that wordFound will be true when the word is NOT in the input (InStr = 0), but only when the MsgBox returns a non-zero result

What you want is something like:

If InStr(imput, aWord) > 0 Then  ' >0 means word was found
  wordFound = True 
  MsgBox "Word is in this"'

The parallel is true for the 'not found' condition.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.