MoveNext方法上的ADO记录集数据类型不匹配

时间:2018-07-10 12:27:43

标签: vba ms-access adodb recordset

我遇到了一个问题,经过广泛的Google搜索,我似乎无法解决。一周以来,用VBA编写的函数之一出现了“运行时错误'-2147467259(80004005)':条件表达式中的数据类型不匹配”。

发生错误的行是“ rstBom.MoveNext”。我似乎缺乏了解(我不是受过教育的VBA程序员)的知识,无法理解记录集中的跳过如何导致数据类型不匹配...

Public Function Packing_PL_BOM()

Dim rst As New ADODB.Recordset
Dim rstIN As New ADODB.Recordset
Dim rstBom As New ADODB.Recordset
Dim rstSC As New ADODB.Recordset

CurrentProject.Connection.Execute "delete from tblPacking_private_label_base"

rst.Open "qryPacking_Private_label_stock", CurrentProject.Connection, adOpenDynamic, adLockReadOnly
rstIN.Open "tblPacking_private_label_base", CurrentProject.Connection, adOpenDynamic, adLockOptimistic

If Not rst.EOF Then
    Do Until rst.EOF
        rstBom.Open "select * from ZBOM_XLS where val(Component) = " & rst.Fields("matnr").Value, CurrentProject.Connection, adOpenDynamic, adLockReadOnly
         If Not rstBom.EOF Then
            Do Until rstBom.EOF
                rstIN.AddNew
                rstIN.Fields("matnr").Value = rst.Fields("matnr").Value
                rstIN.Fields("material_description").Value = rst.Fields("material_description").Value
                rstIN.Fields("Value").Value = rst.Fields("Value").Value
                rstIN.Fields("description").Value = rst.Fields("description").Value
                rstIN.Fields("uom").Value = rst.Fields("uom").Value
                rstIN.Fields("Stock_pack_PC").Value = rst.Fields("SumOfUnrestricted").Value
                rstIN.Fields("Packed_matnr").Value = rstBom.Fields("Material").Value
                rstIN.Fields("Packed_description").Value = rstBom.Fields("Material Description").Value

                Select Case rstBom.Fields("Alternative BOM").Value
                    Case "1"
                        rstSC.Open "select * from qryMaterial_Class_SC_Values where matn = " & rstBom.Fields("material").Value & " and [Supply Chain] = 'PRODUCTION'", CurrentProject.Connection, adOpenDynamic, adLockReadOnly
                        If Not rstSC.EOF Then
                            rstIN.Fields("BOM_found").Value = "X"

                        Else
                            rstIN.Fields("BOM_found").Value = "DEL"

                        End If
                        rstSC.Close

                    Case "4"
                        rstSC.Open "select * from qryMaterial_Class_SC_Values where matn = " & rstBom.Fields("material").Value & " and [Supply Chain] = 'PINGUIN'", CurrentProject.Connection, adOpenDynamic, adLockReadOnly
                        If Not rstSC.EOF Then
                            rstIN.Fields("BOM_found").Value = "X"
                        Else
                            rstIN.Fields("BOM_found").Value = "DEL"

                        End If
                        rstSC.Close

                    Case "5"
                        rstSC.Open "select * from qryMaterial_Class_SC_Values where matn = " & rstBom.Fields("material").Value & " and [Supply Chain] = 'SIVAFROST'", CurrentProject.Connection, adOpenDynamic, adLockReadOnly
                        If Not rstSC.EOF Then
                            rstIN.Fields("BOM_found").Value = "X"

                        Else
                            rstIN.Fields("BOM_found").Value = "DEL"

                        End If
                        rstSC.Close

                    Case "6"
                         rstSC.Open "select * from qryMaterial_Class_SC_Values where matn = " & rstBom.Fields("material").Value & " and [Supply Chain] = 'DIRAMAR'", CurrentProject.Connection, adOpenDynamic, adLockReadOnly
                         If Not rstSC.EOF Then
                            rstIN.Fields("BOM_found").Value = "X"

                         Else
                            rstIN.Fields("BOM_found").Value = "DEL"

                         End If
                         rstSC.Close

                     Case "7"
                        'rstIN.Fields("BOM_found").Value = "COPACK"
                        rstSC.Open "select * from qryMaterial_Class_SC_Values where matn = " & rstBom.Fields("material").Value & " and [Supply Chain] = 'COPACK'", CurrentProject.Connection, adOpenDynamic, adLockReadOnly
                        If Not rstSC.EOF Then
                            rstIN.Fields("BOM_found").Value = "X"
                        Else
                            rstIN.Fields("BOM_found").Value = "DEL"


                        End If
                        rstSC.Close

                    Case "8"
                        'rstIN.Fields("BOM_found").Value = "YUBE"
                        rstSC.Open "select * from qryMaterial_Class_SC_Values where matn = " & rstBom.Fields("material").Value & " and [Supply Chain] = 'YUBE'", CurrentProject.Connection, adOpenDynamic, adLockReadOnly
                        If Not rstSC.EOF Then
                            rstIN.Fields("BOM_found").Value = "X"
                        Else
                            rstIN.Fields("BOM_found").Value = "DEL"


                        End If
                        rstSC.Close

                    Case "9"
                        'rstIN.Fields("BOM_found").Value = "POLAND"
                        rstSC.Open "select * from qryMaterial_Class_SC_Values where matn = " & rstBom.Fields("material").Value & " and [Supply Chain] = 'POLAND'", CurrentProject.Connection, adOpenDynamic, adLockReadOnly
                        If Not rstSC.EOF Then
                            rstIN.Fields("BOM_found").Value = "X"
                        Else
                            rstIN.Fields("BOM_found").Value = "DEL"


                        End If
                        rstSC.Close

                    Case Else
                        rstIN.Fields("BOM_found").Value = "DEL"

                End Select


                rstIN.Update

                rstBom.MoveNext

            Loop

        End If 
        rstBom.Close
    rst.MoveNext
    Loop
End If

CurrentProject.Connection.Execute "delete from tblPacking_private_label_base where BOM_Found = 'DEL'"
End function

奇怪的是,它两次给出错误,我单击“调试”,第三次运行完全正常。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

我重写了val(component)= ...,它成功了。 非常感谢您的帮助!