The Archn00b
Registered User.
- Local time
- Today, 14:05
- Joined
- Jun 26, 2013
- Messages
- 76
Hello,
So I have a table "zztblArticles." Some fields should go to "tblArticles" and values in Tag_ID should go to a lookup table "tblTag" and a junction table "tblArticles_Tags." I'll explain the code I've written below:
The first loop through works fine, I get the records uploaded to all tables. The second loop through fails at
Because it can't decide which ID value to use. This is because the value has been duplicated in tblArticles after the code acts on the same record again. It has completely failed to move to the next record in the recordset, despite the .MoveNext before the Loop!
Any ideas?
Thanks!
So I have a table "zztblArticles." Some fields should go to "tblArticles" and values in Tag_ID should go to a lookup table "tblTag" and a junction table "tblArticles_Tags." I'll explain the code I've written below:
Code:
Private Sub cmdSubmit_Click()
Dim db As Database
Dim strINSERT As String
Dim strVALUES As String
Dim rszztblArticles As DAO.Recordset
Set db = CurrentDb
Set rszztblArticles = db.OpenRecordset("SELECT zztblArticles.Publishing_Date, zztblArticles.Title" & _
", zztblArticles.Source_ID, zztblArticles.Sourcer_ID, zztblArticles.Sourcing_Date" & _
", zztblArticles.Source_Category_ID, zztblArticles.Location, zztblArticles.Comments, zztblArticles.Tag_ID FROM zztblArticles;")
strINSERT = "INSERT INTO tblArticles (Publishing_Date, Title, Location, Sourcing_Date, Comments "
strVAlUES = " VALUES ('" & DateDiff("d", #5/16/2014#, rszztblArticles!Publishing_Date) & _
"', '" & rszztblArticles!Title & "', '" & rszztblArticles!Location & _
"', '" & rszztblArticles!Sourcing_Date & "', '" & rszztblArticles!Comments & "'"
With rszztblArticles
.MoveFirst
Do While .EOF = False
'Creates sql to append to single-value fields from zztblArticles to tblArticles
If IsNull(!Source_ID) = False Then
strINSERT = strINSERT & ", Source_ID"
strVAlUES = strVAlUES & ", '" & !Source_ID & "'"
End If
If IsNull(!Sourcer_ID) = False Then
strINSERT = strINSERT & ", Sourcer_ID"
strVAlUES = strVAlUES & ", '" & !Sourcer_ID & "'"
End If
If IsNull(!Source_Category_ID) = False Then
strINSERT = strINSERT & ", Source_Category_ID"
strVAlUES = strVAlUES & ", '" & !Source_Category_ID & "'"
End If
'Appends single-value fields to tblArticles
db.Execute (strINSERT & ")" & strVALUES & ")")
'Finds the automatically generated ID in tblArticles by matching Sourcing_Date
intArticleID = DLookup("ID", "tblArticles", "Sourcing_Date = " & !Sourcing_Date)
'Creates an array from the Tag ID value which could be (England, Scotland, Wales)
TagsArray = Split(!Tag_ID, ",", , vbTextCompare)
Dim i As Integer
'For each item in the Array
For i = LBound(TagsArray) To UBound(TagsArray)
If IsNull(DLookup("Tag", "tblTag", "tblTag.Tag = '" & Trim(TagsArray(i)) & "'")) Then
db.Execute ("INSERT INTO tblTag (Tag) VALUES ('" & Trim(TagsArray(i)) & "')")
End If
intTagID = DLookup("ID", "tblTag", "tblTag.Tag = '" & Trim(TagsArray(i)) & "'")
db.Execute ("INSERT INTO tblArticles_Tags (Tag_ID, Article_ID) VALUES (" & intTagID & "," & intArticleID & ")")
Next i
db.Execute ("DELETE * FROM zztblArticles WHERE Sourcing_Date = " & rszztblArticles!Sourcing_Date)
.MoveNext
Loop
.Close
End With
Set db = Nothing
db.Close
MsgBox ("Your articles have been uploaded successfully")
Exit Sub
End Sub
The first loop through works fine, I get the records uploaded to all tables. The second loop through fails at
Code:
intArticleID = DLookup("ID", "tblArticles", "Sourcing_Date = " & !Sourcing_Date)
Because it can't decide which ID value to use. This is because the value has been duplicated in tblArticles after the code acts on the same record again. It has completely failed to move to the next record in the recordset, despite the .MoveNext before the Loop!
Any ideas?
Thanks!