 |
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

February 18th, 2008, 02:22 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

February 18th, 2008, 02:31 PM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
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
================================================== =========
|
|

February 18th, 2008, 02:45 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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,
|
|

February 19th, 2008, 02:44 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
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
|
|

April 29th, 2008, 08:32 AM
|
|
Friend of Wrox
|
|
Join Date: Dec 2004
Posts: 221
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

April 29th, 2008, 02:08 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
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...
|
|

April 30th, 2008, 12:11 AM
|
|
Friend of Wrox
|
|
Join Date: Dec 2004
Posts: 221
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

April 30th, 2008, 10:58 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
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.
|
|

April 30th, 2008, 01:16 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

May 1st, 2008, 12:47 AM
|
|
Friend of Wrox
|
|
Join Date: Dec 2004
Posts: 221
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |