Access 2010 VBA类型不匹配(错误13)或运行时错误424(必需的对象)

时间:2018-08-31 17:11:26

标签: vba ms-access-2010

我无法在Access 2010中创建VBA代码来执行2个操作:

  1. 如果我的访问表中的3条地址行是NULL,或者与城市或国家/地区名称匹配,请将它们导出到单独的Excel文件中,以便手动进行审核

  2. 如果没有,则在另一个访问表的“地址”列中选择一个或多个地址值。

这只是试用代码,这意味着我只想表明我可以在输入所有ElseIf条件之前相应地导出值。

  • 按原样,我得到

      

    “类型不匹配”(错误13)。

  • 在Google搜索之后,似乎我分配的对象/方法不正确。如果我从引用的对象中删除"",则会收到另一个错误:

      

    “运行时错误424(必需的对象)。

下面是代码。运行并收到错误后,“ nmad_address_1”的第一个实例将突出显示。

Public Sub EditFinalOutput()

'set loop variable
Dim i As Long
Dim ExportFile As String


'Open Reference Set
Set db = CurrentDb
Set qs = db.OpenRecordset("SunstarAccountsInWebir_SarahTest")
Set ss = db.OpenRecordset("1042s_FinalOutput_6")

'Set loop for entirety of recordset
For i = 0 To qs.RecordCount - 1
    'ExportFile = "I:\Tax Team\Tax Team\Sean" & Format(i, "00") & ".xlsx"

    If qs.Fields(("nmad_address_1" Is Null Or "nmad_address_1" = "nmad_city" 
Or "nmad_address_1" = "Webir_Country") And ("nmad_address_2" Is Null Or 
"nmad_address_2" = "nmad_city" Or "nmad_address_2" = "Webir_Country") And 
("nmad_address_3" Is Null Or "nmad_address_3" = "nmad_city" Or 
"nmad_address_3" = "Webir_Country")) Then
    DoCmd.TransferSpreadsheet acImport, 10, 
"SunstarAccountsInWebir_Charlie", "I:\Tax Team\Tax Team\Sean", 1


    Else:   qs.Edit
            qs.Fields("box13c_Address") = 9999
            qs.Update
    End If
    qs.MoveNext
Next i

'Close Reference Set
qs.Close
Set qs = Nothing
ss.Close
Set ss = Nothing

End Sub

1 个答案:

答案 0 :(得分:4)

此:

If qs.Fields(("nmad_address_1" Is Null Or "nmad_address_1" = "nmad_city" 
Or "nmad_address_1" = "Webir_Country") And ("nmad_address_2" Is Null Or 
"nmad_address_2" = "nmad_city" Or "nmad_address_2" = "Webir_Country") And 
("nmad_address_3" Is Null Or "nmad_address_3" = "nmad_city" Or 
"nmad_address_3" = "Webir_Country")) Then 

是胡说八道。您正在将字符串与空字符串进行比较,并将常量字符串彼此进行比较。您实际上并没有比较任何字段值。我什至不知道哪些应该是字段名称,哪些应该是字符串。

此外,您在查询中进行SomeVar Is Null。在vba中,您进行IsNull(SomeVar)

尝试以下操作:

With qs.Fields
    If IsNull(!nmad_address_1) Or !nmad_address_1 = "nmad_city" Or !nmad_address_1 = "Webdir_Country" 'etc...
End With