请向我展示一种在Access VBA中使用类似值编写此代码的更好方法吗?

时间:2018-07-26 22:06:13

标签: vba ms-access

我有用于填充Web表单的访问代码。我的某些字段名称相似。我想做的是整合它们,因此在复制和粘贴代码时我不会重复。以下示例是带有5个选项的“操作”字段:

Dim Action1, Action2, Action3, Action4 As Integer

Action1 =  IIf(Me.cbo_Action1 = "buy", 1, _
           IIf(Me.cbo_Action1 = "sell", 2, _
           IIf(Me.cbo_Action1 = "trade", 3, _
           IIf(Me.cbo_Action1 = "hold", 4, _
           IIf(Me.cbo_Action1 = "Other", 5, 0)))))

Action2 = IIf(Me.cbo_Action2 = "buy", 1, _
          IIf(Me.cbo_Action2 = "sell", 2, _
          IIf(Me.cbo_Action2 = "trade", 3, _
          IIf(Me.cbo_Action2 = "hold", 4, _
          IIf(Me.cbo_Action2 = "other", 5, 0)))))

Action3 = IIf(Me.cbo_Action3 = "buy", 1, _
          IIf(Me.cbo_Action3 = "sell", 2, _
          IIf(Me.cbo_Action3 = "trade", 3, _
          IIf(Me.cbo_Action3 = "hold", 4, _
          IIf(Me.cbo_Action3 = "Other", 5, 0)))))

Action4 = IIf(Me.cbo_Action4 = "buy", 1, _
          IIf(Me.cbo_Action4 = "sell", 2, _
          IIf(Me.cbo_Action4 = "trade", 3, _
          IIf(Me.cbo_Action4 = "hold", 4, _
          IIf(Me.cbo_Action4 = "other", 5, 0)))))

'Add Action 1 to Web Form
IE.Document.All("txtAction").Value = Action1
IE.Document.All("ddlAction").FireEvent ("onchange")

If Action1 = 5 Then
    IE.Document.All("txtOther").Focus
    IE.Document.All("txtOther").Value = ActionOth1
End If

IE.Document.All("btnactionsubmit").FireEvent ("onclick")

'Add Action 2 to Web Form
IE.Document.All("txtAction").Value = Action2
IE.Document.All("ddlAction").FireEvent ("onchange")

If Action2 = 5 Then
    IE.Document.All("txtOther").Focus
    IE.Document.All("txtOther").Value = ActionOth1
End If

IE.Document.All("btnactionsubmit").FireEvent ("onclick")

2 个答案:

答案 0 :(得分:2)

看起来您可以通过添加一些辅助方法来保存一些复制/粘贴“卷”。也许这样的事情可能会有所帮助:

Sub ModifiedExampleWithHelpers()

    AddAction GetAction(Me.cbo_Action1.Value), ActionOth1
    AddAction GetAction(Me.cbo_Action2.Value), ActionOth2
    AddAction GetAction(Me.cbo_Action3.Value), ActionOth3
    AddAction GetAction(Me.cbo_Action4.Value), ActionOth4

End Sub

Private Function GetAction(selection As String) As Integer
    Select Case selection
        Case "buy"
            GetAction = 1
        Case "sell"
            GetAction = 2
        Case "trade"
            GetAction = 3
        Case "hold"
            GetAction = 4
        Case "other"
            GetAction = 5
        Case Else
            GetAction = 0
    End Select
End Function

'Assumes ActionOth1 is an Integer
Private Sub AddAction(action As Integer, actionOth As Integer)

    IE.Document.All("txtAction").Value = action
    IE.Document.All("ddlAction").FireEvent ("onchange")

    If action = 5 Then
        IE.Document.All("txtOther").Focus
        IE.Document.All("txtOther").Value = actionOth
    End If

    IE.Document.All("btnactionsubmit").FireEvent ("onclick")
End Sub

答案 1 :(得分:0)

我将不得不再玩一些。 getAction对我不起作用。不确定我在做什么错,但是当我的原始代码是多余的时,当我执行onclick过程将数据从Access传输到Web表单时,它可以工作。 Dim Action1, Action2, Action3, Action4 As Integer Dim ActionOth1, ActionOth2, ActionOth3, ActionOth4 As String Dim Outcome1, Outcome2, Outcome3, Outcome4 As String

Action1 = IIf(Me.cbo_Action1 = "buy", 1, _
          IIf(Me.cbo_Action1 = "sell", 2, _
          IIf(Me.cbo_Action1 = "trade", 3, _
          IIf(Me.cbo_Action1 = "hold", 4, _
          IIf(Me.cbo_Action1 = "Other", 5, 0)))))
Action2 = IIf(Me.cbo_Action2 = "buy", 1, _
          IIf(Me.cbo_Action2 = "sell", 2, _
          IIf(Me.cbo_Action2 = "trade", 3, _
          IIf(Me.cbo_Action2 = "hold", 4, _
          IIf(Me.cbo_Action2 = "other", 5, 0)))))
Action3 = IIf(Me.cbo_Action3 = "buy", 1, _
          IIf(Me.cbo_Action3 = "sell", 2, _
          IIf(Me.cbo_Action3 = "trade", 3, _
          IIf(Me.cbo_Action3 = "hold", 4, _
          IIf(Me.cbo_Action3 = "Other", 5, 0)))))
Action4 = IIf(Me.cbo_Action4 = "buy", 1, _
          IIf(Me.cbo_Action4 = "sell", 2, _
          IIf(Me.cbo_Action4 = "trade", 3, _
          IIf(Me.cbo_Action4 = "hold", 4, _
          IIf(Me.cbo_Action4 = "other", 5, 0)))))

