Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access Access VBA DLookup:查询表达式中的语法错误(缺少运算符)_Ms Access_Vba - Fatal编程技术网

Ms access Access VBA DLookup:查询表达式中的语法错误(缺少运算符)

Ms access Access VBA DLookup:查询表达式中的语法错误(缺少运算符),ms-access,vba,Ms Access,Vba,我有一个表单,其中包含一个多选复选框列表框。此字段从“国家”表中提取值,该表有一个“当前索引”列,我需要为每个选定的国家检查该列。更新字段后,我希望找到所选国家/地区的最低“当前索引”值,并用此值填充单独的字段“最低腐败指数” 我已经将下面的VBA代码附加到复选框列表框,但是当我测试它时,我得到以下错误。有人能指出我遗漏了什么吗?我假设这与我如何连接DLookup的标准有关,但在网上找不到任何有用的指导 Private Sub Countries_AfterUpdate() Dim curre

我有一个表单,其中包含一个多选复选框列表框。此字段从“国家”表中提取值,该表有一个“当前索引”列,我需要为每个选定的国家检查该列。更新字段后,我希望找到所选国家/地区的最低“当前索引”值,并用此值填充单独的字段“最低腐败指数”

我已经将下面的VBA代码附加到复选框列表框,但是当我测试它时,我得到以下错误。有人能指出我遗漏了什么吗?我假设这与我如何连接DLookup的标准有关,但在网上找不到任何有用的指导

Private Sub Countries_AfterUpdate()

Dim currentIndex As Variant     'variable to hold index of current country during iteration
Dim countryID As Variant        'variable for iterating through array
Dim arrCountryIDs() As Variant  'array of country IDs
Dim recCountries As Recordset   'Countries table recordset
Dim bytLowestIndex As Byte      'variable to track lowest corruption index of selected countries
Dim strLookupCriteria As String 'workaround for trying to concatenate variable into DLookup criteria

'assign contents of Countries field to array
arrCountryIDs = Me![Countries].Value

'assign lookup countries table as recordset
Set recCountries = CurrentDb.OpenRecordset("look_countries")

'set lowest index variable to highest possible value
bytLowestIndex = 100

'iterate through country ID array, look up index, compare to and update lowest index variable
For Each countryID In arrCountryIDs

    strLookupCriteria = "Country ID = " & countryID

    currentIndex = DLookup("Current Index", "recCountries", strLookupCriteria)

    If currentIndex < bytLowestIndex Then bytLowestIndex = currentIndex

Next countryID

'populate lowest corruption index field
Me![Lowest Corruption Index] = bytLowestIndex

'close recordset and empty all variable
recCountries.Close

Erase arrCountryIDs
Set recCountries = Nothing

End Sub
Private Sub Countries\u AfterUpdate()
Dim currentIndex作为变量,用于在迭代期间保存当前国家/地区的指数
Dim countryID作为变量,用于遍历数组
Dim ARRCONTRYIDS()作为国家ID的变量数组
Dim recCountries作为记录集“国家表记录集”
Dim bytLowestIndex作为“字节”变量,用于跟踪选定国家/地区的最低腐败指数
Dim strLookupCriteria作为字符串的解决方案,用于尝试将变量连接到DLookup标准中
'将国家/地区字段的内容分配给数组
阿里兹=我![国家].价值
'将查找国家/地区表指定为记录集
Set recCountries=CurrentDb.OpenRecordset(“look_countries”)
'将最低索引变量设置为可能的最高值
bytLowestIndex=100
'遍历国家ID数组,查找索引,比较并更新最低索引变量
对于ArrCountryID中的每个countryID
strLookupCriteria=“国家ID=”和国家ID
currentIndex=DLookup(“当前指数”,“国家/地区”,标准)
如果currentIndex
更新:以防这对将来的其他人有用,这里是我的最终代码

Private Sub Countries_AfterUpdate()

Dim currentIndex As Variant     'variable to hold index of current country during iteration
Dim countryID As Variant        'variable for iterating through array
Dim arrCountryIDs() As Variant  'array of country IDs
Dim bytLowestIndex As Byte      'variable to track lowest corruption index of selected countries

'assign contents of Countries field to array
arrCountryIDs = Me![Countries].Value

'set lowest index variable to highest possible value
bytLowestIndex = 100

'iterate through country ID array
For Each countryID In arrCountryIDs

    'look up index for this country
    currentIndex = DLookup("[Current Index]", "look_countries", "[Country ID] = " & countryID)

    'update the lowest index variable if this value is lower
    If currentIndex < bytLowestIndex Then bytLowestIndex = currentIndex

Next countryID

'populate lowest corruption index field
Me![Lowest Corruption Index] = bytLowestIndex

'clear the array
Erase arrCountryIDs

End Sub
Private Sub Countries\u AfterUpdate()
Dim currentIndex作为变量,用于在迭代期间保存当前国家/地区的指数
Dim countryID作为变量,用于遍历数组
Dim ARRCONTRYIDS()作为国家ID的变量数组
Dim bytLowestIndex作为“字节”变量,用于跟踪选定国家/地区的最低腐败指数
'将国家/地区字段的内容分配给数组
阿里兹=我![国家].价值
'将最低索引变量设置为可能的最高值
bytLowestIndex=100
'遍历国家ID数组
对于ArrCountryID中的每个countryID
“查找这个国家的索引
currentIndex=DLookup(“[Current Index]”、“look_countries”、“[Country ID]=”&countryID)
'如果此值较低,则更新最低索引变量
如果currentIndex
在字段名中使用空格,因此:

strLookupCriteria = "[Country ID] = " & countryID
currentIndex = DLookup("[Current Index]", "recCountries", strLookupCriteria)

请您出示一个错误的
strLookupCriteria
示例。我想您需要
[Country ID]=
谢谢。当我按照您的建议添加到括号中时,我收到了另一个错误,即无法定位记录集“recCountries”。因为这是在子程序中定义的变量,所以我尝试删除引号。现在我在DLookup上遇到了一个类型不匹配的错误,我解决了这个问题,并且能够简化我的代码。事实证明,我不需要打开look_countries表作为数据集。完整的代码请参见原始帖子。哦,这很有意义。