H
hikaru
hi all,
i'm using a code in the following form:
http://img169.imageshack.us/img169/3230/frmcontmonthlydetailsedqn4.jpg
when the user click on "Add New", the last record should be duplicated
according to the append query "strSql_ContMonthly", the the form is closed
and opened again so that the new record would be shown. it works fine until
here, but the following line gives me error:
Me.Cont_Month = DateSerial(Year(Cont_Month), Month(Cont_Month) + 1, 1)
the error msg:
Error 2467 - The expression you entered refers to an object that is closed
or doesn't exist.
so i want the new record's date to be 1 month after the previous record, how
can I do this? any help please?
the code:
Private Sub cmdNew_Click()
On Error GoTo Err_Handler
'Purpose: Duplicate the main form record and related records in the
subform.
Dim lngID As Long 'Primary key value of the new record.
Dim rst As DAO.Recordset
Dim strSql_ContMonthly As String 'SQL statement.
Dim strSql_VO As String 'SQL statement.
Dim strSql_Obs As String 'SQL statement.
Dim strSql_Ltr As String 'SQL statement.
Dim stDocName As String
Dim rst2 As DAO.Recordset
Dim ContNo As String
'Save and edits first
If Me.Dirty Then
Me.Dirty = False
End If
'Make sure there is a record to duplicate.
If Me.NewRecord Then
MsgBox "Go to previous record to copy last month's details."
Else
'Duplicate the main record: add to form's clone.
Set rst = Forms![Frm_Cont_Monthly_Details_Edit].RecordsetClone
rst.MoveLast
Forms![Frm_Cont_Monthly_Details_Edit].Bookmark = rst.Bookmark
strSql_ContMonthly = "INSERT INTO Tbl_Cont_Monthly_Change (
Cont_Month, Cont_Name, " & _
"Cont_Org_Value, Cont_Apr_Value,
Cont_Start_Date, Cont_Comp_Date, " & _
"Cont_Contractor, Cont_Consultant, Cont_Overall,
Cont_Comments, Contract_No ) " & _
"SELECT Tbl_Cont_Monthly_Change.Cont_Month,
Tbl_Cont_Monthly_Change.Cont_Name, " & _
"Tbl_Cont_Monthly_Change.Cont_Org_Value,
Tbl_Cont_Monthly_Change.Cont_Apr_Value, " & _
"Tbl_Cont_Monthly_Change.Cont_Start_Date,
Tbl_Cont_Monthly_Change.Cont_Comp_Date, " & _
"Tbl_Cont_Monthly_Change.Cont_Contractor,
Tbl_Cont_Monthly_Change.Cont_Consultant, " & _
"Tbl_Cont_Monthly_Change.Cont_Overall,
Tbl_Cont_Monthly_Change.Cont_Comments, " & _
"Tbl_Cont_Monthly_Change.Contract_No " & _
"FROM Tbl_Cont_Monthly_Change " & _
"WHERE (((Tbl_Cont_Monthly_Change.Cont_Month)=#"
& Me.Cont_Month & "#) AND ((Tbl_Cont_Monthly_Change.Contract_No)=""" &
Me.Contract_No & """));"
Debug.Print strSql_ContMonthly
DBEngine(0)(0).Execute strSql_ContMonthly, dbFailOnError
ContNo = Me.Contract_No
stDocName = "Frm_Cont_Monthly_Details_Edit"
DoCmd.close
DoCmd.OpenForm stDocName, , , "Contract_No = '" & ContNo & "'"
Set rst = Forms![Frm_Cont_Monthly_Details_Edit].RecordsetClone
rst.MoveLast
'this make the record shown is the last one.
Forms![Frm_Cont_Monthly_Details_Edit].Bookmark = rst.Bookmark
Me.Cont_Month = DateSerial(Year(Cont_Month), Month(Cont_Month) + 1, 1)
Set rst = Nothing
End If
Exit_Handler:
Exit Sub
Err_Handler:
MsgBox "Error " & Err.Number & " - " & Err.Description, , "cmdDupe_Click"
Resume Exit_Handler
End Sub
i'm using a code in the following form:
http://img169.imageshack.us/img169/3230/frmcontmonthlydetailsedqn4.jpg
when the user click on "Add New", the last record should be duplicated
according to the append query "strSql_ContMonthly", the the form is closed
and opened again so that the new record would be shown. it works fine until
here, but the following line gives me error:
Me.Cont_Month = DateSerial(Year(Cont_Month), Month(Cont_Month) + 1, 1)
the error msg:
Error 2467 - The expression you entered refers to an object that is closed
or doesn't exist.
so i want the new record's date to be 1 month after the previous record, how
can I do this? any help please?
the code:
Private Sub cmdNew_Click()
On Error GoTo Err_Handler
'Purpose: Duplicate the main form record and related records in the
subform.
Dim lngID As Long 'Primary key value of the new record.
Dim rst As DAO.Recordset
Dim strSql_ContMonthly As String 'SQL statement.
Dim strSql_VO As String 'SQL statement.
Dim strSql_Obs As String 'SQL statement.
Dim strSql_Ltr As String 'SQL statement.
Dim stDocName As String
Dim rst2 As DAO.Recordset
Dim ContNo As String
'Save and edits first
If Me.Dirty Then
Me.Dirty = False
End If
'Make sure there is a record to duplicate.
If Me.NewRecord Then
MsgBox "Go to previous record to copy last month's details."
Else
'Duplicate the main record: add to form's clone.
Set rst = Forms![Frm_Cont_Monthly_Details_Edit].RecordsetClone
rst.MoveLast
Forms![Frm_Cont_Monthly_Details_Edit].Bookmark = rst.Bookmark
strSql_ContMonthly = "INSERT INTO Tbl_Cont_Monthly_Change (
Cont_Month, Cont_Name, " & _
"Cont_Org_Value, Cont_Apr_Value,
Cont_Start_Date, Cont_Comp_Date, " & _
"Cont_Contractor, Cont_Consultant, Cont_Overall,
Cont_Comments, Contract_No ) " & _
"SELECT Tbl_Cont_Monthly_Change.Cont_Month,
Tbl_Cont_Monthly_Change.Cont_Name, " & _
"Tbl_Cont_Monthly_Change.Cont_Org_Value,
Tbl_Cont_Monthly_Change.Cont_Apr_Value, " & _
"Tbl_Cont_Monthly_Change.Cont_Start_Date,
Tbl_Cont_Monthly_Change.Cont_Comp_Date, " & _
"Tbl_Cont_Monthly_Change.Cont_Contractor,
Tbl_Cont_Monthly_Change.Cont_Consultant, " & _
"Tbl_Cont_Monthly_Change.Cont_Overall,
Tbl_Cont_Monthly_Change.Cont_Comments, " & _
"Tbl_Cont_Monthly_Change.Contract_No " & _
"FROM Tbl_Cont_Monthly_Change " & _
"WHERE (((Tbl_Cont_Monthly_Change.Cont_Month)=#"
& Me.Cont_Month & "#) AND ((Tbl_Cont_Monthly_Change.Contract_No)=""" &
Me.Contract_No & """));"
Debug.Print strSql_ContMonthly
DBEngine(0)(0).Execute strSql_ContMonthly, dbFailOnError
ContNo = Me.Contract_No
stDocName = "Frm_Cont_Monthly_Details_Edit"
DoCmd.close
DoCmd.OpenForm stDocName, , , "Contract_No = '" & ContNo & "'"
Set rst = Forms![Frm_Cont_Monthly_Details_Edit].RecordsetClone
rst.MoveLast
'this make the record shown is the last one.
Forms![Frm_Cont_Monthly_Details_Edit].Bookmark = rst.Bookmark
Me.Cont_Month = DateSerial(Year(Cont_Month), Month(Cont_Month) + 1, 1)
Set rst = Nothing
End If
Exit_Handler:
Exit Sub
Err_Handler:
MsgBox "Error " & Err.Number & " - " & Err.Description, , "cmdDupe_Click"
Resume Exit_Handler
End Sub