我收到了英国邮政编码列表,其格式为L15TG或TS14TGU。
我需要能够将这些邮政编码与我存储在数据库中的邮政编码列表相匹配,但是,我的列表只是英国的外码,因此L15TG只是L1而TS14TGU只是TS14。
所以我需要匹配记录并返回匹配的字符串部分,即从L15TG获取L1。
答案 0 :(得分:0)
好的,我们有一个名为[BadCodes]
的表BadCode
-------
L15TG
TS14TGU
和一个名为[OutCodes]
的表OutCode
-------
L1
TS14
我会在[BadCodes]表中添加两个Text
字段:[OuterCode]和[InnerCode]
然后我将使用以下VBA代码填充[BadCodes]。[OuterCode]
Option Compare Database
Option Explicit
Public Sub PopulateOuterCodes()
Dim cdb As DAO.Database, qdf As DAO.QueryDef, rstOutCodes As DAO.Recordset
Set cdb = CurrentDb
Set qdf = cdb.CreateQueryDef("", _
"PARAMETERS prmOuterCode TEXT(255), prmLike TEXT(255);" & _
"UPDATE BadCodes SET OuterCode=[prmOuterCode] " & _
"WHERE BadCode LIKE [prmLike] AND OuterCode IS NULL")
Set rstOutCodes = cdb.OpenRecordset( _
"SELECT OutCode FROM OutCodes " & _
"ORDER BY Len(OutCode) DESC, OutCode", _
dbOpenSnapshot)
Do Until rstOutCodes.EOF
qdf!prmOuterCode = rstOutCodes!OutCode
qdf!prmLike = rstOutCodes!OutCode & "*"
qdf.Execute
rstOutCodes.MoveNext
Loop
rstOutCodes.Close
Set rstOutCodes = Nothing
Set qdf = Nothing
Set cdb = Nothing
End Sub
运行该VBA代码后,我的[BadCodes]表看起来像
BadCode OuterCode InnerCode
------- --------- ---------
L15TG L1
TS14TGU TS14
然后我可以使用查询
填充[BadCodes]。[InnerCode]UPDATE BadCodes
SET InnerCode = Replace(BadCode,OuterCode,"",1,1)
我的[BadCodes]表现在看起来像
BadCode OuterCode InnerCode
------- --------- ---------
L15TG L1 5TG
TS14TGU TS14 TGU