Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 18th, 2008, 02:22 PM
Friend of Wrox
Points: 1,035, Level: 12
Points: 1,035, Level: 12 Points: 1,035, Level: 12 Points: 1,035, Level: 12
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jmss66
Default Parsing of String

How do I get just a part of a string?

For example, I have a field called Travel_ID and it has the following data:

21-1
21-3
21-12

I want to be able to get the numbers after the "-" to store in another table.

Thanks

Reply With Quote
  #2 (permalink)  
Old February 18th, 2008, 02:31 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Something like this:

Dim oldValue as String = "21-1"
Dim newValue as String = oldValue.SubString(oldValue.IndexOf("-"), (oldValue.Lenth - oldValue.IndexOf("-")) - 1)

hth

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
Reply With Quote
  #3 (permalink)  
Old February 18th, 2008, 02:45 PM
Friend of Wrox
Points: 1,035, Level: 12
Points: 1,035, Level: 12 Points: 1,035, Level: 12 Points: 1,035, Level: 12
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jmss66
Default

I did it the long way.

            Dim intPosition As Integer
            Dim intLength As Integer
            Dim strID As String

            Do While Not rsTravel_Detail.EOF

               intPosition = InStr(rsTravel_Detail!Travel_ID, "-")
               intLength = Len(rsTravel_Detail!Travel_ID)
               strID = Right(rsTravel_Detail!Travel_ID, intLength - intPosition)
               rstravel_Detail_Temp.AddNew
               rstravel_Detail_Temp!Travel_ID = strID
               rstravel_Detail_Temp.Update
               rsTravel_Detail.MoveNext
            Loop

I will try doing it the way you posted.

Thanks,


Reply With Quote
  #4 (permalink)  
Old February 19th, 2008, 02:44 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

If you use mid with only 2 arguments, it starts at the position indicated as the Start, and gets the remainder of the string:
Code:
    Dim intPos As Integer
    Dim strID  As String

        With rsTravel_Detail
            Do While Not .EOF

                intPos = InStr(!Travel_ID, "-")
                strID = Mid(!Travel_ID, intPos + 1)
                .AddNew
                !Travel_ID = strID
                .Update
                .MoveNext
            Loop
        End With
        or
Code:
    Dim intPos As Integer
Code:
        With rsTravel_Detail
            Do While Not .EOF
                intPos = InStr(!Travel_ID, "-")
                .AddNew
                !Travel_ID = Mid(!Travel_ID, intPos + 1)
                .Update
                .MoveNext
            Loop
        End With
Reply With Quote
  #5 (permalink)  
Old April 29th, 2008, 08:32 AM
Friend of Wrox
Points: 687, Level: 9
Points: 687, Level: 9 Points: 687, Level: 9 Points: 687, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2004
Location: Chennai, Tamilnadu, India.
Posts: 221
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It will be better if you create a function and use it like this...

Private Function GetID(ID As String) As Long
Dim a
a = InStr(1, ID, "-", vbBinaryCompare)
If a = 0 Then Exit Function
b = Mid(ID, (a + 1), Len(ID) - a)
GetID = CLng(b)
End Function

and assign the functions output to that field.

If the database is SQL then you can create updatable cursors and work on such logic at the DB level itself.

Hope this helps.

With Regards,
Raghavendra Mudugal
Reply With Quote
  #6 (permalink)  
Old April 29th, 2008, 02:08 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

In InStr(), when starting with the 1st character, the "1," is unnec.
If you had read what I posted on Feb 19, you would have read that the MID$() function does not require the 3rd argument when you want the rest of the string following the 2nd argument's postion.

Mid(ID, (a + 1), Len(ID) - a)

does exactly the same thing as

Mid(ID, (a + 1))

Dim a without specifying a type, creates a Variant, which is not as good of an idea as specifying an explicit type.
You didn't even Dim b, a bad practice.
Wrapping your solution in a Function clouds answering the actual question that jmss66 asked.

