1
Sub guessLetter(letterGuess As String)
    Dim lengthOfSecretWord As Integer
    lengthOfSecretWord = Len(Secret_word) - 1
    tempWord = ""
    Dim letterPosition As Integer
    For letterPosition = 0 To lengthOfSecretWord
        If Mid(Secret_word, letterPosition, 1) = letterGuess Then
            tempWord = tempWord & letterGuess
        Else
            tempWord = tempWord & Mid(lblTempWord, letterPosition, 1)
        End If
    Next
    lblTempWord = tempWord
End Sub

I have runtime error "5" and the problem in line IF, i'm stuck to declare Secret_word.substr(letterPosition, 1) on vb6, first i try write Secret_word.substr(letterPosition, 1) but it can't then i try to manipulate that then runtime error 5 came

3
  • 1
    Error 5 is Invalid procedure call or argument for those who dont have all the error codes from a 20 year old product memorized Commented Oct 26, 2017 at 19:18
  • A couple of things to consider: use Option Explicit and declare all your variables. Your issue is with the For loop... start at 1, not 0. Commented Oct 26, 2017 at 19:36
  • 2
    Every legal copy of VB6 comes with full online documentation, there is no excuse for not being able to look up error codes. Commented Oct 26, 2017 at 22:37

1 Answer 1

2

The Mid Function in VB (like most things in VB) is 1-indexed, not 0-indexed.

I'm assuming you're familiar with other languages in which you would loop from 0 to Len(String)-1, but VB thinks you'll find it more intuitive to loop from 1 to Len(String).

Refer to the description and example in the documentation for more details.

Sign up to request clarification or add additional context in comments.

1 Comment

@hadiprasetyo: Glad to hear it worked! If my answer answered your question satisfactorily, you can clicking the check mark near my answer to mark the question as being answered. Welcome to Stack Overflow! Be sure to check out the Help center and Tour pages if you haven't already.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.