With a bound form you need to prevent the row being committed to the table in
any other way than via the command button, so first declare a Boolean
variable in the form's module's Declarations area:
Dim blnSaveMe as Boolean
In the form's BeforeUpdate event procedure set the return value of the
Cancel argument to True if the variable's value is False and reset the value
to False if it is True:
Cancel = Not blnSaveMe
blnSaveMe = False
In the button's Click event procedure set the value to True and save the
record:
blnSaveMe = True
RunCommand acCmdSaveRecord
There is no real answer as to whether this method is more 'correct' than
using an action query. The use of an unbound form from which the data is
written to the table by means of an SQL ststement can be more appropriate in
some circumstances. You can also use DAO or ADO to update the table. The
following is the code from a simple form's module which does this by
establishing a recordset of people selected by surname and populating the
controls on the form one record at a time. In this case each field IS
updated individually as soon as the user edits a control:
Option Compare Database
Option Explicit
Dim dbs As DAO.Database, rst As DAO.Recordset
Private Sub cboFindContact_AfterUpdate()
Dim strSQL As String
If Not IsNull(Me!cboFindContact) Then
strSQL = "SELECT * FROM Addresses WHERE LastName = " & _
Chr(34) & Me!cboFindContact & Chr(34)
Set rst = dbs.OpenRecordset(strSQL)
With rst
Me!txtFirstName = !FirstName
Me!txtLastName = !LastName
End With
Else
Set rst = Nothing
Me!txtFirstName = Null
Me!txtLastName = Null
End If
End Sub
Private Sub cmdNext_Click()
On Error Resume Next
With rst
.MoveNext
If Err = 0 Then
If .EOF Then .MoveLast
Me!txtFirstName = !FirstName
Me!txtLastName = !LastName
End If
End With
End Sub
Private Sub cmdPrevious_Click()
On Error Resume Next
With rst
.MovePrevious
If Err = 0 Then
If .BOF Then .MoveFirst
Me!txtFirstName = !FirstName
Me!txtLastName = !LastName
End If
End With
End Sub
Private Sub Form_Close()
On Error Resume Next
rst.Close
Set rst = Nothing
Set dbs = Nothing
End Sub
Private Sub Form_Load()
Set dbs = CurrentDb
End Sub
Private Sub txtFirstName_AfterUpdate()
With rst
.Edit
!FirstName = Me!txtFirstName
.Update
End With
End Sub
Private Sub txtLastName_AfterUpdate()
Reference:: Sikh Philosophy Network http://www.sikhphilosophy.net/showthread.php?t=6792Reference:: Sikh Philosophy Network http://www.sikhphilosophy.net/showthread.php?t=6792
With rst
.Edit
!LastName = Me!txtLastName
.Update
End With
Me!cboFindContact.Requery
End Sub
"Blair" wrote:
> I have a simple question. I have a form with several text boxes and a button
> to save the data. When the user clicks the save button the data will be
> added to the database. I only want the data entered when the button is
> pressed to stop partial info from being put in the database. I am wondering
> the best way to add this data to the database. My prof from my intro class
> said to use an action query but the more I research these I get the
> impression this is incorrect for this situation. My question is in this type
> of situation is an action query appropriate for this situation or is there a
> more correct way for this? Thanks in advance.....