Hi All,
This has been discussed in an older thread, but I still wasn't able to get this resolved. I was informed to start a new thread and go into more detail so here goes:
1. I want my form to only add new records.
2. Like it should, when you start typing information in one of the fields, the autonumber increments.
3. I have a cancel button that works fine, although it bypasses the beforeupdate code. (I know this because I tested the value of the public variant after using the cancel button)
4. If I try to use the save button, it returns the msgbox from the else case of the beforeupdate event and does nothing else.
Here is the code:
[/SIZE][/FONT]
I have thought of a work around and that is to create an additional button and remove the Control Box / Close button from the form. The first button would cancel by cleaning the form and closing it out. The second would just clean the form and the last would save the record and execute the other necessary code. I would prefer to have a safeguard in there to prevent adding a record with out executing the SQL code to create the child records.
This has been discussed in an older thread, but I still wasn't able to get this resolved. I was informed to start a new thread and go into more detail so here goes:
1. I want my form to only add new records.
2. Like it should, when you start typing information in one of the fields, the autonumber increments.
3. I have a cancel button that works fine, although it bypasses the beforeupdate code. (I know this because I tested the value of the public variant after using the cancel button)
4. If I try to use the save button, it returns the msgbox from the else case of the beforeupdate event and does nothing else.
Here is the code:
Code:
[SIZE=3][FONT=Times New Roman]Private Sub Cancel_Click()[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]On Error GoTo Err_Cancel_Click[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]If Me.Dirty Then[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]End If[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] MyAction = "Cancel"[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] DoCmd.Close[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]Exit_Cancel_Click:[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] Exit Sub[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]Err_Cancel_Click:[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] MsgBox Err.Description[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] Resume Exit_Cancel_Click[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]End Sub[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]Private Sub Form_BeforeUpdate(Cancel As Integer)[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]Select Case MyAction[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] Case "Cancel"[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] Cancel = True[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] Exit Sub[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] Case "Save"[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] Case Else[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] MsgBox "Please press the Save or Cancel button", vbOKOnly[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] Cancel = True[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] Exit Sub[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]End Select[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]End Sub[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]Private Sub Form_Current()[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]MyAction = ""[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]End Sub[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]Private Sub AddMR_Click()[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]On Error GoTo Err_AddMR_Click[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman][SIZE=3][FONT=Times New Roman] [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] MyAction = "Save"[/FONT][/SIZE] [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman][/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] 'Save Record[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] 'Turn warnings off and insert records into related tables, then turn warnings back on[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] DoCmd.SetWarnings False[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] DoCmd.RunSQL "Insert INTO QUOTE(MRID) VALUES(" & Me.MRID & ")"[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] DoCmd.RunSQL "Insert INTO PO(MRID) VALUES(" & Me.MRID & ")"[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] DoCmd.RunSQL "Insert INTO PREQ(MRID) VALUES(" & Me.MRID & ")"[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] DoCmd.SetWarnings True[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] 'Declare variables for openform command[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] Dim stDocName As String[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] Dim stLinkCriteria As String[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] stDocName = "MRSTAT"[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] stLinkCriteria = "[MRID]=" & Me![MRID][/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman][/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] 'Close this form[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] DoCmd.Close[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] 'Open MRSTAT form to status new record[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] DoCmd.openForm stDocName, , , stLinkCriteria[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]Exit_AddMR_Click:[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] Exit Sub[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]Err_AddMR_Click:[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] MsgBox Err.Description[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] Resume Exit_AddMR_Click[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman] [/FONT][/SIZE]
[FONT=Times New Roman][SIZE=3]End Sub[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]
I have thought of a work around and that is to create an additional button and remove the Control Box / Close button from the form. The first button would cancel by cleaning the form and closing it out. The second would just clean the form and the last would save the record and execute the other necessary code. I would prefer to have a safeguard in there to prevent adding a record with out executing the SQL code to create the child records.