0

MS Access には 2 つのテーブル (A と B) があり、タスクは B を A に挿入することです。ただし、いくつかの特別な条件があります。

  • すべてのフィールドはテキスト型です。
  • A と B にはいくつかの共通フィールドがあります。
  • 同じキー フィールドが両方に存在することが保証され、その値は常に異なります。
  • A には、B にはないいくつかのフィールドがあります。挿入されたレコードでは、これらのフィールドが空白になっている必要があります。
  • B には、A にないフィールドがいくつかあります。これらのフィールドは A で作成する必要があり、A の既存のレコードでは空白にする必要があります。
  • このようなケースは数多くあるため、クエリにフィールド名を明示的に含めるべきではありません。ケースごとにクエリをパーソナライズするのは面倒なのでです。ただし、キー フィールドには常に同じ名前が付けられます。
  • A を直接置き換える代わりに、新しいテーブル C を作成することは許容されます。

例:

Table A:
key  a       b       c
--- ------- ------- -------
k0  hello   dear    world
k1  bye     cruel   world

Table B:
key  a       d       e
--- ------- ------- -------
k2  welcome john    doe
k3  turulu  ann     harp

Table C (the new A):
key  a       b       c       d       e
--- ------- ------- ------- ------- -------
k0  hello   dear    world
k1  bye     cruel   world
k2  welcome                 john    doe
k3  turulu                  ann     harp
4

2 に答える 2

1

これを解決する最も簡単な方法は、VBA を使用してクエリ定義を作成することです。

key両方のテーブルに共通する名前の列があると仮定します。

ここで、コレクションを使用して辞書のような構造を作成できることがわかりました。これを使用して、フィールド リストを作成します。

それで、ここに行きます:

public function contains(col as Collection, key as variant) as boolean
    dim obj as variant
    on error goto err
        contains = True
        obj = col(key)
        exit function
    err:
        contains = false
end function

public sub create_this_query(tbl1 as String, tbl2 as String, keyField as String)
    ' tbl1 and tbl2 are the names of the tables you'll use
    dim db as DAO.database, rs1 as DAO.recordset, rs2 as DAO.recordset
    dim columns as Collection
    dim strSQL as String
    dim i as integer
    dim obj as variant, colName as String

    set db = currentdb()
    set tbl1 = db.openrecordset(tbl1, dbopendynaset, dbreadonly)
    set tbl2 = db.openrecordset(tbl2, dbopendynaset, dbreadonly)
    set columns = new Collection

    ' Let's create the field list (ommiting the keyField)
    for i = 1 to tbl1.fields.count
        if not contains(columns, tbl1.fields(i).Name) _
           and tbl1.fields(i).Name <> keyField then
            columns.add tbl1.fields(i).Name, tbl1.fields(i).Name
        end if
    next i
    for i = 1 to tbl2.fields.count
        if not contains(columns, tbl2.fields(i).Name) _
           and tbl2.fields(i).Name <> keyField then
            columns.add tbl1.fields(i).Name, 1 ' The value is just a placeholder
        end if
    next i
    ' Now let's build the SQL instruction
    strSQL = "select [a].[" & keyField & "]"
    for colName in columns
        strSQL = strSQL & ", [" & colName & "]"
    next obj
    strSQL = strSQL & " " & _
             "from " & _
             "    (" & _
             "        select [" & keyField & "] from [" & tbl1 & "] " & _
             "        union " & _
             "        select [" & keyField & "] from [" & tbl2 & "] " & _
             "    ) as a " & _
             "left join [" & tbl1 & "] as t1 " & _
             "    on a.[" & keyField & "] = t1.[" & keyField & "] " & _
             "left join [" & tbl2 & "] as t2 " & _
             "    on a.[" & keyField & "] = t2.[" & keyField & "] "
   ' Finally, let's create the query object
   db.createQueryDef("myNewQuery", strSQL)
end sub

お役に立てれば

于 2013-09-13T22:11:31.570 に答える