2013-12-17 39 views
0

我有一個由文本框組成的訪問表單,我需要檢查它的最後一個單詞,如果這個單詞是多個單詞之一(數組或表格列)做一個動作,並且該檢查將發生在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 

我們還我們一個外部功能,你能幫助我嗎?

+0

文本框中的單詞如何成爲表格的數組或列?這可能是我,但我不明白你的意思是... – Trace

+0

不,我有很多字來檢查,所以我會把他們放在一個數組中,首先我會提取文本字段中的最後一個單詞,我會比較這個單詞包含很多其他詞 – user1921704

+0

您只需要一些功能就可以做到這一點。最重要的是要考慮到一定的驗證。例如,如果最後一個詞出現在沒有空格的點或逗號後面,會發生什麼?你有你的第一個錯誤。 – Trace

回答

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 
+0

代替它謝謝你,你的函數運行良好,但你必須修改它,因爲在iscolumnname()的情況下,它只適用於表中的字段名稱,而我需要它的工作第一列記錄不是字段名稱,同時你必須添加Dim db As Database和 Dim rs1作爲DAO.Recordset在這個函數的開頭,即時消息等待你的修改開始使用它,謝謝你的很大的努力 – user1921704

+0

編輯正如你所希望的那樣......希望它現在能夠運作 – Manu

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 
+0

謝謝,但我不知道如何使用它訪問 – user1921704

+0

你說你有你的數據在一個「數組或表列」。那麼哪一個是真的? – MiVoth

+0

是的,我的意思是訪問表不是Excel表,我可以創建外部Excel表,但我不知道如何在這種情況下使用它 – user1921704

相關問題