检查文本框中的最后一个单词

时间:2013-12-17 10:15:26

标签: vba ms-access ms-access-2007 access-vba

我有一个由文本框组成的访问表单,我需要检查它的最后一个单词,如果这个单词是多个单词之一(数组或表列)做一个动作,这个检查将在after_update事件中发生,像

Private Sub textbox_AfterUpdate()

 Dim txt As String
 Dim lastword As String

  txt = TextBox.Value

 lastword= Right(txt, Len(txt) - InStrRev(txt, " "))

 if lastword in (array() or column in a table) then

    ' do an action

 End If

 End Sub

我们也可以使用外部功能,你可以帮我吗?

2 个答案:

答案 0 :(得分:2)

看起来你已经得到了最后一个单词的功能......现在在数组和表格中搜索使用:

Function isInArray(stringToBeFound As String, arr As Variant) As Boolean
  IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function

Function isColumnName(stringToBeFound As String, tableName As String) As Boolean
    Dim db As Database
    Dim rs1 As DAO.Recordset
    Set db = CurrentDb()
    Set rs1 = db.OpenRecordset(tableName)
    isColumnName = False
    Dim fld As DAO.Field
    do until rs1.EOF
        if rs1.Fields.Item(0).Value = stringToBeFound then
            isColumnName = true
            exit loop
        end if
        rs1.moveNext
    loop
    Set fld = Nothing
End Function

用法:

if isInArray(lastWord, youArray) or isColumnName(lastWord, "yourTable")
    MsgBox "The word is already used!"
end if

答案 1 :(得分:1)

这样的事情怎么样:

Private Sub TextBox1_AfterUpdate()
    Dim txtStr As String
    Dim vWords, v
    txtStr = TextBox1.Text
    If InStr(txtStr, " ") > 0 Then
        txtStr = Right(txtStr, Len(txt) - InStrRev(txt, " "))
    End If
    vWords = Split("word1 word2 word3 word4", " ") ' fill vWords with the words you need
    For Each v In vWords
        If v = txtStr Then
            ' do an action
            Exit For
        End If
    Next
End Sub