1

I have two databases, a "Master" source (held locally) and a "Copy" database (to be distributed). Neither will be able to see each other once distributed (either locally or across a network), so we can't perform queries across the databases after distribution. I need to remove some content from the Copy database before it is distributed, so I decided to create a VBA script to produce the Copy databases for distribution.

There are lookups in the tables, so I have decided to hold a template database (which was copied from the master source, and then stripped the tables out of them), then am dropping the tables and have recreated them in an appropriate order.

I'm now needing to remove some of the data, and I'm struggling.

DeviceTable:
AutoNumber(ID)
Text(DeviceName)
Integer(ClusterID)
Text(Distribution)

ClusterTable:
AutoNumber(ID)
Text(ClusterName)

VirtualSystemTable:
AutoNumber(ID)
Text(VirtualSystemName)
Integer(ClusterID)
Integer(DeviceID)

InterfaceTable:
AutoNumber(ID)
Integer(VirtualSystemID)
Integer(ClusterID)
Integer(DeviceID)
Text(Description)

I need to remove entries from DeviceTable, ClusterTable, VirtualSystemTable and InterfaceTable for anything which is not marked as Distribution: "Public"

Normally I would do (in psudocode):

arrDEV = SQL("SELECT ID, ClusterID FROM DeviceTable WHERE Distribution<>"Public"")

Then, for each response, I would

arrVSYS = SQL("SELECT ID FROM VirtualSystemTable WHERE DeviceID=$arrDEV.ID OR ClusterID=$arrDEV.ClusterID")
SQL("DELETE FROM InterfaceTable WHERE DeviceID=$arrDEV.ID OR ClusterID=$arrDEV.ClusterID OR VirtualSystemID=$arrVSYS.ID")
SQL("DELETE FROM VirtualSystemTable WHERE DeviceID=$arrDEV.ID OR ClusterID=$arrDEV.ClusterID")
SQL("DELETE FROM ClusterTable WHERE ID=$arrDEV.ClusterID")

My issue is that I can't work out how to perform these queries across the database link. I amusing ADODB. I normally code in PHP, so this is a bit of a struggle for me!

4

1 に答える 1

1

あなたの考えは正しいですが、VBA は PHP とはかなり異なります。最初に文字列を終了し、変数を連結してから、文字列を再度開始することで、文字列内で変数を使用することはできません。

通常 arrDev または arrVSys と呼ばれる配列を使用する場所では、MS Access 内で DAO Recordset または ADO Recordset オブジェクトを使用します。

あなたが提供した情報を反映するように回答を更新しました:

Dim db As DAO.Database
Dim sSQL as String
Dim arrDEV As DAO.Recordset, Dim arrVSYS as DAO.Recordset
Set db = OpenDatabase("C:\SomeDatabase.accdb")
sSQL = "SELECT ID, ClusterID FROM DeviceTable WHERE Distribution <> 'Public'"
Set arrDEV = db.Open(sSQL)
If Not (arrDEV.EOF and arrDEV.BOF) Then
    arrDEV.movefirst
    Do Until arrDEV.eof = True
        sSQL = "SELECT ID FROM VirtualSystemTable WHERE DeviceID = " & arrDEV("ID") & _
                " OR ClusterID = " & arrDEV("ClusterID")
        Set arrVSYS = CurrentDb.Open(sSQl)

        sSQL = "DELETE FROM InterfaceTable WHERE DeviceID = " & arrDEV("ID") & _
                " OR ClusterID = " & arrDEV("ClusterID") & " OR VirtualSystemID = " & arrVSYS("ID")
        CurrentDb.Execute sSQl, dbFailOnError

        sSQL = "DELETE FROM VirtualSystemTable WHERE DeviceID = " & arrDEV("ID") & " OR ClusterID = " & arrDEV("ClusterID")
        CurrentDb.Execute sSQl, dbFailOnError

        sSQL = "DELETE FROM ClusterTable WHERE ID = " & arrDEV("ClusterID")
        CurrentDb.Execute sSQl, dbFailOnError

        arrVSYS.Close
        arrDEV.MoveNext
    Loop
End If
'Cleanup
Set arrVSYS = Nothing
arrDEV.Close
Set arrDEV = Nothing
db.close
Set db = Nothing

コードはテストされていないため、エラーが発生する可能性があります。私が計画していなかった唯一のことは、arrVSYS に複数のレコードを持つことでした。そのレコードセットに複数のレコードがある場合は、さらに別のループが必要になります。

ご覧のとおり、ADO を使用して別の Access データベースにアクセスする必要はありません。ただし、「外部」データベースが Access 以外の場合は、ADO を使用する必要があります。

于 2013-08-30T15:49:39.137 に答える