Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / January 2006

Tip: Looking for answers? Try searching our database.

How do I use pattern match in the instr function?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tom Connors - 26 Jan 2006 19:21 GMT
Using Access2003.  I have data sets with a 6 digit string embedded as such:
ACME INC 290452 447-DOE, JANE
WIDGETS CO & SONS 262378 897-SMITH, JOHN
etc etc
I want o extract everything up to the 6 digit portion (eg I want the "ACME
INC" portion).  I thought I could use instr function with a pattern match of
###### to find the position of the 6 digits.  I would then use the left
operator to trim up to that position.
Can I use pattern matching within instr ?  This always returns 0:
test: InStr(1,[Vendor and Description],"######")

Any help would be appreciated.
Thanks
TC


GeoBrooks - 26 Jan 2006 20:00 GMT
TC,

I think brute force would be more successful.  You'd have to build a
function to inspect each character in the string.  Start with the leftmost
and discard it and test again if not numeric.  In case there's a customer
name with a number in it, you should probably make sure that if the character
is numeric it is followed by five more and a space.

Use things like:
strIn = "WIDGETS CO & SONS 262378 897-SMITH, JOHN "
strTest = left(strIn,1)
IsNumeric(strTest)
if not, strIn = right(strIn,len(strIn)-1) and loop

Good luck.

George
[someone who wouldn't know code elegance if bitten by it!]

> Using Access2003.  I have data sets with a 6 digit string embedded as such:
> ACME INC 290452 447-DOE, JANE
[quoted text clipped - 12 lines]
>
>  
John Nurick - 26 Jan 2006 21:08 GMT
Hi Tom,

InStr() doesn't do pattern matching, only literal matches, and Like
doesn't tell you where it finds a match.

I've built a function that uses the VBScript regular expression object
for this sort of parsing; you'll find it at
http://www.j.nurick.dial.pipex.com/Code/vbRegex/rgxExtract.htm

A pattern like this should do the job (everything up to a 6-digit
string:

    (^.*)\s\d{6}

>Using Access2003.  I have data sets with a 6 digit string embedded as such:
>ACME INC 290452 447-DOE, JANE
[quoted text clipped - 10 lines]
>Thanks
>TC

--
John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.
Marshall Barton - 26 Jan 2006 21:33 GMT
>Using Access2003.  I have data sets with a 6 digit string embedded as such:
>ACME INC 290452 447-DOE, JANE
[quoted text clipped - 6 lines]
>Can I use pattern matching within instr ?  This always returns 0:
>test: InStr(1,[Vendor and Description],"######")

No.  Like is the only built-in operator or function that
uses wildcards.

You can easily write your own function that does what you
want though.  Try something along the lines of this untested
air code:

Public Function GetCode(StrCode As String,
                                                Pat As String
                                              ) As String
Dim k As Long

For k = 1 To Len(StrCode) - Len(Pat)
    If Mid(StrCode, k) Like Pat Then
        GetCode = Mid(StrCode, k, Len(Pat))
    End If
Next k
End Function

Signature

Marsh
MVP [MS Access]

Marshall Barton - 27 Jan 2006 00:25 GMT
>Using Access2003.  I have data sets with a 6 digit string embedded as such:
>ACME INC 290452 447-DOE, JANE
[quoted text clipped - 6 lines]
>Can I use pattern matching within instr ?  This always returns 0:
>test: InStr(1,[Vendor and Description],"######")

Well, that untested function certainly won't work :-(

Here's another (quick test) version that mimics InStr much
more closely:

Public Function InStrPat(Start As Variant, _
                             String1 As Variant, _
                             Optional String2 As Variant _
                          ) As Variant
Dim lngStart As Long
Dim strText As String
Dim strPat As String
Dim Lg As Long, K As Long

  InStrPat = Null
  If IsMissing(String2) Then
     If IsNull(Start) Or IsNull(String1) Then Exit Function
     lngStart = 1
     strText = Start
     strPat = String1
  Else
     If IsNull(Start) Or IsNull(String1) Or IsNull(String2)
Then Exit Function
     lngStart = Start
     strText = String1
     strPat = String2
  End If
 
  Lg = Len(strPat)
  InStrPat = 0
  For K = lngStart To Len(strText) - Lg
     If Mid(strText, K, Lg) Like strPat Then
        InStrPat = K
     End If
  Next K
End Function

Signature

Marsh
MVP [MS Access]

Tom Connors - 27 Jan 2006 21:41 GMT
Thanks all !

> >Using Access2003.  I have data sets with a 6 digit string embedded as such:
> >ACME INC 290452 447-DOE, JANE
[quoted text clipped - 43 lines]
>    Next K
> End Function
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.