insert dashes

D

Donald

if i receive a string of data and AAABBBCCCDDD and after
every 3rd letter, i want to insert a dash, how would i go
about doing that? sometimes the data will have just
AAABBBCCC, AAABBBAAACCCDDD, AAABBBCCCBBBDDDAAA, etc but is
ALWAYS in multiples of 3.

thanks

DW
 
J

John Nurick

Hi Donald,

As Ken says it depends on how you receive the data. Assuming these are
values in a text field in a table, you'd use an update query that calls
a VBA function to actually insert the dashes in each string.

Here's a simple function that will do the job (there are ways of
speeding it up if you're continually processing many thousands of
strings).

Public Function InsertDashEvery3rdChar(V As Variant) As Variant
Dim oRE As Object, varTemp As Variant

Set oRE = CreateObject("VBScript.RegExp")
With oRE
.Global = True
.IgnoreCase = True
.Multiline = False
.Pattern = "(...)(?!$)" 'regular expression matching
'3 characters not at end of string
varTemp = .Replace(CStr(Nz(V, "")), "$1-")
End With
InsertDashEvery3rdChar = varTemp
Set oRE = Nothing
End Function
 
G

Guest

i receive the data in text file format.
-----Original Message-----
How are you receiving the data? via form? via file import?

--

Ken Snell
<MS ACCESS MVP>




.
 
G

Guest

John,

thanks for your help.

DW
-----Original Message-----
Hi Donald,

As Ken says it depends on how you receive the data. Assuming these are
values in a text field in a table, you'd use an update query that calls
a VBA function to actually insert the dashes in each string.

Here's a simple function that will do the job (there are ways of
speeding it up if you're continually processing many thousands of
strings).

Public Function InsertDashEvery3rdChar(V As Variant) As Variant
Dim oRE As Object, varTemp As Variant

Set oRE = CreateObject("VBScript.RegExp")
With oRE
.Global = True
.IgnoreCase = True
.Multiline = False
.Pattern = "(...)(?!$)" 'regular expression matching
'3 characters not at end of string
varTemp = .Replace(CStr(Nz(V, "")), "$1-")
End With
InsertDashEvery3rdChar = varTemp
Set oRE = Nothing
End Function

if i receive a string of data and AAABBBCCCDDD and after
every 3rd letter, i want to insert a dash, how would i go
about doing that? sometimes the data will have just
AAABBBCCC, AAABBBAAACCCDDD, AAABBBCCCBBBDDDAAA, etc but is
ALWAYS in multiples of 3.

thanks

DW

--
John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.
.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top