How Do I have a variable in DCount?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a table called tb1. There is a field called Per_Num in a Query which
is based on the table tb1.

I have a second Query which generates values for the field called Per_Num.

How do I write a DCount function for a variable??

For example:

DCount("[Period]","Query","[Per_Num] = '01'")

This works for a set value '01'. How do I do this with a variable called z
i.e.

DCount("[Period]", "Query", "[Per_Num] = z") This doesn't work,

This is where I need help.


Thank You,

Gary
 
If this is in code you concatenate the value of the variable into the string
expression as the function's criterion. With a number data type:

DCount("[Period]", "Query", "[Per_Num] = " & z)

In the case of a text data type wrap the value in quotes:

DCount("[Period]", "Query", "[Per_Num] = """ & z & """")

You can't reference a variable directly in a query, however. If you want to
refer to a public variable in a query you need to wrap the variable in a
function in a standard module, e.g.

Function GetZ()
GetZ = z
End Function

and refer to the function GetZ() in the query.
 
Back
Top