简体   繁体   English

如何将 SQL 结果放入 VBA 中的变量中

[英]How can I put SQL result into variable in VBA

I have example table like below我有如下示例表

| ID | Qty |
| -- | ----|
| 1  | 5   |
| 2  | 7   |
| 3  | 8   |
| 4  | 9   |
| 5  | 12  |

How can I pass result of below query into VBA variable to use in next queries (update and insert)?如何将以下查询的结果传递给 VBA 变量以在下一个查询(更新和插入)中使用?

SELECT example_tab.Qty
FROM example_tab
WHERE ID = 4

In example that test_variable = 9例如 test_variable = 9

To get a single value into a variable, DLookup can be used:要将单个值放入变量中,可以使用DLookup

Dim test_variable As Variant

test_variable = DLookup("[Qty]", "example_tab", "[ID] = 4")

Is this what your looking for?这是你要找的吗?

sSQL = "SELECT example_tab.Qty FROM example_tab WHERE ID = 4"

Dim rs As DAO.Recordset     
Set rs = CurrentDB.OpenRecordset(sSQL)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM