In my Access program, I have a main form that holds recipe general information and a sub-form that keeps the actual formula. The main form name is “recipe” and Record Source is a table named “TRecipeNa me”. Sub-form name is Recipe_Sub and Record Source is a Query named “Q_Recipe”
Two primary keys in the table TRecipeName to make each record unique, recipeID (associate to the text field “txt_recip eID “ in the main form) and RecipeVersion (associated to the text field “txtRecipe Ver” in the main form). Both fields are used to associate to the sub-form.
A button is created to duplicate a record that keeps same recipe ID as current record and new version with all information from current record carried over to the new version.
I tried to follow Vba codes that Allen Browne developed (with a few modifications) for this function but running into error message. Since I am not familiar with the concept of RecordsetClone and the SQL statement in Vba I am hoping someone can take a look of my code and help solve my issue.
When I tried following codes the error message pop up:
Run-time error ‘2465’
System can’t find the field ‘|1’ referred to in your expression.
When clicked debug button then Access pointed to the following line (line #43):
I know my modified codes must have problem at the Select statement, but I don't know how to fix it. In addition, the use of recordsetclone may caused conflicting of my objective of generating same ID but new version. I hope someone with experience of this can help me out.
Thanks in advance.
Joe
Two primary keys in the table TRecipeName to make each record unique, recipeID (associate to the text field “txt_recip eID “ in the main form) and RecipeVersion (associated to the text field “txtRecipe Ver” in the main form). Both fields are used to associate to the sub-form.
A button is created to duplicate a record that keeps same recipe ID as current record and new version with all information from current record carried over to the new version.
I tried to follow Vba codes that Allen Browne developed (with a few modifications) for this function but running into error message. Since I am not familiar with the concept of RecordsetClone and the SQL statement in Vba I am hoping someone can take a look of my code and help solve my issue.
When I tried following codes the error message pop up:
Run-time error ‘2465’
System can’t find the field ‘|1’ referred to in your expression.
When clicked debug button then Access pointed to the following line (line #43):
Code:
If Me.[Recipe_Sub].Form.RecordsetClone.RecordCount > 0 Then
Thanks in advance.
Joe
Code:
Dim strSql As String 'SQL statement.
Dim RPID As String 'Primary key, recipe ID, value of the new record.
Dim RPVer As String 'Primary key, Version, value of the new record.
Dim strRecipeID As String
strRecipeID = Me.[txt_recipeID]
'Save any edits first
If Me.Dirty Then
Me.Dirty = False
End If
'Make sure there is a record to duplicate.
If Me.NewRecord Then
MsgBox "Select the record to duplicate."
Else
'Duplicate the main record: add to form's clone.
With Me.RecordsetClone
.AddNew
!RecipeID = Me.[txt_recipeID]
!RecipeVersion = DMax("[RecipeVersion]", "[TRecipeName]", "[RecipeID] = '" & strRecipeID & "'") + 1
!RName = Me.[txt_Description]
!Procedure = Me.[TxtProcedure]
!CasePack = Me.[txt_UnitPack]
!Sauce_Unit = Me.[txtSaucwPkt]
!UnitPerCase = Me.[txt_UnitCase]
!UnitPkgMtlWt = Me.[txt_UnitPkWt]
!PalletTie1 = Me.[txt_Tie1]
!PalletTie2 = Me.[txt_Tie2]
!PalletTier = Me.[txt_Tier]
!PalletType = Me.[txt_PalletType]
.Update
'Save the primary key value, to use as the foreign key for the related records.
.Bookmark = .LastModified
RPID = !RecipeID
RPVer = !RecipeVersion
'Duplicate the related records: append query.
If Me.[Recipe_Sub].Form.RecordsetClone.RecordCount > 0 Then
strSql = "INSERT INTO [Q_Recipe] (recipeID, RecipeVersion, productID, ProductVersion, %, Recipe_UOM) " _
& "SELECT " & RPID & " As NewID, " & RPVer & " As NewV, productID, ProductVersion, %, Recipe_UOM " _
& "FROM [Q_Recipe] WHERE recipeID = " & Me.[txt_recipeID] & " & AND & RecipeVersion = " & Me.[txtRecipeVer] & ";"
DBEngine(0)(0).Execute strSql, dbFailOnError
Else
MsgBox "Main record duplicated, but there were no related records."
End If
'Display the new duplicate.
Me.Bookmark = .LastModified
End With
End If
Exit_Handler:
Exit Sub
Comment