Since InStr() returns a long, you should have:
Code:
Private Function GetID(ID As String) As Long

    Dim a As Long

    a = InStr(ID, "-", vbBinaryCompare)
    If a = 0 Then Exit Function

    GetID = CLng(Mid(ID, (a + 1)))

End Function
But, aside from the use of a Function as a wrapper, that's actually the solution that I provided...
Reply With Quote
  #7 (permalink)  
Old April 30th, 2008, 12:11 AM
Friend of Wrox
Points: 687, Level: 9
Points: 687, Level: 9 Points: 687, Level: 9 Points: 687, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2004
Location: Chennai, Tamilnadu, India.
Posts: 221
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Setting the MID function to pick number of chars is always better than Letting the MID function to choose. I never use the second way.

Usage of "As Long", actually I missed it. I saw after posting.

The solution what you provided is good if you use the updatable cursors in the SP and imply such logic. But from the VB point of view ... opening a recordset, setting value to a variable, setting the value of the field and updating it, using this complete sequence in a loop for all the records to what the sql statement of the recordset is opened, is not feasible. It seems okay for the single user lever but not to used in more than one user scenario. I believe.

Hope this helps.

With Regards,
Raghavendra Mudugal
Reply With Quote
  #8 (permalink)  
Old April 30th, 2008, 10:58 AM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Quote:
quote:Originally posted by Raghavendra_Mudugal
 Setting the MID function to pick number of chars is always better than Letting the MID function to choose. . .
 In what way? Can you explain what would make letting the function choose inferior, or to be avoided?

Apparently, from the original post, the result is going to be used to cahnge the value of a recordset field. So using a function, or using the string-finding code directly won't change the sorts of things you are contemplating regarding the SP and the interaction with the data. The user, if he takes your suggestion, will merely be putting your function in place of the couple of statements I proposed. The result will be identical in that kind of a case.
Reply With Quote
  #9 (permalink)  
Old April 30th, 2008, 01:16 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Alameda, ca, USA.
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can use also the Split function:

dim secondPart as string
secondPart = Split("21-1", "-")(1)

Split returns an array dividing the string of the first parameter using the separator specified in the second parameter. So to get the second part you have to specify the (1) second element of the array

"There are two ways to write error-free programs. Only the third one works."
Unknown
Reply With Quote
  #10 (permalink)  
Old May 1st, 2008, 12:47 AM
Friend of Wrox
Points: 687, Level: 9
Points: 687, Level: 9 Points: 687, Level: 9 Points: 687, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2004
Location: Chennai, Tamilnadu, India.
Posts: 221
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by BrianWren
 In what way? Can you explain what would make letting the function choose inferior, or to be avoided?

It's as simple as this... If you do not specify the value, the mid function will attempt to find how many chars to be taken, and then, it will eliminate all other chars and pick only those as much as needed. If you specify the value then it's just picks up no.of position and the respective chars straight away. It will reduce one extra simple work on the mid function.

Quote:
quote:Originally posted by BrianWren
 Apparently, from the original post, the result is going to be used to cahnge the value of a recordset field. So using a function, or using the string-finding code directly won't change the sorts of things you are contemplating regarding the SP and the interaction with the data. The user, if he takes your suggestion, will merely be putting your function in place of the couple of statements I proposed. The result will be identical in that kind of a case.
Well... its not about my suggestion or your suggestion, its all about doing better than what we have now. As suggested by marcostraf, using a split... that's also a good suggestion. May be some other poster will come and will improve what I or U said in here... am just attempting to make it better still better, not the best but not the worst either. I never look on the identical results, as my interest goes on how it is written.

Anyways. Hope this helps

With Regards,
Raghavendra Mudugal
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing from a text string jroxit Classic ASP Basics 5 November 18th, 2008 05:08 PM
Parsing a string problem poyserr Access 4 February 28th, 2007 08:19 AM
Parsing a String? MBowen SQL Server 2000 9 December 1st, 2006 01:17 PM
Parsing a text string medix_911 Access 8 November 21st, 2006 01:37 PM
SQL String Parsing TdyIndy SQL Language 2 July 26th, 2004 10:47 PM



All times are GMT -4. The time now is 09:53 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.