Access 2010的VBA查询表,并遍历结果遍历、结果、Access、VBA

2023-09-08 10:58:59 作者:剩余酸涩

我有,我想对一个表执行查询。有了结果我想要做的事。在我的头伪code是:

I have a query that I want to execute against a table. With the results I want to do something. In my head the pseudo code is:

var q = "select * from table where some condition";
var results = db.getResults(q);
foreach (row r in results )
    do something with result

我将如何让使用VBA类似的东西?

How would I so something similar with vba?

推荐答案

DAO是本机访问和迄今为止最好的一般使用。 ADO有它的地方,但它是不可能的,这是它。

DAO is native to Access and by far the best for general use. ADO has its place, but it is unlikely that this is it.

 Dim rs As DAO.Recordset
 Dim db As Database
 Dim strSQL as String

 Set db=CurrentDB

 strSQL = "select * from table where some condition"

 Set rs = db.OpenRecordset(strSQL)

 Do While Not rs.EOF

    rs.Edit
    rs!SomeField = "Abc"
    rs!OtherField = 2
    rs!ADate = Date()
    rs.Update

    rs.MoveNext
Loop