我如何在打开表单时提示输入多个字符串?

问题描述 投票:0回答:1

在我的Access DB中,我制作了一个表单,要求用户打开输入。用户可以输入多个用逗号分隔的itemID(数字),查询将返回包含所有相应项的表。

我的ID现在也包含字母(不是我的决定!),因此我需要将输入类型更改为String。有什么办法可以使用以下VBA代码库像以前一样使用Strings来工作?

Private Sub Form_Open(Cancel As Integer)
Dim m As String

m = InputBox("Please enter itemIDs, seperated by commas", "itemID")
If m <> "" Then
      Me.Filter = "itemID in (" & m & ")"
      Me.FilterOn = True
Else
     Me.FilterOn = False
End If
End Sub

感谢您的帮助!干杯!

vba ms-access access-vba
1个回答
0
投票

如果您将过滤器用引号引起来,您的代码将起作用。因此:

   Private Sub Form_Open(Cancel As Integer)
   Dim m As String

   m = InputBox("Please enter itemIDs, seperated by commas", "itemID")
   If m <> "" Then
         Me.Filter = "itemID in ('" & m & "')" 'Notice the single quotes
         Me.FilterOn = True
   Else
         Me.FilterOn = False
   End If
   End Sub

然而,麻烦的是,多个itemID将无法正确过滤,因为您需要将所有ID都用引号引起来。喜欢:

    Me.Filter = "itemID in ('ID001', 'ID002', 'etc')"

因此,如果用户输入中包含讨厌的昏迷,则需要先添加多余的引号。您可以执行以下操作:

   Private Sub Form_Open(Cancel As Integer)
   Dim m As String

   m = InputBox("Please enter itemIDs, seperated by commas", "itemID")
   If m <> "" Then
        'as a quick example, if m contains comas we surround them by quotes
         If InStr(1, m, ",") <> 0 Then m = Replace(m, ",", "','")
        'however this would only work if the user inputs the ids like "id1,id2"
        'it fails to account for a space after the comas like "id1, id2" 
         Me.Filter = "itemID in ('" & m & "')"
         Me.FilterOn = True
   Else
         Me.FilterOn = False
   End If

   End Sub

希望这会为您指明正确的方向!

© www.soinside.com 2019 - 2024. All rights reserved.