Hi everyone. I've created a button on one of my forms, for the purpose of copying some data on the current record into a new record. I have created several variables to store the data that exists on the current form, which will then paste that data into the new record, when it becomes the current one. I have an array for storing multiple values, to save me from typing more code and creating even more variables. However, the value stores inside my arrays is all turning to Null once I create the new record.
Code:
Private Sub DuplicateButton_Click()
Dim JobID_Old As Integer
Dim WeekEnding_Old As Date
Dim Call_Old As Variant
Dim M1Out_Old As Variant
Dim M1In_Old As Variant
Dim M2Out_Old As Variant
Dim M2In_Old As Variant
Dim Wrap_Old As Variant
Dim CallControl As String
Dim M1OutControl As String
Dim M1InControl As String
Dim M2OutControl As String
Dim M2InControl As String
Dim WrapControl As String
Dim Counter As Integer
If Me.Dirty Then Me.Dirty = False
'this reads all the data off the current time card that I will want to appear in the new time card record.
Call_Old = Array(Me.D1Call, Me.D2Call, Me.D3Call, Me.D4Call, Me.D5Call, Me.D6Call, Me.D7Call)
M1Out_Old = Array(Me.D1Meal1Out, Me.D2Meal1Out, Me.D3Meal1Out, Me.D4Meal1Out, Me.D5Meal1Out, Me.D6Meal1Out, Me.D7Meal1Out)
M1In_Old = Array(Me.D1Meal1In, Me.D2Meal1In, Me.D3Meal1In, Me.D4Meal1In, Me.D5Meal1In, Me.D6Meal1In, Me.D7Meal1In)
M2Out_Old = Array(Me.D1Meal2Out, Me.D2Meal2Out, Me.D3Meal2Out, Me.D4Meal2Out, Me.D5Meal2Out, Me.D6Meal2Out, Me.D7Meal2Out)
M2In_Old = Array(Me.D1Meal2In, Me.D2Meal2In, Me.D3Meal2In, Me.D4Meal2In, Me.D5Meal2In, Me.D6Meal2In, Me.D7Meal2In)
Wrap_Old = Array(Me.D1Wrap, Me.D2Wrap, Me.D3Wrap, Me.D4Wrap, Me.D5Wrap, Me.D6Wrap, Me.D7Wrap)
JobID_Old = Me.JobID_notFK
WeekEnding_Old = Me.WeekEnding
'this creates the new record
DoCmd.GoToRecord , , acNewRec
'this writes the data to the new record
Me.JobID_notFK = JobID_Old
Me.WeekEnding = WeekEnding_Old
For Counter = 0 To 6
CallControl = "D" & Counter + 1 & "Call"
M1OutControl = "D" & Counter + 1 & "Meal1Out"
M1InControl = "D" & Counter + 1 & "Meal1In"
M2OutControl = "D" & Counter + 1 & "Meal2Out"
M2InControl = "D" & Counter + 1 & "Meal2In"
WrapControl = "D" & Counter + 1 & "Wrap"
Me.Controls(CallControl) = Call_Old(Counter)
Me.Controls(M1OutControl) = M1Out_Old(Counter)
Me.Controls(M1InControl) = M1In_Old(Counter)
Me.Controls(M2OutControl) = M2Out_Old(Counter)
Me.Controls(M2InControl) = M2In_Old(Counter)
Me.Controls(WrapControl) = Wrap_Old(Counter)
Next
End Sub
The data that is stores inside JobID_Old and WeekEnding_Old stays inside those variables and is properly placed on the new record, just as intended. As for the arrays, the data does go inside the arrays as planned (I stepped through the code and watched those variables in the Watch window), but as soon as the line
Code:
DoCmd.GoToRecord , , acNewRec
is executed, the data inside the arrays all becomes Null. I don't understand why this would be happening. Why does it not hold the values like the JobID_Old and WeekEnding_Old variables do?