Search code examples
vbams-accesscontrolsms-access-2010

MS Access: Using query result to control form object property


I’m making an MS Access (2010) Form for a SQL Server (2012) database. A query created in Access gives a resultset containing up to 5 unique numbers from 1 to 5 (with all combinations). So, for example, the query result looks like this:

ID
—
1
3
4

The query result is stored in a table. Now I want to use it to control the visibility of 5 control buttons placed on a form. That is, I want button no. 1 to be visible only if the result set contains the number 1. If number 2 is in the result, button no. 2 gets visible, and so on.

It’s not a problem to check if a number is in the resultset, but I have no idea how to use an SQL statement result as a control property value - can this be done somehow?

Button visibility should be set when the form opens. Any ideas?


Solution

  • Try something like:

    Me.button1.Visible = Not IsNull(DLookup("ID", "queryname", "ID=1"))

    or

    Me.button1.Visible = DCount("*", "queryname", "ID=1") > 0