Public Function CopyItemsToADifferentList() As Boolean 'this is boolean as I call it from a command button on a user form
CopyItemsToADifferentList = False 'set this to false, so if something errors along the way, we don't continue on and purge the original items
'*********************************************
'Declare some variables here....
Dim dbs As DAO.Database
Dim rs As DAO.Recordset, rsMatch As DAO.Recordset, rsDestination As DAO.Recordset
Dim I As Integer, iBatch As Integer
Dim strSQL As String, strLogFileName As String, strLoopAction As String
'/variable declarations
'*********************************************
'Show the hour glass so the user knows the system is doing something
DoCmd.Hourglass True
On Error GoTo WhatBroke
Set dbs = CurrentDb
'this recordset needs the be from the list we are going to be archiving FROM (Source List Items). We need
'to determine if there are any records that need to be archived given our filtering selection(s).
strSQL = "Select * FROM [SourceListItems] WHERE [SomeField] = 'SomeValue'"
'now load up the recordset
Set rs = dbs.OpenRecordset(strSQL, dbOpenDynaset)
I = 0 'counter
Do While Not rs.EOF 'how many records are we dealing with?
I = I + 1
rs.MoveNext
Loop
'Show the progress bar. This is helpful for when processes take some time, so the user doesn't think the system locked up
SysCmd acSysCmdInitMeter, "Processing Source List Items Archive...", rs.RecordCount
'Prep the log file. We should NEVER end up with a duplicate file name. Intentionally not bothering to check.
'Note: The trailing '\' IS INCLUDED in the LogFilesPath tempvar - which is set by a text box that allows for a folder path selection in my user form.
'If something goes wrong with the log write process, start by checking the value in this TV first
strLogFileName = TempVars![LogFilesPath] & "LogFile_SourceListItems_" & Year(Now()) & Month(Now()) & Day(Now()) & "_hr" & DatePart("h", Now()) & "_min" & DatePart("n", Now()) & "_sec" & DatePart("s", Now()) & ".txt"
'Debug.Print strLogFileName 'in the event there's some necessary troubleshooting, uncomment the first part of this line
Open strLogFileName For Output As #1
Print #1, "Total Records to be processed to the SharePoint Destination Items List is " & I & "|Started at " & Now() & "|By: " & (Environ$("Username")) 'write in the log who ran this process
If I > 0 Then 'we have some records to process
rs.MoveFirst 'need to move back to the beginning of our recordset
I = 1 'reset our counter
Do While Not rs.EOF
strSQL = "Select Count(ID) as ItemMatch from [DestinationListItems] WHERE [OldID]=" & rs![Id].Value 'in my destination list, I copy the original list ID to help with pricessing tasks
Set rsMatch = dbs.OpenRecordset(strSQL, dbOpenDynaset)
If (rsMatch![ItemMatch].Value > 0) Then 'we already have this item in our destination list, so update it instead of adding a new item
strSQL = "Select * from [DestinationListItems] WHERE [OldID]=" & rs![Id].Value
Set rsDestination = dbs.OpenRecordset(strSQL, dbOpenDynaset)
rsDestination.Edit
strLoopAction = "Update"
ElseIf (rsMatch![ItemMatch].Value = 0) Then 'Add a new Record
Set rsDestination = dbs.OpenRecordset("[DestinationListItems]", dbOpenDynaset)
rsDestination.AddNew
rsDestination![OldID].Value = rs![Id].Value
strLoopAction = "AddNew"
End If
rsMatch.Close
rsDestination![DestinationFields].Value = rs![SourceFields].Value
rsDestination![OriginalCreateDate].Value = rs![Created].Value
'include other fields in the list that need to be populated. Watch out for unmathced field types between the lists,
'and for multiple-lines of text list field types, make sure the field is set to plain text, and not rich or enhanced rich text - updates/adds will fail
rsDestination.Update
rsDestination.Bookmark = rsDestination.LastModified 'move our cursor back to the row we just updated
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
'After we deal with updating or adding the list item data, then need to deal with the list item attachments
'Sharepoint doesn't allow for the folder creation! need to rethink this section...
'Right now this function breaks if VBA tries to create the destination item attachment folder
If ProcessListItemAttachments(rs![Id].Value, TempVars!AttachPathSource.Value, rsDestination![Id].Value, TempVars!AttachPathDestination.Value) = False Then
Print #1, "There was an error in the attachment copy process for Source List Item # " & rs![Id].Value
End If
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SysCmd acSysCmdUpdateMeter, I
Print #1, "Loop#" & I & "|Source List Items RowID:" & rs![Id].Value & "|Destination RowID: " & rsDestination![Id].Value & "| " & strLoopAction & "|" & Now()
rsDestination.Close
I = I + 1
rs.MoveNext
Loop 'To the next Source List Items record we want to update or add to the destination list
Else
MsgBox "There are no matching Source List Items records for copying.", vbInformation + vbOKOnly, "Nothing to Process"
Print #1, "There are no matching Source List Items records for processing."
End If
rs.Close
I = I - 1
Print #1, "Completed updating/adding " & I & " records on " & Now()
exitblock:
Set dbs = Nothing
Set rs = Nothing
Set rsMatch = Nothing
Set rsDestination = Nothing
Close #1
'Remove the progress bar
SysCmd acSysCmdRemoveMeter
'Show the normal cursor again
DoCmd.Hourglass False
Exit Function
WhatBroke:
If (Err.Number <> 0) Then
CopyItemsToADifferentList = False 'make sure that we stop the purge function from running if something gets pooched during this process.
MsgBox "Error " & Err.Number & ": " & Err.Description & " in " & VBE.ActiveCodePane.CodeModule, vbOKOnly, "Error"
DoCmd.Hourglass False
'Print #1, "Error " & Err.Number & ": " & Err.Description & " in " & VBE.ActiveCodePane.CodeModule
Resume exitblock
End If
End Function