在 Access 查询中使用带有 IIf 的循环

Using loop with IIf in Access query

我正在尝试计算 Access 2010 查询中的一个字段,该查询采用卷数并使用默认值 URL(对于值 1)或循环次数与要创建的卷数一样多一连串的URL(如果都在一起也没关系,最后会在报告中输出文本)。

如果 1 卷,http://blah.com/1. If 2 volumes, http://blah.com/2/vol1, http://blah.com/2/vol2 .

我在想这样的事情:

=IIf([volumes]="1",[URL],for i=1 to [volumes] output [URL] & "/vol[i], " next)

但我不知道我是否需要卷变量或如何分配它或如何生成输出。非常感谢任何关于如何解决这个问题的建议。

URL                 Item    volumes
http://blah.com/1   Book1   1
http://blah.com/2   Book2   2
http://blah.com/3   Book3   10

谢谢, 桑迪

一个简单的循环即可:

Public Function PrintUrlVol(ByVal Url As String, ByVal Volume As Integer) As String

    Dim UrlList As String
    Dim Index   As Integer

    If Volume = 1 Then
        UrlList = Url
    ElseIf Volume > 1 Then
        For Index = 1 To Volume
            If UrlList <> "" Then
                UrlList = UrlList & ", "
            End If
            UrlList = UrlList & Url & "/vol" & CStr(Index)
        Next
    End If

    PrintUrlVol = UrlList

End Function