在MS Access中使用recordset.findfirst进行记录搜索会引发错误3070

时间:2019-02-24 02:09:15

标签: access-vba ms-access-2016

我正在尝试建立一个MS Access数据库。

其中之一是记录管理表单中的自动填充。

表单布局:

The layout of the form is like this

请注意,电池ID和型号在一个表中(称为表1),化学类型,规格电压,规格容量基于型号(在表2中)存储在另一表中。这两个表在“型号”条目中以一对多关系连接在一起。

我想实现的是让用户能够使用afterupdate()输入电池ID,如果表1中已经存在电池ID,则搜索表1,获取型号。然后搜索表2,获取电池型号详细信息(如果有)。

To clarify the flow, here is the flowchart

“搜索表1以匹配电池ID”部分使用以下代码:

Private Sub txtBatteryID_AfterUpdate()
    'Set the path to the Battery Records table
    Set RS = CurrentDb.OpenRecordset("Batteries for Portable Radios", dbOpenDynaset)

    'do a findfirst search for the Battery ID, using value from textbox txtBatteryID
    RS.FindFirst "[Battery ID]=" & txtBatteryID

    'If no matching record, leave the other fields empty
    If RS.NoMatch Then
        cmbModelNumber.Value = Null
        cmbChemistryType.Value = Null
        txtSpecVoltage.Value = Null
        txtSpecCapacity.Value = Null
    'If there is a matching record, then, grab the model number
    Else
        cmbModelNumber.Value = RS("Model Number")
        'as there is an existing record with model number, run a search on the model number and grab the model info
        Call cmbModelNumber_AfterUpdate
    End If
    'close recordset
    RS.Close
    'clear recordset path
    Set RS = Nothing
End Sub

对于“搜索表2以匹配型号”部分,我认为它是相同的结构:

Private Sub cmbModelNumber_AfterUpdate()
    'Set the path to the Model Records table
    Set ModelRS = CurrentDb.OpenRecordset("tblInfoBatteryModelByModel#", dbOpenDynaset)

    'do a findfirst search for the Model Number, using value from combobox cmbModelNumber
    ModelRS.FindFirst "[Model Number]=" & cmbModelNumber

    'If no matching record, leave the other fields empty
    If ModelRS.NoMatch Then
        cmbChemistryType.Value = Null
        txtSpecVoltage.Value = Null
        txtSpecCapacity.Value = Null
     'If there is a matching record, then, grab the Model Info
    Else
        cmbChemistryType.Value = ModelRS("Chemistry Type")
        txtSpecVoltage.Value = ModelRS("Spec Voltage (V)")
        txtSpecCapacity.Value = ModelRS("Spec Capacity (mAh)")
    End If
    'close recordset
    ModelRS.Close
    'clear recordset path
    Set ModelRS = Nothing

End Sub

这次,除非我键入现有的电池ID或型号,否则会引发3070错误。

错误消息:

Error Message

问题专线:

Problematic Line

我不知道为什么这不会取值。

1 个答案:

答案 0 :(得分:1)

由于您的Model Number字段看起来是一个字符串,因此您将需要用单引号或双引号将FindFirst方法提供的条件值括起来,例如:

ModelRS.FindFirst "[Model Number]='" & cmbModelNumber & "'"

或者:

ModelRS.FindFirst "[Model Number]=""" & cmbModelNumber & """"