MS Access:使用VBA将字符串从文本框拆分为其他文本框

卡塞姆

我想在Access中使用vba将文本框中的字符串拆分为多个文本框,每个框包含一个单词

所以说文本框名称text1,它包含一个字符串,例如hello I'm asking a question

我想将该字符串拆分为文本框,所以就像

text2 = hello
text3 = I'm
text4 = asking
 ...etc 

有一个简单的方法吗?

亨多格

这是一个整洁的解决方案:ParseWord()函数

下面的代码有点冗长,但是一旦实现就易于使用。该代码的作用是在您的数据库项目中创建一个函数。该函数称为:ParseWord()。它不是内置函数。因此,为什么需要将代码添加到数据库项目中的模块。

Function ParseWord(varPhrase As Variant, ByVal iWordNum As Integer, Optional strDelimiter As String = " ", _
    Optional bRemoveLeadingDelimiters As Boolean, Optional bIgnoreDoubleDelimiters As Boolean) As Variant
On Error GoTo Err_Handler 'I COMMENTED THIS OUT AND THE REFERENCE AT THE BOTTOM
    'Purpose:   Return the iWordNum-th word from a phrase.
    'Return:    The word, or Null if not found.
    'Arguments: varPhrase = the phrase to search.
    '           iWordNum = 1 for first word, 2 for second, ...
    '               Negative values for words form the right: -1 = last word; -2 = second last word, ...
    '               (Entire phrase returned if iWordNum is zero.)
    '           strDelimiter = the separator between words. Defaults to a space.
    '           bRemoveLeadingDelimiters: If True, leading delimiters are stripped.
    '               Otherwise the first word is returned as null.
    '           bIgnoreDoubleDelimiters: If true, double-spaces are treated as one space.
    '               Otherwise the word between spaces is returned as null.
    'Author:    Allen Browne. http://allenbrowne.com. June 2006.
    Dim varArray As Variant     'The phrase is parsed into a variant array.
    Dim strPhrase As String     'varPhrase converted to a string.
    Dim strResult As String     'The result to be returned.
    Dim lngLen As Long          'Length of the string.
    Dim lngLenDelimiter As Long 'Length of the delimiter.
    Dim bCancel As Boolean      'Flag to cancel this operation.

    '*************************************
    'Validate the arguments
    '*************************************
    'Cancel if the phrase (a variant) is error, null, or a zero-length string.
    If IsError(varPhrase) Then
        bCancel = True
    Else
        strPhrase = Nz(varPhrase, vbNullString)
        If strPhrase = vbNullString Then
            bCancel = True
        End If
    End If
    'If word number is zero, return the whole thing and quit processing.
    If iWordNum = 0 And Not bCancel Then
        strResult = strPhrase
        bCancel = True
    End If
    'Delimiter cannot be zero-length.
    If Not bCancel Then
        lngLenDelimiter = Len(strDelimiter)
        If lngLenDelimiter = 0& Then
            bCancel = True
        End If
    End If

    '*************************************
    'Process the string
    '*************************************
    If Not bCancel Then
        strPhrase = varPhrase
        'Remove leading delimiters?
        If bRemoveLeadingDelimiters Then
            strPhrase = Nz(varPhrase, vbNullString)
            Do While Left$(strPhrase, lngLenDelimiter) = strDelimiter
                strPhrase = Mid(strPhrase, lngLenDelimiter + 1&)
            Loop
        End If
        'Ignore doubled-up delimiters?
        If bIgnoreDoubleDelimiters Then
            Do
                lngLen = Len(strPhrase)
                strPhrase = Replace(strPhrase, strDelimiter & strDelimiter, strDelimiter)
            Loop Until Len(strPhrase) = lngLen
        End If
        'Cancel if there's no phrase left to work with
        If Len(strPhrase) = 0& Then
            bCancel = True
        End If
    End If

    '*************************************
    'Parse the word from the string.
    '*************************************
    If Not bCancel Then
        varArray = Split(strPhrase, strDelimiter)
        If UBound(varArray) >= 0 Then
            If iWordNum > 0 Then        'Positive: count words from the left.
                iWordNum = iWordNum - 1         'Adjust for zero-based array.
                If iWordNum <= UBound(varArray) Then
                    strResult = varArray(iWordNum)
                End If
            Else                        'Negative: count words from the right.
                iWordNum = UBound(varArray) + iWordNum + 1
                If iWordNum >= 0 Then
                    strResult = varArray(iWordNum)
                End If
            End If
        End If
    End If

    '*************************************
    'Return the result, or a null if it is a zero-length string.
    '*************************************
    If strResult <> vbNullString Then
        ParseWord = strResult
    Else
        ParseWord = Null
    End If

Exit_Handler:
    Exit Function

Err_Handler: 'I COMMENTED OUT THESE 4 LINES
    Call LogError(Err.Number, Err.Description, "ParseWord()")
    Resume Exit_Handler
End Function

