Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string parsing

Status
Not open for further replies.

GROKING

Programmer
Mar 8, 2005
26
US
I have a table in access with strings like this in a text column.
I want to create a query that trims off the "and everything to the right of ".com" leaving just "model.com". I know I need to use instr and mid to do this but not sure how to do it all in so I can append this to a new blank column.


Any ideas would be very helpful??

Thanks
 
create the following VBA function:
Code:
public function ParseURL(byval sURL as variant) as string
  Dim lStart as long
  Dim lEnd as long
  
  lStart=instr(sURL & "","[URL unfurl="true"]www.",vbtext)[/URL]
  lEnd = instr(sURL & "",".com",vbtext)
  ParseURL = mid(sURL & "",lStart+4,lEnd-lStart+1)
end sub
 
Correction:
Code:
public function ParseURL(byval sURL as variant) as string
  Dim lStart as long
  Dim lEnd as long
  
  lStart = instr(1,sURL,"[URL unfurl="true"]www.",vbTextCompare)[/URL]
  lEnd = instr(1,sURL,".com",vbTextCompare)
  ParseURL = mid(sURL & "",lStart+4,lEnd-lStart+1)
end sub

You may want to add some special case logic if either lStart or lEnd is 0 meaning that it did not contain a a com.
 
Public Function GetTheRest(strURL As String) As String

Select Case True
Case Left(strURL, 7)= " strURL = Right(strURL, Len(strURL) - 7)
Case Left(strURL, 6)= "ftp://"
strURL = Right(strURL, Len(strURL) - 6)
End Select
If Left(strURL, 4)= " Then
GetTheRest = Right(strURL, Len(strURL) - 4)
Else
GetTheRest = strURL
End If
End Function
 
Jerry,

I like your function. The only suggestion I would make is to make strURL a byval variant because this function is going to be used within an sql query. You wouldn't want it to bomb if the field was null.

- Dan

Public Function GetTheRest(byval strURL As variant) As String

strURL = trim(strURL & "")

Select Case True
Case Left(strURL, 7)= " strURL = Right(strURL, Len(strURL) - 7)
Case Left(strURL, 6)= "ftp://"
strURL = Right(strURL, Len(strURL) - 6)
End Select
If Left(strURL, 4)= " Then
GetTheRest = Right(strURL, Len(strURL) - 4)
Else
GetTheRest = strURL
End If
End Function
 

Your mentioning of *null* brings up errors in Select Case
part. We should add at the very top

If Len(strURL & "") < 8 Then
GetTheRest = ""
Exit Function
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top