领先和尾随空间

时间:2016-01-20 12:37:15

标签: ms-access access-vba

我想出了一个奇怪的情况。我有一个从网页输入表单的字符串。我注意到当我尝试应用过滤器时,字符串没有按预期运行。

问题的关键在于我如何以不同的方式查看字符串。

Form View - "523548"
Datasheet View - "  523548"
Raw Sql - " 523548"

实际上,当我查看数据表值时,它显示为"523548 ",但复制为" 523548"

Asc(左(字符串),1)告诉我第一个字符是Chr9(Tab键)

我真的很难发现为什么会这样,或者更重要的是,我能做些什么来纠正它。

谢谢!

戴夫。

3 个答案:

答案 0 :(得分:0)

好吧,调整您的例程,不要包含新条目的标签。

您可以使用替换调整旧条目:

NewValue = Replace([OldValue], Chr(9), "")

答案 1 :(得分:0)

我在这里使用Trim()功能。虽然该链接适用于Excel,但它与Access中的语法相同。

将值写入表时使用此功能,您也可以使用此功能清理当前数据。这应该删除幻像标签以及空格。

答案 2 :(得分:0)

感谢您的提示。我发现了这个功能并添加了几个项目以满足我的需求。

' Strip Illegal Characters
' http://www.utteraccess.com/wiki/index.php/Strip_Illegal_Characters
' Code courtesy of UtterAccess Wiki
' Licensed under Creative Commons License
' http://creativecommons.org/licenses/by-sa/3.0/
'
' You are free to use this code in any application,
' provided this notice is left unchanged.
'
' rev  date                          brief descripton
' 1.0  2010-10-23                    Writing files to disk that contain illegal file characters can cause sometimes obscure error message(s)
'
Public Function fStripIllegal(strCheck As String, Optional strReplaceWith As String = "") As String

On Error GoTo StripIllErr
'illegal file name characters included in default string are    ? [ ] / \ = + < > :; * " , '

Dim intI As Integer
Dim intPassedString As Integer
Dim intCheckString As Integer
Dim strChar As String
Dim strIllegalChars As String
Dim intReplaceLen As Integer

If IsNull(strCheck) Then Exit Function

strIllegalChars = "?[]/\=+<>:;,*" & Chr(34) & Chr(39) & Chr(32) & Chr(9)  'add/remove characters you need removed to this string

intPassedString = Len(strCheck)
intCheckString = Len(strIllegalChars)

intReplaceLen = Len(strReplaceWith)

If intReplaceLen > 0 Then   'a character has been entered to use as the replacement character

   If intReplaceLen = 1 Then   'check the character itself isn't an illegal character

       If InStr(strIllegalChars, strReplaceWith) > 0 Then
           MsgBox "You can't replace an illegal character with another illegal character", _
                  vbOKOnly + vbExclamation, "Invalid Character"
           fStripIllegal = strCheck
           Exit Function
       End If

   Else   'only one replacement character allowed

       MsgBox "Only one character is allowed as a replacement character", _
              vbOKOnly + vbExclamation, "Invalid Replacement String"
       fStripIllegal = strCheck
       Exit Function

   End If
End If

If intPassedString < intCheckString Then

   For intI = 1 To intCheckString
       strChar = Mid(strIllegalChars, intI, 1)
       If InStr(strCheck, strChar) > 0 Then
           strCheck = Replace(strCheck, strChar, strReplaceWith)
       End If
   Next intI

Else

   For intI = 1 To intPassedString
       strChar = Mid(strIllegalChars, intI, 1)
       If InStr(strCheck, strChar) > 0 Then
           strCheck = Replace(strCheck, strChar, strReplaceWith)
       End If
   Next intI

End If

fStripIllegal = Trim(strCheck)

StripIllErrExit:
Exit Function

StripIllErr:
MsgBox "The following error occured: " & err.Number & vbCrLf _
    & err.Description, vbOKOnly + vbExclamation, "Unexpected Error"

fStripIllegal = strCheck

Resume StripIllErrExit

End Function