VBA, MS Access MS Access в примерах

Очистка строки от всех символов кроме ЦИФР

Public Function CleanPhoneNo(vDirtyNo As Variant) As Variant
'Очистка значения поля от всех символов кроме цифр (для последующего поиска например)
'Clearing the field value of all characters except digits (for later search, for example)
'--------------------------------------------------------------------
Dim s As String
Dim t As String
Dim i As Integer, x As Integer

On Error GoTo CleanPhoneNo_Err
    
    s = CStr(vDirtyNo)
    x = Len(s)
    For i = 1 To x
        t = Mid(s, i, 1)
        If IsNumeric(t) Then
            CleanPhoneNo = CleanPhoneNo & t
        End If
    Next i
    
CleanPhoneNo_Bye:
    Exit Function

CleanPhoneNo_Err:
    CleanPhoneNo = Null
    Err.Clear
    Resume CleanPhoneNo_Bye
End Function



Ещё вариант:

Private Sub YourTextFieldName_AfterUpdate()
Dim s As String
Dim t As String
Dim i As Integer

'Clearing the field value of all characters except digits
    s = Me.YourTextFieldName & ""
    Me.YourTextFieldName = Null
    For i = 1 To Len(s)
        t = Mid(s, i, 1)
        If IsNumeric(t) Then
            Me.YourTextFieldName = Me.YourTextFieldName & t
        End If
    Next i

' ... next code ...

End Sub
Назад ToTop
L.E. 24.09.2023
Рейтинг@Mail.ru