Assign a value from select to a variable

Gr3g0ry

Registered User.
Local time
Yesterday, 22:12
Joined
Oct 12, 2017
Messages
163
so i have a simple select query which selects one value from a table. how do i then pass the one value to a variable ?
1642562708720.png

the query in question is highlighted.
 
RunSQL is for action statements - DELETE, INSERT, UPDATE, not for SELECT. Use SELECT to set recordset object.

Use domain aggregate function instead of opening a recordset.

x = DLookup("Amount", "inventory", "InventoryID ='" & Me.Inventory_ID & "'")

If InventoryID is a number field, don't use apostrophe delimiters.
 
As has already been said, better to use dlookup for a single value from a single table or query. But if you have a more complex requirement - multiple fields and/or multiple tables use a recordset e.g.

Code:
dim rs as DAO.Recordset

set rs=currentdb.openrecordset("SELECT * FROM inventory WHERE InventoryID =" & Me.Inventory_ID )
'assign to controls
me.txtInvName=rs!InvName
me.txtColour=rs!colour

set rs=nothing
 
RunSQL is for action statements - DELETE, INSERT, UPDATE, not for SELECT. Use SELECT to set recordset object.

Use domain aggregate function instead of opening a recordset.

x = DLookup("Amount", "inventory", "InventoryID ='" & Me.Inventory_ID & "'")

If InventoryID is a number field, don't use apostrophe delimiters.
thanks alot. worked like a charm. thanks for the pointer on the encapsulation and tutorial on how to use DLookup.
 
As has already been said, better to use dlookup for a single value from a single table or query. But if you have a more complex requirement - multiple fields and/or multiple tables use a recordset e.g.

Code:
dim rs as DAO.Recordset

set rs=currentdb.openrecordset("SELECT * FROM inventory WHERE InventoryID =" & Me.Inventory_ID )
'assign to controls
me.txtInvName=rs!InvName
me.txtColour=rs!colour

set rs=nothing
thanks
 
thanks alot. worked like a charm. thanks for the pointer on the encapsulation and tutorial on how to use DLookup.
Glad to hear you got it sorted out. Good luck with your project.
 

Users who are viewing this thread

Back
Top Bottom