0

我想在 Form_Load 上循环一些 VBA 代码,它将根据其他 2 个字段更新我的表单中的一个字段。目前它只会更新第一条记录或当我点击一条记录时。

目前它看起来像这样:

Private Sub Form_Current()

    If Style = "W" And Size = "120" Then ContainerType = 9
    If Style = "W" And Size = "240" Then ContainerType = 2
    If Style = "W" And Size = "360" Then ContainerType = 34
    If Style = "R" And Size = "120" Then ContainerType = 37
    If Style = "R" And Size = "240" Then ContainerType = 5
    If Style = "R" And Size = "360" Then ContainerType = 12
    If Style = "Y" And Size = "120" Then ContainerType = 24
    If Style = "Y" And Size = "240" Then ContainerType = 4
    If Style = "Y" And Size = "360" Then ContainerType = 14
    If Style = ("2Y") And Size = "120" Then ContainerType = 9
    If Style = ("2Y") And Size = "240" Then ContainerType = 25
    If Style = ("2Y") And Size = "360" Then ContainerType = 28
    If Style = ("3Y") And Size = "120" Then ContainerType = 9
    If Style = ("3Y") And Size = "240" Then ContainerType = 51
    If Style = ("3Y") And Size = "360" Then ContainerType = 29

End Sub

也许有更好的方法来做到这一点?

4

2 回答 2

0

当然,“更好的方法”是创建一个名为 [ContainerType_lookup] 的表:

Style  Size  ContainerType
-----  ----  -------------
W      120               9
W      240               2
W      360              34
...

然后使用 SQLJOINDLookup()函数找到合适的ContainerType. 那样...

  1. 如果发生变化(例如新的 ContainerType),您不必修改代码,并且

  2. 如果您需要在应用程序的其他任何地方派生 ContainerType,您也不必在那里复制您的 VBA 逻辑。

于 2013-05-30T08:43:36.293 回答
0

在我看来,我认为您可以使用 Select Case 来拆分它并减轻阅读负担。根据您的需要,您还可以获得更多按大小或样式对它们进行分组的组织。

但是,我同意使用带或不带查询的表将使您的生活更轻松。

Select Case Size
    Case "120" 
        If Style = "W" Then ContainerType = 9
        If Style = "R" Then ContainerType = 37
        ...
    Case "240"
        If Style = "W" Then ContainerType = 2
        If Style = "R" Then ContainerType = 5
        ...
    Case "360"
        If Style = "W" Then ContainerType = 34
        If Style = "R" Then ContainerType = 12
        ...
    Case Else
        Exit Sub
End Select
于 2013-05-30T11:51:08.430 回答