This works in my sample database - but *very* slowly. It requires
further slight change to the VBA function; see below.
SELECT B.Field6, A.ID, A.Postcode,
NumMatchingCharacters(A.PostCode, B.Field6) As MatchLength
FROM AddrNew AS A INNER JOIN Asterisk AS B
ON Left(B.Field6, 1) = Left(A.Postcode, 1)
WHERE NumMatchingCharacters(A.PostCode, B.Field6) = (
SELECT MAX(NumMatchingCharacters(C.Postcode, B.Field6))
FROM AddrNew AS C
WHERE NumMatchingCharacters(C.PostCode, B.Field6) > 0
);
[AddrNew] is the main table, and [Postcode] corresponds to your [HIER]
field. [Asterisk] is the table that contains the values you want to find
matches for, in [Field6]. The [MatchLength] calculated field is only
there to show what's going on.
The INNER JOIN is an attempt to speed things up by excluding pairs of
records that can't possibly match; I'm not sure that it's actually any
faster than using a Cartesian join
FROM AddrNew AS A, Asterisk AS B
WHERE B.Field6 IS NOT NULL
AND NumMatchingCharacters(...
Maybe a real SQL expert will see this and show us a better way.
One thing I'm not sure about. Here's the output from the query in my
sample database:
Field6 ID Postcode MatchLength
NICKJHT 1 NICKJHT 7
ROSEBER 18 ROSEBER40 7
JOHNSXRES 25 JOHNSXTERES 6
JOTRESG 25 JOHNSXTERES 2
Note that two values in Field6 have matched against the same record in
the main table. Is that what you want to happen? If not, what?
Here's the revised NumMatchingCharacters() function: it now handles Null
values better.
Function NumMatchingCharacters( _
ByVal SearchString As Variant, _
ByVal MatchString As Variant) As Long
If IsNull(SearchString) Or IsNull(MatchString) Then
NumMatchingCharacters = 0
Exit Function
End If
If Len(MatchString) > Len(SearchString) Then
MatchString = Left(MatchString, Len(SearchString))
End If
Do While Len(MatchString) > 0
If InStr(SearchString, MatchString) = 1 Then
Exit Do
Else
MatchString = Left(MatchString, Len(MatchString) - 1)
End If
Loop
NumMatchingCharacters = Nz(Len(MatchString), 0)
End Function
John almost there. I do have a question about how it's set up below though.
Match is actually going to be another field from a table, that we are trying
to match so no need for parameters. I understand the MAX number but I need
another field to show what we were trying to match. So I am assuming I need
to do a left join first to show all the values of HIER. and then see if the
values in the Match field are there? Not sure how to write it out.
PARAMETERS [Match] TEXT (10);
SELECT HIER, Match
FROM MyTable
WHERE NumMatchingCharacters(HIER, Match) = (
SELECT MAX(NumMatchingCharacters(HIER, Match))
FROM MyTable
WHERE NumMatchingCharacters(HIER, Match) > 0
:
In that case just change
If InStr(SearchString, MatchString) > 0 Then
in the function to
If InStr(SearchString, MatchString) = 1 Then
John thank you for your time, however the partial match should be at the
beginning of the match. Please see my other posts and replies to John
Spencer. He knows exactly what I'm looking for.
:
Hi Alex,
It would be helpful if you explained what was wrong with the three
suggestions you got in microsoft.public.access.modulescoding on 8
February. Anyway, here's another, which assumes that the partial match
can occur anywhere in the field rather than just at the beginning. It
needs a VBA function that returns the number of characters in the
"match" string (e.g. NICKJM) that can be matched in a target string:
Function NumMatchingCharacters( _
ByVal SearchString As Variant, _
ByVal MatchString As String) As Long
If IsNull(SearchString) Then
NumMatchingCharacters = 0
Exit Function
End If
If Len(MatchString) > Len(SearchString) Then
MatchString = Left(MatchString, Len(SearchString))
End If
Do While Len(MatchString) > 0
If InStr(SearchString, MatchString) > 0 Then
Exit Do
Else
MatchString = Left(MatchString, Len(MatchString) - 1)
End If
Loop
NumMatchingCharacters = Len(MatchString)
End Function
Then you can use this in a rather slow query and subquery, e.g.:
PARAMETERS [Match] TEXT (10);
SELECT HIER, Match
FROM MyTable
WHERE NumMatchingCharacters(HIER, Match) = (
SELECT MAX(NumMatchingCharacters(HIER, Match))
FROM MyTable
WHERE NumMatchingCharacters(HIER, Match) > 0
)
;
On Wed, 28 Feb 2007 06:43:44 -0800, Alex
I'm not sure if this can be done in a query by creating a function first.
Here's an example: I need to find NICKJM in the "HIER" field. If there is
none, then it would look for NICKJ, and if there is no value it would look
for NICK. It will do this until it has matched. Now the value NICKJM is an
example and it can be anything; MMHGA, LKEDK. Once the value is found in
Hier it will place the need to find field in the new field. Is this poosible
to do in a query?