1

テーブル フィールド '電話番号' があり、テーブル内のデータはデータベース内の Excel ファイル リンクを介してインポートされます。ユーザーが電話番号を入力する方法がわからないため、フィールドのデータ型はテキストです (国コードがある場合と国コードがない場合があります)。

テーブルが毎回更新されるか、データがテーブルにインポートされると、「電話番号」フィールドをフォーマットしたいと思います。[国コード]-[ローカルコード]-[電話番号] の形式。

更新クエリを作成するか、このフィールドを更新する VBA コードを作成するか、どうすればよいかわかりません。この点でどんな助けにも感謝します。

4

1 に答える 1

1

一般に、データベース フィールドでは、電話番号を数値形式のみで維持することをお勧めします (つまり、括弧、ダッシュなどを使用しないことを意味します)。これにより、データの安定性が向上し、出力時の将来の書式設定が改善/容易になります。最も推奨される方法は、指定された数値から数値以外の文字をすべて取り除き、その値を保存することです。

この情報をデータベースに入れる前に Excel シートで作業している場合は、電話番号を含む列を書式設定して、すべてを 800-555-1212 または (888) 222 のように 1 つの数値に変換するだけです。 -1515 は 8005551212 と 8882221515 になります。これは、Excel に組み込まれている既存のセルの書式設定オプションを使用して行うことができます。または、フィールドに値があるときにトリガーする簡単な VBA コードをオンザフライで行うこともできます。

EDIT #1 (超シンプルな機能)

Public Function numOnly(sToClean As String) As String
    Const NUM_CHARS = "0123456789"
    Dim lChar As Long
    Dim sResult As String
    For lChar = 1 To Len(sToClean)
        If InStr(1, NUM_CHARS, Mid$(sToClean, lChar, 1)) > 0 Then
            'Found a numeric character
            sResult = sResult + Mid$(sToClean, lChar, 1)
        End If
    Next
    'Return the result
    numOnly = sResult
End Function

編集#2(より多くの機能の高度なバージョン)

Option Explicit 

Public Function stripChars(CheckStr As String, Optional KillNums As Boolean = False, Optional AllowedChars As String, Optional NotAllowed As String) 

     ' CheckStr = the string being evaluated
     ' KillNums [True/False] = remove numbers/remove non-numeric
     ' AllowedChars = what to allow even if you are removing non-numeric (aka KillNums=False) [ex. "$,."] or removing numbers (aka KillNums=True) [ex. "6,9"]
     ' NotAllowed = override characters never allowed and processed before any other option (meaning if you have it in both allow/not allow - not allow takes precedence
     ' NOTE: AllowedChars and NotAllowed arguments are *not* case-sensitive

    Dim Counter As Long 
    Dim TestChar As String 
    Dim TestAsc As Long 

     ' Loop through characters
    For Counter = 1 To Len(CheckStr) 
         ' Get current character and its ANSI number
        TestChar = Mid(CheckStr, Counter, 1) 
        TestAsc = Asc(TestChar) 

         ' Test first to see if current character is never allowed
        If InStr(1, NotAllowed, TestChar, vbTextCompare) > 0 Then 
             ' do nothing

             ' If current character is in AllowedChars, keep it
        ElseIf InStr(1, AllowedChars, TestChar, vbTextCompare) > 0 Then 
            stripChars = stripChars & TestChar 

             ' If KillNums=True, test for not being in numeric range for ANSI
        ElseIf KillNums Then 'only allow non-numbers
            If TestAsc < 48 Or TestAsc > 57 Then 
                stripChars = stripChars & TestChar 
            End If 

             ' If KillNums=False, test for being in numeric ANSI range
        Else 'only allow numbers
            If TestAsc >= 48 And TestAsc <= 57 Then 
                stripChars = stripChars & TestChar 
            End If 
        End If 
    Next 

End Function 

これらのいずれかを Excel のモジュール ( Alt+ F11) または Access フォームにドロップできます。

于 2013-09-14T16:59:04.063 に答える