Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Short VB code error

  • 22-02-2005 03:08PM
    #1
    Moderators, Music Moderators, Recreation & Hobbies Moderators Posts: 9,391 Mod ✭✭✭✭


    Can anyone notice anything wrong with below
    thanks

    Option Explicit
    Private Sub Form_Load()
    Dim string1 As String
    Dim string2 As String
    Dim msg As String
    Dim position As Integer

    string1 = "smithtwitdumfreemantwitdumrothtwitdum"
    position = InStr(string1, "twitdum")
    string2 = Mid(string1, "twitdum")
    msg = "the first occurrence of " & "`" & string2 & "`"
    msg = msg & " is at position " & position
    msg = msg & vbCrLf & vbCrLf

    position = InStr(position + 1, string1, "twitdum")
    string2 = Mid(string1, position, 7)
    msg = msg & "the second occurrence of" & _
    "`" & string2 & "`"
    msg = msg & " is at postion " & position
    msg = msg & vbCrLf & vbCrLf

    position = InStr(position + 1, string1, "twitdum")
    msg = msg & " the third occurrence of " & _
    "`" & string2 & "`"
    msg = msg & " is at postion " & position
    MsgBox msg
    End Sub


Comments

  • Closed Accounts Posts: 2,639 ✭✭✭Laguna


    What's the error number/message you get during compile time?


  • Registered Users, Registered Users 2 Posts: 450 ✭✭krinpit


    Perhaps your problem is
    string2 = Mid(string1, "twitdum")
    
    Isn't the second parameter to Mid supposed to be an Integer?


  • Registered Users, Registered Users 2 Posts: 21,277 ✭✭✭✭Eoin


    Off the top of my head, it looks like you have the "mid" bit wrong. If the usage is the same as in ASP, which I would think it is, it expects the following syntax:
    string2 = mid(string1, nStart, nEnd)
    
    where nStart is the integer you want it to start at, and nEnd is the last position.

    You have passed in two strings instead of one string and two integers
    string2 = Mid(string1, "twitdum")

    Try this:
    string2 = mid(string1, instr(string1, "twitdum"), len("twitdum"))
    

    Eoin


Advertisement