MS Access列表框-从字段中添加项目后,所有计算出的字段都将进入“ #Error”和“ #Type!”

问题描述

这是一个奇怪的错误,我似乎只是偶尔遇到。当我解决此问题时,它是随机发生的,并且我永远无法重现该问题/解决方案。

情况是这样。我有一个包含多个字段的表单,填写后用户单击Add按钮时,它将把这些字段添加到名为Processing_Lb_R的列表框中。 (有关表单的外观,请参见下面的图像)。 Remnant CodeWeightNew Cost字段是计算字段。

如您所见,这些计算字段正在正确计算。当我单击添加时,它会收集所有必要的字段,并尝试将它们插入到图像顶部的列表框中(请忽略下面的列表框,因为与该问题无关)。

这是添加按钮Click事件:

Private Sub Add_Button_R_Click()

    ' Validation happens here
    ' ...

    Dim jobNum As String
    jobNum = Job_Number_R.value

    remnantDict.Add Item_Type_R.value & "-" & Item_Code_R.value & "-" & jobNum,1
    
    ' This code runs fine. The item is added to the listBox.
    ' Immediately after though,the value of the Remnant_Code goes to #Error,the value of Weight goes to #Type!,and the value of New_Cost goes to #Type!.
    Processing_Lb_R.AddItem (Item_Type_R.value & ";" & Item_Code_R.value & ";" & Job_Number_R.value & ";" & STD_Weight_Per_Ft_R.value & ";" & Category_R.value & ";" & Grade_R.value & ";" & Gauge_R.value & ";" & Width_R.value & ";" & Length_R.value & ";" & Remnant_Code_R.value & ";" & Quantity_R.value & ";" & Weight_R.value & ";" & New_CWT_R.value & ";" & New_Cost_R.value)
    
    ' This method depends on the calculated fields values. So it errors out since the values are invalid.
    addLocationValuesToLb
       
End Sub

起初,我认为这是因为将无效字符添加到了列表框,使它奇怪地起作用了(逗号,分号甚至是撇号),但事实并非如此,正如您在我的代码和字段图像中所看到的那样数据。

然后我认为Access可能存在问题,如果列表框中的列数与添加的项目数不匹配,则会导致问题。事实并非如此,因为列表框有14列。这是Column Widths属性的格式:

2.501cm;5cm;2cm;0cm;0cm;0cm;0cm;0cm;0cm;5cm;1.524cm;2cm;2cm;2cm

如果您查看我在列表框中添加一个项目的语句,则有14个项目。

为什么会发生此错误?在添加到与字段值更改无关的列表框后,为什么所有计算出的字段都会重新计算并导致无效值?当我添加到导致这些更改的列表框时,是否会触发一个事件?

谢谢

enter image description here

编辑:

我将这些值添加到列表框中,因为单击“添加”并将它们插入到列表框中后,用户可以再次填写字段并添加更多数据。这个想法是直到他们单击Submit按钮之前,什么都不会插入到表中。因此,他们可以在不触摸任何表的情况下从列表框中添加/删除数据。

这是计算字段的功能。我不认为这些功能内容对于此问题很重要,因为就像我说的那样,只有在将项目添加到列表框中时才会出现计算错误。我不反对计算所依赖的任何表单字段。

这是重量计算的代码

Public Function calculateWeight(itemCode As String,quantityVal As Variant,Optional itemType As String) As Long

On Error GoTo ErrorHandler

    ' If any of the fields are empty,set weight to 0
    If Trim(itemCode & vbNullString) = vbNullString Then
        calculateWeight = 0
        Exit Function
    End If

    If Trim(quantityVal & vbNullString) = vbNullString Then
        calculateWeight = 0
        Exit Function
    End If
    
    If quantityVal = 0 Then
        calculateWeight = 0
        Exit Function
    End If

    Dim widthVal As Variant
    Dim lengthVal As Variant
    Dim stdWeight As Double
    
    ' If the itemtype isn't set,search from inventory
    If Trim(itemType & vbNullString) = vbNullString Then
        itemType = "Inventory"
    End If
    
    If itemType = "Inventory" Or itemType = "Remnant" Then
        widthVal = DLookup("Width",itemType,"Code='" & itemCode & "'")
        lengthVal = DLookup("Length","Code='" & itemCode & "'")
        stdWeight = DLookup("STD_Weight_Per_Ft","Code='" & itemCode & "'")
    Else
        MsgBox ("Error: Invalid item type.")
    End If

    calculateWeight = -Int(-(widthVal * lengthVal * quantityVal) / 144 * stdWeight)

Exit Function

ErrorHandler:

    MsgBox (Err.Number & ": " & Err.Description & vbCrLf & vbCrLf & "Contact the administrator for more help")

End Function

以下是处理成本计算的代码

Public Function calculateProcessingCost(itemCode As String,weightVal As Long,itemType As String,percentageVal As Double) As Currency

    ' If any of the fields are empty,set cost to 0
    If Trim(itemCode & vbNullString) = vbNullString Or Trim(weightVal & vbNullString) = vbNullString Then
        calculateProcessingCost = 0
        Exit Function
    End If

    If Trim(itemType & vbNullString) = vbNullString Then
        calculateProcessingCost = 0
    End If

    Dim avgCost As Currency
    
    If itemType = "Inventory" Or itemType = "Remnant" Then
        avgCost = DLookup("Average_CWT","Code='" & itemCode & "'")
    Else
        MsgBox ("Error: Invalid item type")
        avgCost = 0
    End If
    calculateProcessingCost = Round((weightVal * avgCost / 100) * percentageVal / 100,2)

End Function

以下是用于生成剩余代码代码

Public Function generateRemnantCode(inventoryCode As String,widthVal As Long,lengthVal As Long) As String

    If Trim(inventoryCode & vbNullString) = vbNullString Then
        generateRemnantCode = ""
        Exit Function
    End If
    
    If Trim(widthVal & vbNullString) = vbNullString Then
        generateRemnantCode = ""
        Exit Function
    End If
    
    If Trim(lengthVal & vbNullString) = vbNullString Then
        generateRemnantCode = ""
        Exit Function
    End If

    ' Get remnant code from product code
    Dim remnantCode As String
    Dim index As Integer
    
    ' Get position of first x. This represents the length.
    index = InStrRev(inventoryCode,"x")
    ' Get position of second x. This represents the width.
    index = InStrRev(inventoryCode,"x",index - 1)
    ' The remnant code will use whatever is to the left of the width
    remnantCode = Left(inventoryCode,index - 1)
    
    'Add the new width and length to the remnant code
    remnantCode = remnantCode & "x" & widthVal & "x" & lengthVal
    
    generateRemnantCode = remnantCode

End Function

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)