将功能添加到数据库中的模块后,就可以像内置功能一样在VBA代码中调用它。

示例(点击事件):

在此处输入图片说明

我还必须注释掉“ On Error GoTo Err_Handler”行,因为我没有设置这些行。(我在代码中引用了这些)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

ms-access 根据表单选项在文本框中生成字符串

来自分类Dev

MS Access Form-来自文本框的字符串,用于LIKE查询以过滤报告结果

来自分类Dev

VBA MS Access在文本框中更改值的范围为2个字符

来自分类Dev

MS Access填充文本框和组合框

来自分类Dev

MS Access VBA显示列表框计数到文本框

来自分类Dev

仅允许MS Access在文本框中输入字母

来自分类Dev

MS Access:如何用查询绑定表格的文本框?

来自分类Dev

Javascript根据文本框值从MS Access检索值

来自分类Dev

如何在MS Access中通过VBA填充文本框?

来自分类Dev

MS ACCESS VBA:Me.Dirty = False OnTimer事件还原编辑的文本框数据

来自分类Dev

将MS Access表单中的文本框设置为数组?

来自分类Dev

将MS Access表单中的文本框设置为数组?

来自分类Dev

如何将文本框绑定到MS Access中子窗体的链接子字段?

来自分类Dev

将查询记录计数到MS Access中的文本框

来自分类Dev

将文本框中的数据保存到 MS Access 数据库

来自分类Dev

如何在 MS Powerpoint 中使用带有 VBA 的文本框?

来自分类Dev

使用循环从表中选择多个记录以填充MS Access中的未绑定文本框

来自分类Dev

根据具有if条件的多个文本框表达式值将值分配给表单上的文本框-MS ACCESS

来自分类Dev

ms access-将光标在文本字段中的位置设置为仅在文本框为空时开始

来自分类Dev

列表框MS Access,项目添加到文本框

来自分类Dev

如何在 MS Access VBA 上将文本框注释值输入设置为表数据插入

来自分类Dev

MS Word中隐藏文本框的未知状态

来自分类Dev

MS访问,在VBA中编写查询,并在文本框中显示结果

来自分类Dev

使用vba从ms访问表单文本框中的多个excel文件中检索特定单元格值

来自分类Dev

不允许在所有文本框中输入特定字母以用于MS Access

来自分类Dev

MS Access:如何在筛选的搜索中筛选多个字段(文本框)

来自分类Dev

为什么我的MS Access文本框(RTF)缺少单词之间的空格?

来自分类Dev

在文本框中继续循环以从数据库,MS Access中搜索多个值

来自分类Dev

如何在MS Access中比较两个文本框?

Related 相关文章

  1. 1

    ms-access 根据表单选项在文本框中生成字符串

  2. 2

    MS Access Form-来自文本框的字符串,用于LIKE查询以过滤报告结果

  3. 3

    VBA MS Access在文本框中更改值的范围为2个字符

  4. 4

    MS Access填充文本框和组合框

  5. 5

    MS Access VBA显示列表框计数到文本框

  6. 6

    仅允许MS Access在文本框中输入字母

  7. 7

    MS Access:如何用查询绑定表格的文本框?

  8. 8

    Javascript根据文本框值从MS Access检索值

  9. 9

    如何在MS Access中通过VBA填充文本框?

  10. 10

    MS ACCESS VBA:Me.Dirty = False OnTimer事件还原编辑的文本框数据

  11. 11

    将MS Access表单中的文本框设置为数组?

  12. 12

    将MS Access表单中的文本框设置为数组?

  13. 13

    如何将文本框绑定到MS Access中子窗体的链接子字段?

  14. 14

    将查询记录计数到MS Access中的文本框

  15. 15

    将文本框中的数据保存到 MS Access 数据库

  16. 16

    如何在 MS Powerpoint 中使用带有 VBA 的文本框?

  17. 17

    使用循环从表中选择多个记录以填充MS Access中的未绑定文本框

  18. 18

    根据具有if条件的多个文本框表达式值将值分配给表单上的文本框-MS ACCESS

  19. 19

    ms access-将光标在文本字段中的位置设置为仅在文本框为空时开始

  20. 20

    列表框MS Access,项目添加到文本框

  21. 21

    如何在 MS Access VBA 上将文本框注释值输入设置为表数据插入

  22. 22

    MS Word中隐藏文本框的未知状态

  23. 23

    MS访问,在VBA中编写查询,并在文本框中显示结果

  24. 24

    使用vba从ms访问表单文本框中的多个excel文件中检索特定单元格值

  25. 25

    不允许在所有文本框中输入特定字母以用于MS Access

  26. 26

    MS Access:如何在筛选的搜索中筛选多个字段(文本框)

  27. 27

    为什么我的MS Access文本框(RTF)缺少单词之间的空格?

  28. 28

    在文本框中继续循环以从数据库,MS Access中搜索多个值

  29. 29

    如何在MS Access中比较两个文本框?

热门标签

归档