ASP嵌套循环返回错误

时间:2013-06-29 06:01:58

标签: ms-access asp-classic

我试图为菜单创建一个嵌套循环,但它返回错误。请有人帮我解决这个问题。甚至可以确定循环出错的地方。

Error Type:
Microsoft VBScript compilation (0x800A040E)
'loop' without 'do'
/asp/default.asp, line 64
loop until RS.EOF

守则

msql = "SELECT * FROM tblMenu ORDER BY [link_position] ASC"
Set RS = Server.CreateObject("ADODB.Recordset")
Set RSsub = Server.CreateObject("ADODB.Recordset")
RS.Open msql, Conn, 1,3
RSsub.Open msql

If Not RS.EOF Then
    Do
        Response.Write "<li><a href="""& RS("link")&""" title ="""& RS("link_title")&""">"& RS("link_name") &"</a>"

        If NOT RSsub.EOF Then
            Response.Write("<ul>" & vbcrlf)
            Do While NOT RSsub.EOF
                Response.Write(RSsub("sub_menu") & vbcrlf)
                RSsub.MoveNext
            Loop
            Response.Write("</ul>" & vbcrlf)
            RSsub.Close

            Response.Write("</li>" & vbcrlf)        
            RS.MoveNext
    loop until RS.EOF
End if

预期输出

<li><a href="some-link" title="">Record from DB</a>
    <ul>
        <li><a href="some-link">Records from DB</a></li>
    </ul>
</li>

2 个答案:

答案 0 :(得分:4)

End If,你遗漏了If NOT RSsub.EOF Then

答案 1 :(得分:2)

几个语法错误。这是清理过的代码。

    msql = "SELECT * FROM tblMenu ORDER BY [link_position] ASC"
    RS = Server.CreateObject("ADODB.Recordset")
    RSsub = Server.CreateObject("ADODB.Recordset")
    RS.Open(msql, Conn, 1, 3)
    RSsub.Open(msql)

    If Not RS.EOF Then
        Do
            Response.Write("<li><a href=""" & RS("link") & """ title =""" & RS("link_title") & """>" & RS("link_name") & "</a>")

            If Not RSsub.EOF Then
                Response.Write("<ul>" & vbcrlf)
                Do While Not RSsub.EOF
                    Response.Write(RSsub("sub_menu") & vbcrlf)
                    RSsub.MoveNext()
                Loop
                Response.Write("</ul>" & vbcrlf)
                RSsub.Close()
            End If

            Response.Write("</li>" & vbcrlf)
            RS.MoveNext()
        Loop Until RS.EOF
    End If

更新:

根据您发布的图片,表格中的sub_menu只是一堆html li元素,您可以直接附加到响应中。您不需要单独的记录集来读取子菜单。试试这段代码:

msql = "SELECT * FROM tblMenu ORDER BY [link_position] ASC"
RS = Server.CreateObject("ADODB.Recordset")
RS.Open(msql, Conn, 1, 3)

Do
    Response.Write("<li><a href=""" & RS("link") & """ title =""" & RS("link_title") & """>" & RS("link_name") & "</a>" & vbcrlf)

    If Not String.IsNullOrEmpy(RS("sub_menu")) Then
        Response.Write("<ul class=""sub-menu"">" & vbcrlf)
        Response.Write(RS("sub_menu") & vbcrlf)
        Response.Write("</ul>" & vbcrlf)
    End If
    Response.Write("</li>" & vbcrlf)
    RS.MoveNext()
Loop Until RS.EOF