如果Else阻止错误

时间:2013-05-30 08:31:37

标签: vba access-vba

我遇到这个If ElseIf阻塞的麻烦,我无法弄清楚错误的位置。

我得到的信息是“Else without if”。

如果有任何帮助或指导,我很高兴使用if。

错误行:

ElseIf booSM = True And _
       booInv = True And _
       booHOP = False _
       Then strTo = Me.txtServiceManager.Column(1) & ";" & Me.txtInvestigator.Column(1)

完整代码:

Dim txtScan As String
Dim strTo As String
Dim booSM As Boolean
Dim booInv As Boolean
Dim booHOP As Boolean

Me.Refresh
txtScan = Me.txtScanLocation
booSM = booComDocSMSent
booInv = booComDocInvSent
booHOP = booComDocHOPSent

If booSM = True And _
   booInv = True And _
   booHOP = True _
   Then strTo = Me.txtServiceManager.Column(1) & ";" & Me.txtInvestigator.Column(1) & ";" &  Me.txtHop.Column(1)
ElseIf booSM = True And _
       booInv = True And _
       booHOP = False _
       Then strTo = Me.txtServiceManager.Column(1) & ";" & Me.txtInvestigator.Column(1)
ElseIf booSM = True And _
       booInv = False And _
       booHOP = True _
       Then strTo = Me.txtServiceManager.Column(1) & ";" & Me.txtHop.Column(1)
ElseIf booSM = False And _
       booInv = True And _
       booHOP = True _
       Then strTo = Me.txtInvestigator.Column(1) & ";" & Me.txtHop.Column(1)
ElseIf booSM = True And _
       booInv = False And _
       booHOP = False _
       Then strTo = Me.txtServiceManager.Column(1)
ElseIf booSM = False And _
       booInv = True And _
       booHOP = False _
       Then strTo = Me.txtInvestigator.Column(1)
ElseIf booSM = False And _
       booInv = False And _
       booHOP = True _
       Then strTo = Me.txtHop.Column(1)
End If

2 个答案:

答案 0 :(得分:2)

查看If statement代码部分的以下逻辑:

Sub Logic()
'Attempt 1st
    If a = 1 And _
        b = 1 _
        Then C = 1 'end of if statement

'Attempt 2nd
    If a = 1 And _
        b = 1 _
        Then _
        C = 1 'end of if statement

'Attempt 3rd
    If a = 1 And _
        b = 1 _
        Then
        C = 1 'and continuation below
    ElseIf a = 1 And _
        b = 1 _
        Then
        'something here
    End If
End Sub

写作if conditions check有三种不同的逻辑。在你的情况下你要么错过了一些_ underline marks,要么你应该将一些行移到下一行(Then关键词之后的代码)。

答案 1 :(得分:1)

我手边没有VBA,但看起来你的行结尾有问题

格式

if condition then
  statement
elesif conditon
  statement
end if 

您正在使用续行(_),格式如下

if condition then statement
elseif condition then statement
endif

所以

If booSM = True And booInv = True And booHOP = True Then 
   strTo = Me.txtServiceManager.Column(1) & ";" & Me.txtInvestigator.Column(1) & ";" &  Me.txtHop.Column(1)
ElseIf booSM = True And booInv = True And booHOP = False Then 
  strTo = Me.txtServiceManager.Column(1) & ";" & Me.txtInvestigator.Column(1)
End If

可能对你更好 或者如果你想保留一些多行格式

If booSM = True And _
   booInv = True And _
   booHOP = True Then 
       strTo = Me.txtServiceManager.Column(1) & ";" & Me.txtInvestigator.Column(1) & ";" &  Me.txtHop.Column(1)
ElseIf booSM = True And _
       booInv = True And _
       booHOP = False _
Then 
      strTo = Me.txtServiceManager.Column(1) & ";" & Me.txtInvestigator.Column(1)
End If