【问题标题】:How to replace the characters in one table to the records of another table如何将一张表中的字符替换为另一张表的记录
【发布时间】:2013-11-13 04:46:46
【问题描述】:

我在 MSACCESS 中有两个表。

表1

table1ID,txtOld,txtreplace,country

1,ä,AA,德国

2,ä,a,全部

3,Ä,A,挪威

4,Ä,a,全部

5,Ä,A,德国

表2

table2ID、名字、地址、国家

1,RonÄld, mäin street,Germany

2,MÄdonä,公园路,美国

3,Madonä,公园路,挪威

我想根据 table1 上分配的国家/地区字符替换 table2 上名字和地址上的特殊字符。如果国家不匹配或未在 table1 上分配,例如此处未分配 USA,它将替换 table1 中与国家名称“all”匹配的字符。

【问题讨论】:

    标签: sql ms-access vba ms-access-2010


    【解决方案1】:

    很遗憾,我们无法在单个 Access SQL 查询中执行此操作,因此我们使用 VBA:

    Sub ReplaceAccentsInTable2()
    '
      Dim rst As dao.Recordset
      Dim rst1 As dao.Recordset
    '
      Set rst = CurrentDb.OpenRecordset("table2")
    '
    ' replace first by country specific alphabet:
    '
      Do While (Not rst.EOF)
        '
        Set rst1 = CurrentDb.OpenRecordset("SELECT txtOld, txtreplace" _
          & " FROM table1 WHERE (country='" & rst!Country & "');")
        '
        rst.Edit
        '
        While (Not rst1.EOF)
          rst!Firstname = Replace(rst!Firstname, rst1!txtOld, rst1!txtreplace, , , vbBinaryCompare)
          rst!Address = Replace(rst!Address, rst1!txtOld, rst1!txtreplace, , , vbBinaryCompare)
          rst1.MoveNext
        Wend
        '
        rst.Update
        rst.MoveNext
        '
      Loop
    '
    ' replace then "all" alphabet: this is useful for Norway, as â is not set in table1:
    '
      Set rst1 = CurrentDb.OpenRecordset("SELECT txtOld, txtreplace" _
        & " FROM table1 WHERE (country='all');")
    '
      rst.MoveFirst
    '
      Do While (Not rst.EOF)
        '
        rst.Edit
        rst1.MoveFirst
        '
        While (Not rst1.EOF)
          rst!Firstname = Replace(rst!Firstname, rst1!txtOld, rst1!txtreplace, , , vbBinaryCompare)
          rst!Address = Replace(rst!Address, rst1!txtOld, rst1!txtreplace, , , vbBinaryCompare)
          rst1.MoveNext
        Wend
        '
        rst.Update
        rst.MoveNext
        '
      Loop
    
    '
    ' close ADO objects:
    '
      rst1.Close
      Set rst1 = Nothing
    '
      rst.Close
      Set rst = Nothing
    '
    End Sub
    

    这里的结果表2:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-22
      • 2012-01-07
      • 1970-01-01
      相关资源
      最近更新 更多