Did you post the entire function into the vba module? When you did so,
did any of the code lines change color? I would expect the error
message to occur if you had dropped the first line. The following is
the first line and it should all be on one line.
Public Function fStripToNumbersOnly(ByVal varText As Variant) As Variant
The code should look like
'---------- Code Starts -----------------
Public Function fStripToNumbersOnly(ByVal varText As Variant) As Variant
'Takes input and returns only the numbers in the input.
'Strips out all other characters.
'Handles nulls, dates, numbers, and strings.
Const strNumbers As String = "0123456789"
Dim strOut As Variant
Dim intCount As Integer
If Len(varText & "") = 0 Then
strOut = varText
Else
varText = varText & ""
For intCount = 1 To Len(varText)
If InStr(1, strNumbers, Mid(varText, intCount, 1)) > 0 Then
strOut = strOut & Mid(varText, intCount, 1)
End If
Next intCount
End If
fStripToNumbersOnly = strOut
End Function
'------------ Code end ----------------
I am going out of town for a week - starting tomorrow-, so you may need
to start a new thread if this does not solve your problem.
'====================================================
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
'====================================================
Thanks again for this response. Let's continue to work together until we get
it right. I have done what you suggested as far as creating the module.
When I try to save the module I get the following error, "Compile Error.
Invalid Outside Procedure." The the very first If statement has "varText"
highlighted.
When I attempt to do the Update Query where I copy and past your code into
the Update to field, I get the error message "Expression you entered contains
invalid syntax." WHERE is highlighted. Then I insert my table name and Fax
for Telephone. Please help.
John Spencer said:
OK.
Create the module with the code first.
Open a new module in the database
-- Open database in design mode
-- Click on Modules
-- Click on New button at the top
-- Paste in the posted code
-- Select File: Save ... from the menu and accept the proposed name module#
MAKE A BACKUP of your data
Now that the module is built, go back to the database window
-- Create a new query with your table and the field to be updated
-- Select Query: Update Query from the menu
-- Under the phone field
------ Update To: fStripToNumbersOnly ([YourTableName].[YourFieldName])
------ Criteria: Not Like "##########"
The criteria will screen out records that are already in the format of 10
numeric characters. Since you may be doing this over and over it makes
sense to avoid processing records that already meet the format you want.
I would set up a separate query to handle the Fax numbers. You can do both
the fax and telephone numbers in one query if you wish.
AS I said backup up your data first (at least the until you are sure this
works for you).
By the way an alternative is to add two columns to your table FixedPhone and
FixedFax and UPDATE those fields to the calculated values of the existing
fields. That way you will have both the input version and the calculated
version of the numbers to compare.
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
Perusing your response further, the line
Const strNumbers As String = "0123456789"
is exactly what I want for the Phone and Fax fields. Now please tell me
how
to insert your code into an Update query. Thanks.
:
You can use a VBA function to do this. Paste the function below into a
module and save it the module (Don't name the module the same as the
function)
If you want to do this permanently then use the function in an Update
query.
If you just want to do this to see the value then you can use the
function
in the select clause
SELECT fStripToNumbersOnly([Telephone]) as PhoneNum
FROM YourTable
IF you have a lot of records this could be slow.
The update query would look like
UPDATE YourTable
SET [Telephone] = fStripToNumbersOnly([YourTable].[Telephone])
WHERE [Telephone] is Not Null
Public Function fStripToNumbersOnly(ByVal varText As Variant) As Variant
'Takes input and returns only the numbers in the input. Strips out
'all other characters. Handles nulls, dates, numbers, and strings.
Const strNumbers As String = "0123456789"
Dim strOut As Variant
Dim intCount As Integer
If Len(varText & "") = 0 Then
strOut = varText
Else
varText = varText & ""
For intCount = 1 To Len(varText)
If InStr(1, strNumbers, Mid(varText, intCount, 1)) > 0 Then
strOut = strOut & Mid(varText, intCount, 1)
End If
Next intCount
End If
fStripToNumbersOnly = strOut
End Function
--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
A project that I am doing requires collecting large amounts of contact
data
and working with the phone numbers. My job would be a lot easier if
the
formatting on the phone numbers were deleted. For example, the left
and
right parentheses, Slashes, dashes and spaces cause some problems and I
would
like to know how to remove them. I am not knowledgable in writing code
but
do know a few things about queries. Everything I try results in a
syntax
error message. What can you suggest?