阻止如果没有结束if

时间:2013-08-16 16:53:08

标签: ms-access if-statement access-vba

我对VBA很新。我试图计算一个向量的中位数。以下代码不断收到关于“如果没有结束则阻止”的警告。我试图更改“结束IF”的位置,但它导致了另一个警告“阻止结束,如果没有,”。您的意见将不胜感激。感谢。

Sub CalculateMedian()

    DoCmd.SetWarnings False

    Dim db As DAO.Database
    Dim onet As DAO.Recordset

    Dim Ocode As String
    Dim ag As DAO.Recordset
    Dim agMedian As Integer

    Set db = CurrentDb

    'select one variable in current database
    Set onet = db.OpenRecordset("SELECT DISTINCT ONetCode FROM Single WHERE LEN(ONetCode)>8")

    Do While Not onet.EOF

        'assigning value to a variable does not need a "SET"
        Ocode = onet.Fields("ONetCode")
        'any data meet the criterion--&Ocode& can vary
        Set ag = db.OpenRecordset("SELECT AG FROM Single WHERE ONetCode='" & Ocode & "' ORDER BY AG")

        'using .recordcount needs to use .movelast first
        ag.MoveLast
        ag.MoveFirst
        If ag.RecordCount Mod 2 = 1 Then
            agMedian = ((ag.RecordCount + 1) / 2)
            thecount = 0
            Do While Not ag.EOF
                thecount = thecount + 1
                If thecount = agMedian Then
                    'inset the result into a new table, and need to create a new table in advance
                    DoCmd.RunSQL ("INSERT INTO PCImedian(onetcode, agMedian) VALUES('" & Ocode & "'," & ag("AG") & ");")
                    Exit Do
        End If

       If ag.RecordCount Mod 2 = 0 Then
            agMedian = ag.RecordCount / 2
            thecount = 0
            Do While Not ag.EOF
                thecount = thecount + 1
                If thecount = agMedian Then
                    m1 = ag("AG")
                ElseIf thecount = agMedian + 1 Then
                    m2 = ag("AG")
                    DoCmd.RunSQL ("INSERT INTO PCImedian(onetcode, agMedian) VALUES('" & Ocode & "'," & ((m1 + m2) / 2) & ");")
                    Exit Do
        End If

    Loop

    DoCmd.SetWarnings True

End Sub

2 个答案:

答案 0 :(得分:1)

如果在第一个代码块中退出后,你似乎错过了一个结束。那里应该有2个,一个用于关闭最后一个if语句,一个用于关闭第一个块。

Sub CalculateMedian()

    DoCmd.SetWarnings False

    Dim db As DAO.Database
    Dim onet As DAO.Recordset

    Dim Ocode As String
    Dim ag As DAO.Recordset
    Dim agMedian As Integer

    Set db = CurrentDb

    'select one variable in current database
    Set onet = db.OpenRecordset("SELECT DISTINCT ONetCode FROM Single WHERE LEN(ONetCode)>8")

    Do While Not onet.EOF

        'assigning value to a variable does not need a "SET"
        Ocode = onet.Fields("ONetCode")
        'any data meet the criterion--&Ocode& can vary
        Set ag = db.OpenRecordset("SELECT AG FROM Single WHERE ONetCode='" & Ocode & "' ORDER BY AG")

        'using .recordcount needs to use .movelast first
        ag.MoveLast
        ag.MoveFirst
        If ag.RecordCount Mod 2 = 1 Then
            agMedian = ((ag.RecordCount + 1) / 2)
            thecount = 0
            Do While Not ag.EOF
                thecount = thecount + 1
                If thecount = agMedian Then
                    'inset the result into a new table, and need to create a new table in advance
                    DoCmd.RunSQL ("INSERT INTO PCImedian(onetcode, agMedian) VALUES('" & Ocode & "'," & ag("AG") & ");")


                   End If 'ends the If thecount = agMedian if statement -- will continue to iterate until EOF

                    Exit Do 'EOF hit.


        End If 'ends the If ag.RecordCount Mod 2 = 1 block

       If ag.RecordCount Mod 2 = 0 Then
            agMedian = ag.RecordCount / 2
            thecount = 0
            Do While Not ag.EOF
                thecount = thecount + 1
                If thecount = agMedian Then
                    m1 = ag("AG")
                ElseIf thecount = agMedian + 1 Then
                    m2 = ag("AG")
                    DoCmd.RunSQL ("INSERT INTO PCImedian(onetcode, agMedian) VALUES('" & Ocode & "'," & ((m1 + m2) / 2) & ");")
                    Exit Do
        End If 'thecount = agMedian if statement
        End If 'end ag.RecordCount Mod 2 = 0

    Loop

    DoCmd.SetWarnings True

End Sub

答案 1 :(得分:1)

代码缺少多个End If。而且还有2个Loop个缺失的陈述。

当代码足够复杂以至于挑选出块结束语句变得具有挑战性时,请复制该过程并基本上丢弃除块控制语句之外的所有内容。该方法从您当前的代码中留下了这个。

    Do While Not onet.EOF
        If ag.RecordCount Mod 2 = 1 Then
            Do While Not ag.EOF
                If thecount = agMedian Then
        End If
        If ag.RecordCount Mod 2 = 0 Then
            Do While Not ag.EOF
                If thecount = agMedian Then
                ElseIf thecount = agMedian + 1 Then
        End If
    Loop

这是我对你所需要的最好的猜测。我在其中几个语句中添加了注释,因为它可以帮助我正确匹配它们。

    Do While Not onet.EOF
        If ag.RecordCount Mod 2 = 1 Then
            Do While Not ag.EOF
                If thecount = agMedian Then
                End If ' thecount
            Loop ' Not ag.EOF
        End If ' ag.RecordCount Mod 2 = 1
        If ag.RecordCount Mod 2 = 0 Then
            Do While Not ag.EOF
                If thecount = agMedian Then
                ElseIf thecount = agMedian + 1 Then
                End If ' thecount
            Loop ' Not ag.EOF
        End If ' ag.RecordCount Mod 2 = 0
    Loop ' Not onet.EOF