Ms access 在Ms Access或Excel中的一个字段中按顺序编号

Ms access 在Ms Access或Excel中的一个字段中按顺序编号,ms-access,Ms Access,我试图创建一个查询,当我提供最小和最大范围时,它可以在一个字段中为我提供一个数字列表。 例如,最小值=30 最大值=35 结果:30,31,32,33,34,35 结果应该在一个字段中 Farrukh Khan您可以使用如下函数: Public Function ListOfNumbers(ByVal FirstValue As Integer, ByVal LastValue As Integer) As String Dim NumberList() As String

我试图创建一个查询,当我提供最小和最大范围时,它可以在一个字段中为我提供一个数字列表。 例如,最小值=30 最大值=35 结果:30,31,32,33,34,35

结果应该在一个字段中


Farrukh Khan

您可以使用如下函数:

Public Function ListOfNumbers(ByVal FirstValue As Integer, ByVal LastValue As Integer) As String

    Dim NumberList()    As String
    Dim Index           As Integer

    ReDim NumberList(FirstValue To LastValue)

    For Index = FirstValue To LastValue
        NumberList(Index) = CStr(Index)
    Next

    ListOfNumbers = Join(NumberList, ",")

End Function

我认为古斯塔夫提供的答案是一个开始。这就是需要修改以获得所需内容的方式

首先,在标准模块中创建VBA用户定义函数。这样,

Public Function ListOfNumbers(FirstValue As Long, LastValue As Long) As String
    Dim Index As Integer, tmpStr As String

    If LastValue < FirstValue Then
        ListOfNumbers = vbNullString
        Exit Function
    End If

    For Index = FirstValue To LastValue
        tmpStr = tmpStr & Index & ", "
    Next

    ListOfNumbers = Left(tmpStr, Len(tmpStr) - 2)
End Function

你想要什么?您想要sql语句吗?
PARAMETERS 
    MinVal Long, 
    MaxVal Long;
SELECT 
    ListOfNumbers(MinVal, MaxVal);