2

我不知道这在 MS Access 中是否可行,但我想要做的是检测破折号 (-) 并在 SQL 语句和或逗号中使用 Between。

前任。我有一个名为“Books”的表,其中包含以下字段:BookID、Title、Subject。

现在我如何查询允许用户在文本框中输入值的 Books 表,例如:

1 or 1-5 or 1,3,4.

如果值为 1,则 sql 语句应为:

SELECT * FROM Books WHERE BookID = 1

如果值为 1-5 那么 sql 语句应该是:

SELECT * FROM Books WHERE BookID BETWEEN 1 And 5

如果值为 1,3,4 那么 sql 语句应该是:

SELECT * FROM Books WHERE BookID IN (1,3,4)
4

2 回答 2

3

从我已经拥有的东西上剪下来;

s = "SELECT * FROM Books WHERE BookID" & parseSarg("11,22,33")

使用;

Public Function parseSarg(str As String) As String
    Dim re As Object: Set re = CreateObject("vbscript.regexp")
    re.Global = True
    Select Case True
        '//is number
        Case reTest(re, "^\d+$", str):          parseSarg = " = " & str
        '//is number-number
        Case reTest(re, "^\d+-\d+$", str):      parseSarg = " BETWEEN " & Replace$(str, "-", " AND ")
        '//is number,number[,number]
        Case reTest(re, "^\d+(?:,\d+)*$", str): parseSarg = " IN (" & str & ")"
        '//is >number
        Case reTest(re, "^>\s*\d+$", str):      parseSarg = " >" & Mid$(str, 2)
        Case Else
            parseSarg = " = 0 AND 1=0"
    End Select
End Function

Function reTest(re As Object, pattern As String, value As String) As Boolean
    re.pattern = pattern
    reTest = re.Test(value)
End Function
于 2012-06-21T13:59:35.577 回答
0
SELECT Books.Title FROM Books WHERE Books.BookID > 1 AND Books.BookID < 5;
于 2012-06-21T13:56:33.980 回答