2016-04-22 22 views
0

我想在一個文本字符串分割成許多文本框中包含使用VBA一個詞在訪問每個盒子MS訪問:使用VBA從一個文本框將一個字符串分解成其他文本框

所以讓我們說文本框名稱text1,它包含像hello I'm asking a question

我想這個字符串分割成文本框,以便它會像

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

有一種簡單的方法來做到這串?

+0

是的,有。將文本框的值按空格分割成一個數組,然後每隔一段時間通過它。循環的核心應該CreateControl(見ms幫助)一個新的文本框,添加第n個數組元素作爲它的文本,這就是它 – 2016-04-22 20:48:23

回答

1

這裏找到了一個巧妙的解決辦法:ParseWord() Function

下面的代碼是一個有點冗長,但很容易使用,一旦實現。 這段代碼的功能是在數據庫項目中創建一個函數。這個函數被稱爲: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代碼中調用它。

例子(click事件):

enter image description here

我也不得不註釋掉「對錯誤轉到Err_Handler」行,因爲我沒有那些成立。 (我在代碼中引用了這些內容)

+0

非常感謝你的代碼像魅力一樣工作如預期的那樣工作我沒有錯誤處理程序也是如此我再次刪除這些行謝謝:) –

+0

不是我的代碼。信貸該網站。我剛剛發現了這個代碼,如果我需要它,我一定會記住它。樂意效勞! – Huntdogg

+0

即使它不是你的,它幫助了我很多我一直在試圖找出這麼多天,我甚至沒有運氣搜索它,所以謝謝你的幫助 –

3

訪問具有內置的split()命令,可以執行此操作。

所以,這個代碼將工作:

Dim v  As Variant 
Dim sOne  As Variant 
Dim i  As Integer 

v = Split(Me.TextBox0, " ") 
i = 0 
For Each sOne In v 
    i = i + 1 
    Me("text" & i) = sOne 
Next 

所以上面將採取從窗體上textbox0字符串,並把說的3個值到文本框1〜3

那麼大的重擊的代碼是不需要的。

相關問題