【问题标题】:how to search a word in a two-dimensional array based on my textbox with vba code in MS Access form如何根据我的文本框在二维数组中搜索一个单词,并在 MS Access 表单中使用 vba 代码
【发布时间】:2020-06-23 06:28:35
【问题描述】:

我有一个这样的数组:

jack,33500;luci,36120;andro,33941;jenifer,25980;ivanka,12500

我想在我的文本框中写 33941 时找到例如 "andro"
注意 em>:我发现很多其他编程方法的代码,但我需要获取在MS Access中使用的代码。

我的代码如下所示。 当我单击列表框时,我的数组已构建,同时我希望显示结果,但它显然会返回错误(子字符串超出范围) 代码:

Private Sub txNumber1_Click()

    Dim CurMobile As String, curMesssage As String, strMobiles As String, strMesssages As String, strMM As String
    Dim rowNum As Variant

    Dim TestArrayMobile() As String
    Dim TestArrayMessage() As String
    Dim arr() As String
    Dim i As Integer
    Dim j As Integer

    ' Get selected phone numbers & names
    With txNumber1
        For Each rowNum In .ItemsSelected
             CurMobile = .Column(1, rowNum)
             curMesssage = .Column(3, rowNum)
             strMM = strMM + ";" + curMesssage + "," + CurMobile
             arr() = Split(strMM, ";")

               For j = LBound(arr) To UBound(arr)
'               MsgBox arr(j, 1), , arr(j, 0)
                MsgBox arr(j, 1)
'                      If StrComp(.Column(1, rowNum), arr(j, 1), vbTextCompare) = 0 Then
'                        MsgBox arr(j, 0)
'                        Exit For
'                      End If
                Next j

        Next rowNum
    End With

End Sub

【问题讨论】:

  • 为什么是数组?没有包含此信息的表格吗?如果您想“搜索”一组虚拟数据,可能应该使用集合对象而不是数组。使用数组,必须循环遍历数组元素,直到遇到匹配。
  • ma 数据通过 Web 服务以上述数组的形式返回,我需要将其中一个数字分开并从表中保存名称和其他内容并将它们插入到表中
  • 数据是作为数组接收还是只是一个字符串?您显示的内容看起来像一个字符串。
  • 嗨,我把我的代码放在上面了。

标签: arrays ms-access search


【解决方案1】:

考虑:

Sub GetData(stringToBeFound As String, arr As Variant)
Dim i As Long
For i = LBound(arr) To UBound(arr)
    If StrComp(stringToBeFound, arr(i, 1), vbTextCompare) = 0 Then
        Debug.Print arr(i, 0)
        Exit For
    End If
Next i
End Sub

代替Debug.Print,可以将数据写入表单上的控件或运行INSERT action SQL。

【讨论】:

  • 我已经为您的代码制作了 cmets。编辑问题以显示数据如何在列表框中列出的示例。在您的问题中发布您的代码,而不是答案。
猜你喜欢
  • 1970-01-01
  • 2020-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-30
  • 2013-01-31
  • 1970-01-01
相关资源
最近更新 更多