ArisaAnsar
asked on
Microsoft Access
In Excel, there is a feature that allows you to convert data in a column into multiple columns by using the Text to Column feature.
Is there a similar feature in Access? My table currently includes a column with account numbers. Unfortunately the column also has a preceding 0 on each row that I would like to remove.
Is there a similar feature in Access? My table currently includes a column with account numbers. Unfortunately the column also has a preceding 0 on each row that I would like to remove.
If you have multiple leading zeroes and need to eliminate all of them, you need code.
For example, add this to your VBA project:
Then use it like this:
or
That approach uses Regular Expressions, as explained here: https://www.experts-exchange.com/Programming/Languages/Visual_Basic/A_1336-Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html
For example, add this to your VBA project:
Function RegExpReplace(LookIn As String, PatternStr As String, Optional ReplaceWith As String = "", _
Optional ReplaceAll As Boolean = True, Optional MatchCase As Boolean = True, _
Optional MultiLine As Boolean = False)
' Function written by Patrick G. Matthews. You may use and distribute this code freely,
' as long as you properly credit and attribute authorship and the URL of where you
' found the code
' For more info, please see:
' http://www.experts-exchange.com/articles/Programming/Languages/Visual_Basic/Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html
' This function relies on the VBScript version of Regular Expressions, and thus some of
' the functionality available in Perl and/or .Net may not be available. The full extent
' of what functionality will be available on any given computer is based on which version
' of the VBScript runtime is installed on that computer
' This function uses Regular Expressions to parse a string, and replace parts of the string
' matching the specified pattern with another string. The optional argument ReplaceAll
' controls whether all instances of the matched string are replaced (True) or just the first
' instance (False)
' If you need to replace the Nth match, or a range of matches, then use RegExpReplaceRange
' instead
' By default, RegExp is case-sensitive in pattern-matching. To keep this, omit MatchCase or
' set it to True
' If you use this function from Excel, you may substitute range references for all the arguments
' Normally as an object variable I would set the RegX variable to Nothing; however, in cases
' where a large number of calls to this function are made, making RegX a static variable that
' preserves its state in between calls significantly improves performance
Static RegX As Object
If RegX Is Nothing Then Set RegX = CreateObject("VBScript.RegExp")
With RegX
.Pattern = PatternStr
.Global = ReplaceAll
.IgnoreCase = Not MatchCase
.MultiLine = MultiLine
End With
RegExpReplace = RegX.Replace(LookIn, ReplaceWith)
End Function
Then use it like this:
SELECT RegExpReplace(AcctNum, "^0+") AS xAcctNum
FROM SomeTable
or
UPDATE SomeTable
SET AcctNum = RegExpReplace(AcctNum, "^0+")
WHERE AcctNum Like "0*"
That approach uses Regular Expressions, as explained here: https://www.experts-exchange.com/Programming/Languages/Visual_Basic/A_1336-Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html
ASKER
Thanks for helping. I appreciate it.
I don't know how to use VBA in access. Is there a way to extract the zero using the right or left function?
I don't know how to use VBA in access. Is there a way to extract the zero using the right or left function?
ArisaAnsar,
Please see my first post, which explains how to trim a single leading zero without any VBA at all.
Patrick
Please see my first post, which explains how to trim a single leading zero without any VBA at all.
Patrick
ASKER
Sorry - Please be a little patient with me. Can you advise where I should add the codes? I'm a new user to Access. Do I post in the criteria of the account column or in the Field row and replace AcctNum with my column name?
UPDATE SomeTable
SET AcctNum = Mid(AcctNum, 2)
WHERE AcctNum Like "0*"
UPDATE SomeTable
SET AcctNum = Mid(AcctNum, 2)
WHERE AcctNum Like "0*"
ASKER
Here is a sample of the database. Thanks again.
Relationship-Database.mdb
Relationship-Database.mdb
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you! This worked.
Access has no automated Text to Columns feature, but one can (mostly) replicate the functionality with queries and code.
As for eliminating a leading zero, this will remove a single leading zero on a text field:
Open in new window
or
Open in new window
Patrick