I have an MS Access database that has a field called Field1that contains several values separated by commas. i.e.,
Value1,Value 2, Value3, Value 4,Value5
I try to split the values not into separate fields, but by duplicating the record and storing each value in another field. It will be such that a record containing a cell with three values will be duplicated three times, with each record changing in the value contained in the new field. For instance,
Before requesting / starting the module:
+-----------+------------------------+
| App Code | Field1 |
+-----------+------------------------+
| AB23 | Value1, Value 2,Value3 |
+------------------------------------+
After the request / execution of the module:
+-----------------------------------------------+
| App Code | Field1 | Field2 |
+-----------+------------------------+----------+
| AB23 | Value1, Value 2,Value3 | Value1 |
+-----------+------------------------|----------+
| AB23 | Value1, Value 2,Value3 | Value 2 |
+-----------+------------------------+----------+
| AB23 | Value1, Value 2,Value3 | Value3 |
+-----------+------------------------+----------+
, . , , , , VBA.
, VBA, :
Function CountCSWords (ByVal S) As Integer
' Counts the words in a string that are separated by commas.
Dim WC As Integer, Pos As Integer
If VarType(S) <> 8 Or Len(S) = 0 Then
CountCSWords = 0
Exit Function
End If
WC = 1
Pos = InStr(S, ",")
Do While Pos > 0
WC = WC + 1
Pos = InStr(Pos + 1, S, ",")
Loop
CountCSWords = WC
End Function
Function GetCSWord (ByVal S, Indx As Integer)
' Returns the nth word in a specific field.
Dim WC As Integer, Count As Integer, SPos As Integer, EPos As Integer
WC = CountCSWords(S)
If Indx < 1 Or Indx > WC Then
GetCSWord = Null
Exit Function
End If
Count = 1
SPos = 1
For Count = 2 To Indx
SPos = InStr(SPos, S, ",") + 1
Next Count
EPos = InStr(SPos, S, ",") - 1
If EPos <= 0 Then EPos = Len(S)
GetCSWord = Trim(Mid(S, SPos, EPos - SPos + 1))
End Function
Access Query ? , , (.. VBA)?
, Application Code, autonumber. . , , .