0

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)?

SELECT example_tab.Qty
FROM example_tab
WHERE ID = 4

In example that test_variable = 9

2
  • Does this answer your question? How to use a SQL SELECT statement with Access VBA Commented Mar 1, 2021 at 9:36
  • Is your VBA being hosted in Access, or in some other application? Also, do you mean to take the result of the query and use it in another statement? Some more context would be helpful. Commented Mar 1, 2021 at 9:41

2 Answers 2

1

To get a single value into a variable, DLookup can be used:

Dim test_variable As Variant

test_variable = DLookup("[Qty]", "example_tab", "[ID] = 4")
Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.