如何在VBA中将查询结果设置为等于变量?

时间:2020-12-19 15:42:54

I have a table titled MIUInfo. In it there are three columns; MIUID, Latitude, and Longitude.
In it I'm running the following query which gets the number of MIUIDs in the criteria range.

我有一张名为MIUInfo的表。其中有三列; MIUID,纬度和经度。在其中我运行以下查询,该查询获取条件范围内的MIUID数。

SELECT Count(MIUInfo.MIUID)
      From MIUInfo 
      WHERE ((MIUInfo.Latitude Between " & minLat & " And " & maxLat & ") 
      AND (MIUInfo.Longitude Between " & minLong & " And " & maxLong & "))

I need to set the result of this query equal to a variable in VBA so that I can use it as a condition for a loop. I can't figure out how to do this.

我需要将此查询的结果设置为等于VBA中的变量,以便我可以将其用作循环的条件。我无法弄清楚如何做到这一点。

Also, when I try to use the DoCmd.RunSQL command I get an error:

此外,当我尝试使用DoCmd.RunSQL命令时,我收到一个错误:

Run-time error '2342': A RunSQL action requires an argument of an SQL statement.

运行时错误'2342':RunSQL操作需要SQL语句的参数。

The only difference between this query and others I'm running with the DoCmd.RunSQL is that all of the other select queries are generating a new table.

此查询与我正在使用DoCmd.RunSQL运行的其他查询之间的唯一区别是,所有其他选择查询都在生成新表。

2 个解决方案

#1


In Access the easiest way to do this is with the DCount function:

在Access中,最简单的方法是使用DCount函数:

Total = DCount("MIUInfo.MIUID", "MIUInfo", "(MIUInfo.Latitude Between " & minLat & " And " & maxLat & ") AND (MIUInfo.Longitude Between " & minLong & " And " & maxLong & ")")

#2


Use DCOUNT

#1


In Access the easiest way to do this is with the DCount function:

在Access中,最简单的方法是使用DCount函数:

Total = DCount("MIUInfo.MIUID", "MIUInfo", "(MIUInfo.Latitude Between " & minLat & " And " & maxLat & ") AND (MIUInfo.Longitude Between " & minLong & " And " & maxLong & ")")

#2


Use DCOUNT