IE.Document.All("ddlAction").Value = Action1
IE.Document.All("ddlAction").FireEvent ("onchange")

Do
Loop Until IE.ReadyState = 4
While IE.Busy
DoEvents
Wend
If Action1 = 5 Then
IE.Document.All("txtOther").Focus
IE.Document.All("txtOther").Value = ActionOth1
End If
IE.Document.All("txtOutcome").Value = Outcome1
IE.Document.All("btnactionsubmit").FireEvent ("onclick")

Do
Loop Until IE.ReadyState = 4
While IE.Busy
DoEvents
Wend

If Action2 > 0 Then
    IE.Document.All("ddlAction").Value = Action2
    IE.Document.All("ddlAction").FireEvent ("onchange")
    If Action2 = 5 Then
        IE.Document.All("txtOther").Focus
        IE.Document.All("txtOther").Value = ActionOth2
    End If
IE.Document.All("txtOutcome").Value = Outcome2
IE.Document.All("btnactionsubmit").FireEvent ("onclick")
End If

Do
Loop Until IE.ReadyState = 4
While IE.Busy
DoEvents
Wend

If Action3 > 0 Then
IE.Document.All("ddlAction").Value = Action3
IE.Document.All("ddlAction").FireEvent ("onchange")
    If Action3 = 5 Then
        IE.Document.All("txtOther").Focus
        IE.Document.All("txtOther").Value = ActionOth3
    End If
IE.Document.All("txtOutcome").Value = Outcome3
IE.Document.All("btnactionsubmit").FireEvent ("onclick")
End If

Do
Loop Until IE.ReadyState = 4
While IE.Busy
DoEvents
Wend

If Action4 > 0 Then
IE.Document.All("ddlAction").Value = Action4
IE.Document.All("ddlAction").FireEvent ("onchange")
    If Action4 = 5 Then
        IE.Document.All("txtOther").Focus
        IE.Document.All("txtOther").Value = ActionOth4
    End If
IE.Document.All("txtOutcome").Value = Outcome4
IE.Document.All("btnactionsubmit").FireEvent ("onclick")
End If

Action1 = IIf(Me.cbo_Action1 = "buy", 1, _ IIf(Me.cbo_Action1 = "sell", 2, _ IIf(Me.cbo_Action1 = "trade", 3, _ IIf(Me.cbo_Action1 = "hold", 4, _ IIf(Me.cbo_Action1 = "Other", 5, 0))))) Action2 = IIf(Me.cbo_Action2 = "buy", 1, _ IIf(Me.cbo_Action2 = "sell", 2, _ IIf(Me.cbo_Action2 = "trade", 3, _ IIf(Me.cbo_Action2 = "hold", 4, _ IIf(Me.cbo_Action2 = "other", 5, 0))))) Action3 = IIf(Me.cbo_Action3 = "buy", 1, _ IIf(Me.cbo_Action3 = "sell", 2, _ IIf(Me.cbo_Action3 = "trade", 3, _ IIf(Me.cbo_Action3 = "hold", 4, _ IIf(Me.cbo_Action3 = "Other", 5, 0))))) Action4 = IIf(Me.cbo_Action4 = "buy", 1, _ IIf(Me.cbo_Action4 = "sell", 2, _ IIf(Me.cbo_Action4 = "trade", 3, _ IIf(Me.cbo_Action4 = "hold", 4, _ IIf(Me.cbo_Action4 = "other", 5, 0))))) IE.Document.All("ddlAction").Value = Action1 IE.Document.All("ddlAction").FireEvent ("onchange") Do Loop Until IE.ReadyState = 4 While IE.Busy DoEvents Wend If Action1 = 5 Then IE.Document.All("txtOther").Focus IE.Document.All("txtOther").Value = ActionOth1 End If IE.Document.All("txtOutcome").Value = Outcome1 IE.Document.All("btnactionsubmit").FireEvent ("onclick") Do Loop Until IE.ReadyState = 4 While IE.Busy DoEvents Wend If Action2 > 0 Then IE.Document.All("ddlAction").Value = Action2 IE.Document.All("ddlAction").FireEvent ("onchange") If Action2 = 5 Then IE.Document.All("txtOther").Focus IE.Document.All("txtOther").Value = ActionOth2 End If IE.Document.All("txtOutcome").Value = Outcome2 IE.Document.All("btnactionsubmit").FireEvent ("onclick") End If Do Loop Until IE.ReadyState = 4 While IE.Busy DoEvents Wend If Action3 > 0 Then IE.Document.All("ddlAction").Value = Action3 IE.Document.All("ddlAction").FireEvent ("onchange") If Action3 = 5 Then IE.Document.All("txtOther").Focus IE.Document.All("txtOther").Value = ActionOth3 End If IE.Document.All("txtOutcome").Value = Outcome3 IE.Document.All("btnactionsubmit").FireEvent ("onclick") End If Do Loop Until IE.ReadyState = 4 While IE.Busy DoEvents Wend If Action4 > 0 Then IE.Document.All("ddlAction").Value = Action4 IE.Document.All("ddlAction").FireEvent ("onchange") If Action4 = 5 Then IE.Document.All("txtOther").Focus IE.Document.All("txtOther").Value = ActionOth4 End If IE.Document.All("txtOutcome").Value = Outcome4 IE.Document.All("btnactionsubmit").FireEvent ("onclick") End If