J
Jay
Hi,
I'm new to access and have been trying to understand the event model for
forms in access and am having trouble understanding some of the basics.
For example, I created a simple database just for testing purposes, it has
one table. This table has just two fields, an autonumber field used as the
primary key, and a text data field.
Then I created a form that was bound to the table. The form has a text box
control bound to the data field in the table and a command button for
adding a new record to the table.
The intention was to create a form where the only way a new record would be
entered into the table was if the "Add Record" button on the form was
clicked.
Here is the code that I have to try and accomplish this. Basically I am
using a variable called OkToAdd that is checked in the form before_update
procedure to determine whether or not to cancel the event. When the form
is loaded OkToAdd is set to false, and is only set to true in "Add Record"
buttons on_click event handler.
This seems to work in that it does prevent the new record from being saved
in the table, but when the form is closed by hitting the close button on
the forms title bar, I get a warning message from Access saying :
"You can't save this record at this time. Microsoft access may have
encountered an error while trying to save a record. If you close this
object now, the data changes you made will be lost. Do you want to close
the database object anyway"
If I say Yes then the form closes and the record is not added, but how do I
prevent this message from popping up.
Is this an appropriate approach to forcing the user to click the "Add
Record" button before any table additions are made, or is their a better
way.
Thanks.
----------------------------------------------------------
Option Compare Database
Option Explicit
Dim OkToAdd As Boolean
----------------------------------------------------------
Private Sub AddRecordBtn_Click()
On Error GoTo Err_AddRecordBtn_Click
MsgBox "entering add record button click event handler"
OkToAdd = True
DoCmd.GoToRecord , , acNewRec
Exit_AddRecordBtn_Click:
Exit Sub
Err_AddRecordBtn_Click:
MsgBox Err.Description
Resume Exit_AddRecordBtn_Click
End Sub
-----------------------------------------------------------
Private Sub Form_AfterUpdate()
MsgBox "entering form after update event handler"
OkToAdd = False 'setting up for the next possible record
End Sub
------------------------------------------------------------
Private Sub Form_BeforeUpdate(Cancel As Integer)
MsgBox "entering form before update event handler"
Me.Undo
Cancel = Not OkToAdd
End Sub
I'm new to access and have been trying to understand the event model for
forms in access and am having trouble understanding some of the basics.
For example, I created a simple database just for testing purposes, it has
one table. This table has just two fields, an autonumber field used as the
primary key, and a text data field.
Then I created a form that was bound to the table. The form has a text box
control bound to the data field in the table and a command button for
adding a new record to the table.
The intention was to create a form where the only way a new record would be
entered into the table was if the "Add Record" button on the form was
clicked.
Here is the code that I have to try and accomplish this. Basically I am
using a variable called OkToAdd that is checked in the form before_update
procedure to determine whether or not to cancel the event. When the form
is loaded OkToAdd is set to false, and is only set to true in "Add Record"
buttons on_click event handler.
This seems to work in that it does prevent the new record from being saved
in the table, but when the form is closed by hitting the close button on
the forms title bar, I get a warning message from Access saying :
"You can't save this record at this time. Microsoft access may have
encountered an error while trying to save a record. If you close this
object now, the data changes you made will be lost. Do you want to close
the database object anyway"
If I say Yes then the form closes and the record is not added, but how do I
prevent this message from popping up.
Is this an appropriate approach to forcing the user to click the "Add
Record" button before any table additions are made, or is their a better
way.
Thanks.
----------------------------------------------------------
Option Compare Database
Option Explicit
Dim OkToAdd As Boolean
----------------------------------------------------------
Private Sub AddRecordBtn_Click()
On Error GoTo Err_AddRecordBtn_Click
MsgBox "entering add record button click event handler"
OkToAdd = True
DoCmd.GoToRecord , , acNewRec
Exit_AddRecordBtn_Click:
Exit Sub
Err_AddRecordBtn_Click:
MsgBox Err.Description
Resume Exit_AddRecordBtn_Click
End Sub
-----------------------------------------------------------
Private Sub Form_AfterUpdate()
MsgBox "entering form after update event handler"
OkToAdd = False 'setting up for the next possible record
End Sub
------------------------------------------------------------
Private Sub Form_BeforeUpdate(Cancel As Integer)
MsgBox "entering form before update event handler"
Me.Undo
Cancel = Not OkToAdd
End Sub