0

I am new to vb6 and I get a compiler error in my function when trying to return my variable.

"end of statement expected error vb6"

my function goes as following:

Public Function StringFormat(ByVal MyStr As String) As String
   
   Dim i As Integer
   Dim sBadChar As String
   Dim NewString As String
   
   ' List all illegal/unwanted characters
   sBadChar = "/<>?\{}[]()=,!#*:'*¬-"

   ' Loop through all the characters of the string
   For i = 1 To Len(MyStr)
       If InStr(sBadChar, Mid(MyStr, i, 1)) Then
           Mid(MyStr, i, 1) = ""
       End If
   Next i
   
   Return MyStr
   
End Function

I get the error on the return, any ideas as of why this happens? thank you in advance

4
  • "I am new to vb6..." No one should be new to VB6. :-) It's a dead technology. (VBA is still active, though.) If you're new to programming, FWIW I suggest studying something more modern, like VB.Net. Commented Oct 15, 2020 at 10:19
  • What line is the error complaining about? My VB6 is very rusty, but I suspect you want to compare the result of InStr with something, e.g. If InStr(sBadChar, Mid(MyStr, i, 1) > 0. But it might also be complaining about Mid(MyStr, i, 1) = "", I don't recall whether that's valid in VB6, but I suspect not. You probably want MyStr = Left(MyStr, i - 1) & Mid(MyStr, i) Note that you'll also have to handle the fact that i is now too far ahead (because the string got shorter). You could fix that by looping backward rather than forward (For i = Len(MyStr) To 1 Step -1). Commented Oct 15, 2020 at 10:23
  • 1
    @T.J.Crowder, Yes I only have a few months of programming and I prefer java or python but the biggest software we provide is still written in vb6 this is why I said I was new to VB6. we are converting everything to VB.NET :) thanks for your feedback I think Johns answer is working fine for me. Commented Oct 15, 2020 at 12:43
  • 4
    We always forget there is a huge base of VB6 code in production. It's not going away anytime soon, and someone has to maintain it. Commented Oct 15, 2020 at 14:02

1 Answer 1

5

Should be:

Public Function StringFormat(ByVal MyStr As String) As String
   
   Dim i As Integer
   Dim sBadChar As String
   Dim NewString As String
   
   ' List all illegal/unwanted characters
   sBadChar = "/<>?\{}[]()=,!#*:'*¬-"

   ' Loop through all the characters of the string

       For i = 1 To Len(MyStr)
           If InStr(sBadChar, Mid(MyStr, i, 1)) Then
               Mid(MyStr, i, 1) = ""
           End If
       Next i
       
       StringFormat = MyStr 
      
    End Function
Sign up to request clarification or add additional context in comments.

3 Comments

That was typed in a hurry. I should have added that the Return statement is only used to end a GoSub routine in VB6 and I can't recall ever using GoSub in all the years I've been using VB6! :^)
John is right, but to put it another way: whereas in VB.NET you assign the return value of a function with the Return keyword, in VB6 (and VBA) you assign the function's name the return value: <Function name> = <return value>, i.e. in this case StringFormat = MyStr
Having never used VB.Net I wouldn't know, but I'm not in the least bit surprised! Another way that MS broke the conversion between the two! :^))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.