在 Dlookup 中使用变量来通过 For Each 循环

Using Varible in a Dlookup to rifle through a For Each Loop

我正在计算来自不同 table 的数字的概率。每个客户都有一个专门用于文档编号的字母。这些字母有概率(0/0.3 或 0.7)。我是 Vba 的新手,真的不知道为什么我会收到这个错误,或者我该如何解决它。

Dim Sum As Double
Dim CB As Double
Dim Csize As Double
Dim USp As Double
Dim DocCat As String

Sum = 0

'CB Loop - Go through all customers in CArray and get the document letter corresponding to the document number in the textbox named SBBox

Dim CArray(1 To 5) As String
CArray(1) = "FF"
CArray(2) = "GG"
CArray(3) = "HH"
CArray(4) = "JJ"
CArray(5) = "DD"

Dim customer As Variant
For Each customer In CArray

'Get portion from customer table by corresponding the customer name with name in array. Here is my problem at the moment
Csize = DLookup("[portion]", "tblCustomer", "[sName]" = customer)

'get document letter from table customer depending on name from array. The table contaning the DocLetters is named the same as in the array (e.g. FF GG HH and so on)
    DocCat = DLookup("[DocLetter]", customer, "[Doc Number]= SBBox.Value")

   
'Use the DocCat to categoriez "bad", "good", "great" Letter
    If StrComp(DocCat, "A") Or StrComp(DocCat, "B") Or StrComp(DocCat, "C") Then

    CB = 0

    ElseIf DocCat = StrComp(DocCat, "E") Or StrComp(DocCat, "F") or Null Then

    CB = 0.3

    Else: CB = 0.7

    End If

'get the usage amount from the tblUsage depending from the PartNumber in PNBox
 USp = DLookup("[Rel]", "tblUsage", "[Material]=PNBox.Value")

'sum up all poducts from each loop

      Sum = Sum + CB * Csize * USp


Next customer


'print out inTextbox
    Sum = TextBox1.Text

End Sub


我在 Csize = DLookup 上收到错误“无效使用 Null”.... 我猜是因为标准

我不可能有任何 table 名字,代码将在未来扩展。

如有任何帮助,我们将不胜感激。

您必须连接变量:

' Text
Csize = Nz(DLookup("[portion]", "tblCustomer", "[sName]" = '" & customer & "'"))

' Number
DocCat = DLookup("[DocLetter]", customer, "[Doc Number] = " & SBBox.Value & "")

' Text
USp = DLookup("[Rel]", "tblUsage", "[Material] = '" & PNBox.Value & "'")