Share via

Using DAvg with variables

Anonymous
2014-03-08T14:22:44+00:00

I am using Access 2007 to calculate an Average and Median value for a set of values. I have used the AVG in a query and DAvg in VBA and it works if I write the criteria. I want to replace the criteria with a string that I manipulate. I use a function called DMedian that works the same way.

The overall expression  of variable =DAvg([Field], [Table], Criteria) translates to variable=DAvg("[TimeToClose]", "WorkRequests", [Shop] ='Machine'").

If I define a string in vba to use  like Shp = "Machine" and try to run this I can get:

variable=DAvg("[TimeToClose]", "WorkRequests", [Shop] ='& Shp"')

I only get a Null value returned.

A few questions:

  1. Can I use a variable in DAvg in this manner?
  2. If I can, where I am going wrong on the Syntax?

Any help would be appreciated!

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    2014-03-08T14:50:24+00:00

    Your syntax is slightly off.  Try:

    Shp = "Machine"

    variable=DAvg("[TimeToClose]", "WorkRequests", "[Shop] ='" & Shp & "'")

    Was this answer helpful?

    0 comments No comments