Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts

    Splitting strings in VBscript?

    Hello,

    I'm having a bit of bother with the few lines of VBscript below - I usually do ASP in Jscript and I'm not sure what's up with it?
    Code:
    splitDBF=Split(Cstr(rs.Fields("database_field").value), "+")
    dbTable=splitDBF(0)
    dbColumn=splitDBF(1)
    This is in a chunk of code that loops through a recordset: the 'rs.Fields("database_field").value' bit definitely supplies a string that looks (for eg.) like "employees+first_name".

    I get an error on the 'dbColumn=splitDBF(1)' line, saying:
    Subscript out of range: '[number: 1]'
    What am I doing wrong?

    Cheers.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Cincinnati, OH
    Posts
    545
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try doing a

    Response.Write(UBound(splitDBF))
    Response.Write(Ubound(dbTable))
    Response.Write(Ubound(dbColumn))

    Before each split to see how many elements are being put into the array.

    It looks like on dbTable you are splitting by " ", then on dbColumn you are again splitting using " ". Do you want to split by the same delimiter twice?

  • #3
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    Pardon? Thanks for your help, I think I may have got myself a little lost somewhere.

    I've got a string that looks like "tableName+columnName" coming out of a database recordset.

    All I want to do is split it on the "+".

    So I've set the variable "splitDBF" to be an Array - which is returned from running the Split() function on that recordset value, and which holds what I assume are only 2 elements:

    dbTable="tableName" [the first element in the zero-based array "splitDBF"]
    dbColumn="columnName" [the second element in the array]

    Is that not what the bit of code I posted does? Am I splitting something twice?

    Cheers again...

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,050
    Thanks
    0
    Thanked 251 Times in 247 Posts
    there may be some fields which doesnt have + sign. try to verify by running this code:

    field = Cstr(rs.Fields("database_field").value)
    response.write "Field: " & field & "<br>"
    splitDBF=Split(field, "+")
    response.write "Ubound: " & ubound(splitDBF) & "<br>"
    for i = 0 to ubound(splitDBF)
    response.write i & ": " & splitDBF(i) & "<br>"
    next

  • #5
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    Hmmm - this gets weirder.

    There are indeed some records that don't contain the "+" sign. I THOUGHT I was catching that, but it turns out I'm not - and that's probably why it falls over when it gets to one.

    So - this InStr() function. Does it actually work? If it doesn't find a match, it returns "0", right? Could anyone explain why the following:
    Code:
    Do While Not rs.EOF
    		
    	mydbString=CStr(rs.Fields("database_field").value)
    					
    	Response.Write mydbString & " - "
    	Response.Write InStr("employees", mydbString)
    	Response.Write "<br>"
    
    	rs.MoveNext
    Loop
    results in my browser displaying:

    employees+name_first - 0
    employees+name_last - 0
    addresses+email - 0
    -- - 0

    ??

    Cheers....

  • #6
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    AAAAAAAAAAAHHHHHHHHHHHH!!!

    I've just realised.

    Sorry everyone.

    *Runs and hides sheepishly under his desk before swapping the argumments round for InStr()*


    DOH.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •