1レコードのみ
Option Compare Database
Option Explicit
Dim flg As Boolean
Private Sub cmdA_Click()
If IsNull(Me.txtA) Or IsNull(Me.txtB) Then
MsgBox "未入力があります。"
If MsgBox("入力データは保存されません。 終了しますか?", vbYesNo + vbQuestion) = vbYes Then
flg = False
DoCmd.Close acForm, "f_new"
Else
Me.txtA.SetFocus
End If
Else
If MsgBox("入力データを保存して、終了しますか?", vbYesNo + vbQuestion) = vbYes Then
DoCmd.Close acForm, "f_new"
Else
MsgBox "保存しないで終了します。"
flg = False
DoCmd.Close acForm, "f_new"
End If
End If
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If flg = False Then
Cancel = True
End If
End Sub
複数レコード
Private Sub Form_BeforeInsert(Cancel As Integer)
cnt = DCount("*", "t_仮")
If cnt > 0 Then
Me.txtID = DMax("ID", "t_仮") + 1
Else
Me.txtID = DMax("ID", "t_A") + 1
End If
End Sub
Private Sub cmdEnd_Click()
Dim strSQL As String
DoCmd.SetWarnings False
If IsNull(Me.txtA) Or IsNull(Me.txtB) Then
MsgBox "未入力があります。"
If MsgBox("入力データは保存されません。 終了しますか?", vbYesNo + vbQuestion) = vbYes Then
DoCmd.Close acForm, "f_A"
strSQL = "DELETE * FROM t_仮"
DoCmd.RunSQL strSQL
Else
Me.txtA.SetFocus
End If
Else
If MsgBox("入力データを保存して、終了しますか?", vbYesNo + vbQuestion) = vbYes Then
DoCmd.Close acForm, "f_A"
strSQL = "INSERT INTO t_A ( ID, AA, BB ) " _
& "SELECT t_仮.ID, t_仮.AA, t_仮.BB FROM t_仮"
DoCmd.RunSQL strSQL
strSQL = "INSERT INTO t_B ( ID ) " _
& "SELECT t_仮.ID FROM t_仮"
DoCmd.RunSQL strSQL
strSQL = "INSERT INTO t_C ( ID ) " _
& "SELECT t_仮.ID FROM t_仮"
DoCmd.RunSQL strSQL
strSQL = "DELETE * FROM t_仮"
DoCmd.RunSQL strSQL
Else
MsgBox "保存しないで終了します。"
DoCmd.Close acForm, "f_A"
strSQL = "DELETE * FROM t_仮"
DoCmd.RunSQL strSQL
End If
End If
DoCmd.SetWarnings True
End Sub
最終更新:2012年03月22日 17:25