Share via

Query a table while ignoring special characters.

Anonymous
2017-02-06T19:47:52+00:00

Hello

I am trying to conduct a query through the use of a search form on a table that includes a list of phone numbers. These numbers were never standardized, so they look like the following:

5558909989

555-873-0909

555 233 5353

+55 555 594 5943

Say for example, I want to search the second number listed above. It will only appear in my results if I type "555-873-0909". How can I make it appear if I only type "5558730909". In other words, I want the query to ignore the "dashes" when searching through the table.

The query string below is what I have so far. But it does not work.

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2017-02-06T20:06:34+00:00

Add the following function into a standard VBA module

Public Function StripAllChars(strString As String) As String

'Return only numeric values from a string

'Source:

Dim lngCtr As Long

Dim intChar As Integer

    For lngCtr = 1 To Len(strString)

        intChar = Asc(Mid(strString, lngCtr, 1))

        If intChar >= 48 And intChar <= 57 Then

            StripAllChars = StripAllChars & Chr(intChar)

        End If

    Next lngCtr

End Function

Then, instead of querying your fields directly, add a new field

HomePhoneNo: StripAllChars([Home Phone])

and Search it.

Do the same for all your other phone number fields.

This is why it can be beneficial to only store the number, and not the formatting for such fields, and then use the Format control property in forms and reports to display them as you please for visualization.

Was this answer helpful?

0 comments No comments

10 additional answers

Sort by: Most helpful
  1. Anonymous
    2017-02-06T20:46:02+00:00

    I see. To do this, would I also need to add this field to my table with the phone numbers? I am querying multiple tables that store phone numbers.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2017-02-06T20:42:12+00:00

    Go on a new blank query field and enter

    HomePhoneNo: StripAllChars([Home Phone])

    as the Field expression.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2017-02-06T20:30:31+00:00

    Mr Pineault

    Forgive me, but I am a little confused about where to add

    "HomePhoneNo: StripAllChars([Home Phone])"

    You say I should not query my fields directly, but rather, "add a new field." What do you mean by this?

    Thanks

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2017-02-06T20:12:42+00:00

    Mr Pineault

    Thank you very much for this. I was not the one to develop this database. There are a lot of things I wouldve done differently ie: storing only numbers, no " " (spaces).

    I appreciate your help

    Was this answer helpful?

    0 comments No comments