拆分字符串

时间:2017-06-05 17:44:45

标签: vba ms-access

我正在使用VBA运行access2010表单。有一些文本框我使用拆分功能划分为单个字符串,然后在单击搜索按钮时将这些单独的字符串导入搜索表单。如果我在每个文本框中输入文本,则按预期导入字符串,但如果没有或甚至不是所有文本框都输入了值,则会出现错误:无效使用Null。如果我在每个框中输入一个值并进行搜索,我可以返回上一个表单并清除文本框,然后向上拉搜索表单而不会出现问题。以下是一些代码:

Private Sub Advanced_Search_btn_Click()
On Error GoTo Advanced_Search_btn_Click_Err
    Dim TitleArray() As String
    Dim TitleLength As Integer

    TitleArray() = Split(Title)     'Splits the textbox named title
    TitleLength = UBound(TitleArray) - LBound(TitleArray)
    If Trim(Title.Value & vbNullString) = vbNullString Then    'This is supposed 
        'to make nothing happen if nothing is in the text box, could be the problem
        Call TitleNone
    ElseIf TitleLength >= 5 Then
        Call Title5
    ElseIf TitleLength = 4 Then
        Call Title4
    ElseIf TitleLength = 3 Then
        Call Title3
    ElseIf TitleLength = 2 Then
        Call Title2
    ElseIf TitleLength = 1 Then
        Call Title1
    ElseIf TitleLength = 0 Then
        Call Title0
    Else
        Call TitleNone   'Sub to do nothing
    End If

    Sub TitleNone()
    End Sub

    TitleArray() = Split(Title)   'Example of string import
    Forms![Advanced Search]![Title] = TitleArray(0)
    Forms![Advanced Search]![Title Keyword 2] = TitleArray(1)
    Forms![Advanced Search]![Title Keyword 3] = TitleArray(2)
    Forms![Advanced Search]![Title Keyword 4] = TitleArray(3)
    Forms![Advanced Search]![Title Keyword 5] = TitleArray(4)
    Forms![Advanced Search]![Title Keyword 6] = TitleArray(5)
End Sub

无论如何,我希望这是足够的信息,看看我做错了什么,谢谢。

编辑:我提出了一种方法来使其工作,我只是添加额外的潜艇来完成这项工作,发布给遇到类似问题的人:     Private Sub Advanced_Search_btn_Click()     Dim TitleArray()As String     昏暗的TitleLength作为整数     DoCmd.OpenForm“高级搜索”,acNormal,“”,“”,acNormal

If Not IsNull(Me.Title.Value) Then
TitleArray() = Split(Title)
TitleLength = UBound(TitleArray) - LBound(TitleArray)
Call TitleCall
End If

Sub TitleCall()
Dim TitleArray() As String
Dim TitleLength As Integer
TitleArray() = Split(Title)
TitleLength = UBound(TitleArray) - LBound(TitleArray)
If TitleLength >= 5 Then
Call Title5
ElseIf TitleLength = 4 Then
Call Title4
ElseIf TitleLength = 3 Then
Call Title3
ElseIf TitleLength = 2 Then
Call Title2
ElseIf TitleLength = 1 Then
Call Title1
ElseIf TitleLength = 0 Then
Call Title0
Else
Call TitleNone
End If
End Sub

Sub Title5()
Dim TitleArray() As String
TitleArray() = Split(Title)
    Forms![Advanced Search]![Title] = TitleArray(0)
    Forms![Advanced Search]![Title Keyword 2] = TitleArray(1)
    Forms![Advanced Search]![Title Keyword 3] = TitleArray(2)
    Forms![Advanced Search]![Title Keyword 4] = TitleArray(3)
    Forms![Advanced Search]![Title Keyword 5] = TitleArray(4)
    Forms![Advanced Search]![Title Keyword 6] = TitleArray(5)

Sub TitleNone()
End Sub

End Sub

0 个答案:

没有答案