DoCmd.SetWarnings False
On Error Resume Next
Dim GrapeCnt, IntGrapeID, IntPunctPos As Integer
Dim GrapeOrgStr, GrapeTrimStr, GrapeSendStr As String
Dim QryAppend, QryDelRec As String
'determine number of grapes to be normalised
GrapeCnt = DCount("grapeid", "tbl_grape1")
While GrapeCnt > 0
'determine next recrod to be nomalised
IntGrapeID = DLookup("grapeID", "tbl_grape1", "grapeID = " & DMin("grapeID", "tbl_grape1"))
'Determine String to be normalised
GrapeOrgStr = DLookup("Grape", "tbl_grape1", "grapeID = " & IntGrapeID)
'MsgBox "Next text to be normalised is " & GrapeOrgStr
'Check record for commas or ampersands
'to work this portion of the code relies on commas always proceeding ampersands
If InStr(1, GrapeOrgStr, ",") > 0 Then 'Check if text contains a comma and record postion
IntPunctPos = InStr(1, GrapeOrgStr, ",")
ElseIf InStr(1, GrapeOrgStr, "&") > 0 Then 'Check if text contains a ampersand and record postion
IntPunctPos = InStr(1, GrapeOrgStr, "&")
End If
If IntPunctPos <> 0 Then 'Check if commas or ampersands detected then cycle through text removing and append data to new table
'MsgBox "we have work to do"
While IntPunctPos > 0
GrapeSendStr = Left(GrapeOrgStr, IntPunctPos - 1)
QryAppend = "INSERT INTO TBL_Grape2 ( Grape )" & _
"values ( " & Chr(34) & GrapeSendStr & Chr(34) & " ) ;"
If DCount("[Grape]", "tbl_grape2", "[grape] = '" & Chr(34) & GrapeSendStr & Chr(34) & "'") < 1 Then 'check if this grape already exsists and skip
DoCmd.RunSQL (QryAppend)
End If
If (Len(GrapeOrgStr) - (IntPunctPos + 1)) > 1 Then
GrapeTrimStr = Right(GrapeOrgStr, (Len(GrapeOrgStr) - (IntPunctPos + 1)))
GrapeOrgStr = GrapeTrimStr
End If
IntPunctPos = 0
If InStr(1, GrapeOrgStr, ",") > 0 Then 'Check if text contains a comma and record postion
IntPunctPos = InStr(1, GrapeOrgStr, ",")
ElseIf InStr(1, GrapeOrgStr, "&") > 0 Then 'Check if text contains a ampersand and record postion
IntPunctPos = InStr(1, GrapeOrgStr, "&")
End If
Wend
Else 'If no commas or ampersands detected simply append current record to new table
QryAppend = "INSERT INTO TBL_Grape2 ( Grape ) " & _
"SELECT TBL_Grape1.Grape " & _
"FROM TBL_Grape1 " & _
"WHERE (((TBL_Grape1.GrapeID)= " & IntGrapeID & " ));"
DoCmd.RunSQL (QryAppend)
End If
End If
'Delete first record once appended to new Table
QryDelRec = "DELETE TBL_Grape1.GrapeID, TBL_Grape1.Grape " & _
"FROM TBL_Grape1 " & _
"WHERE (((TBL_Grape1.GrapeID)= " & IntGrapeID & "));"
DoCmd.RunSQL (QryDelRec)
'Update GrapeCnt with new record total
GrapeCnt = DCount("grapeid", "tbl_grape1")
Wend
DoCmd.SetWarnings True
End Sub
Private Sub Test000()
Const APND_QUERY = _
"INSERT INTO TBL_Grape2 ( Grape ) " & _
"SELECT p1;"
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim GrapeCnt As Long
Dim GrapeOrgStr As String
Dim varValue As Variant
Dim i As Integer
Set db = CurrentDb
Set rs = db.OpenRecordset("tbl_grape1", dbOpenSnapshot, dbReadOnly)
With rs
If Not (.BOF And .EOF) Then
.MoveLast
GrapeCnt = .RecordCount
.MoveFirst
End If
Do Until .EOF
GrapeOrgStr = !Grape & ""
varValue = Split(GrapeOrgStr, ",")
If UBound(varValue) = 0 Then
varValue = Split(GrapeOrgStr, "&")
End If
For i = 0 To UBound(varValue)
With db.CreateQueryDef("", APND_QUERY)
.Parameters(0) = Trim$(varValue(i))
.Execute
End With
Next
.MoveNext
GrapeCnt = GrapeCnt - 1
Loop
.Close
End With
Set rs = Nothing
Set db = Nothing
End Sub
Set rs = db.OpenRecordset("tbl_grape1", dbOpenSnapshot, dbReadOnly)
With db.CreateQueryDef("", APND_QUERY)
GrapeOrgStr = !Grape & ""
varValue = Split(GrapeOrgStr, ",")
If UBound(varValue) = 0 Then
varValue = Split(GrapeOrgStr, "&")
End If
...
...
Dim varValue1 As Variant
Dim varValue2 As Variant
Dim i As Integer, k As Integer
Set db = CurrentDb
Set rs = db.OpenRecordset("tbl_grape1")
With rs
If Not (.BOF And .EOF) Then
.MoveLast
GrapeCnt = .RecordCount
.MoveFirst
End If
Do Until .EOF
GrapeOrgStr = !Grape & ""
varValue1 = Split(GrapeOrgStr, ",")
For i = 0 To UBound(varValue1)
varValue2 = Split(varValue1(i), "&")
For k = 0 To UBound(varValue2)
With db.CreateQueryDef("", APND_QUERY)
.Parameters(0) = Trim$(varValue2(k))
.Execute
End With
Next k
Next i
.MoveNext
GrapeCnt = GrapeCnt - 1
Loop
.Close
End With
Set rs = Nothing
Set db = Nothing
End Sub
Private Sub Command4_Click()
'define append query SQL
Const APND_QUERY = _
"INSERT INTO TBL_Grape2 ( Grape ) " & _
"SELECT p1;"
'Define various variables that will be required for this process
Dim GrapeCnt As Long 'Count of records in data set
Dim GrapeOrgStr As String 'Original record string to be searched
Dim varValue1 As Variant 'variable to store string with commas removes
Dim varValue2 As Variant 'Variable to store string with ampersands removed
Dim i As Integer, k As Integer 'variables to be used as counters
'Define general location of data
Set db = CurrentDb
'Define specific location of data, and open the record set
Set rs = db.OpenRecordset("tbl_grape1")
'Start working on the record set as defined above
With rs
'Count number of records and save as GrapeCnt
If Not (.BOF And .EOF) Then
.MoveLast 'move to last record
GrapeCnt = .RecordCount 'assing record count to GrapeCnt variable
.MoveFirst 'move to first record
End If
'This is were the real work happens
Do Until .EOF 'Start cycleing through record set until the end is reached (Do Loop)
GrapeOrgStr = !Grape & "" 'Assign next record in Field[Grape] to variable GrapeOrgStr
varValue1 = Split(GrapeOrgStr, ",") 'Assign split string (split on commas) to variable VarVal1
For i = 0 To UBound(varValue1) '(loop i) - will loop the number detected by UBound()
varValue2 = Split(varValue1(i), "&") 'Assign split string (split on Ampersand) to variable VarVal2 - This variable now has no "," or "&" just a number of discrete strings
For k = 0 To UBound(varValue2) '(loop k) - will loop the number detected by UBound()
With db.CreateQueryDef("", APND_QUERY) 'Create append query based on SQL previous defined (at the start)
.Parameters(0) = Trim$(varValue2(k)) 'Insert string to be append in Append Query
.Execute 'Run append query
End With '
Next k 'End of loop k, return to start
Next i 'End of loop i, return to start
.MoveNext 'Move to next record
GrapeCnt = GrapeCnt - 1 'Incriment GrapeCnt down by one -Not sure why? as GrapeCnt dosen't seem to be in use??
Loop 'Return start of loop "Do" Loop
'We're done here, now let's close the data set
.Close
'Tidy up before exiting the sub-routine
End With
Set rs = Nothing
Set db = Nothing
End Sub