Not saving the record to my table. Any ideas why?

G

Guest

I have one button that is the "exit" button for my data entry form. The idea
is that people can enter data directly to the linked table (create a new
record and add info) and then return to the previous search form. For some
reason, the information isn't being saved to the table before it returns to
the search form. Anyone have any ideas why? Code begins (break lines
added for clarity):

Option Explicit
------------------------------------------------------
Private Sub cmdBackToSearch_Click()
On Error GoTo Err_cmdBackToSearch_Click

Dim stDocName As String
Dim stLinkCriteria As String
DoCmd.Close
stDocName = "frmSearch"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_cmdBackToSearch_Click:
Exit Sub

Err_cmdBackToSearch_Click:
MsgBox Err.Description
Resume Exit_cmdBackToSearch_Click

End Sub

--------------------------------------------------------------------------

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
strMsg = "Data has changed."
strMsg = strMsg & " Do you wish to save the changes?"
strMsg = strMsg & " Click Yes to save record or No to exit without
saving."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Save this Record?") = vbYes Then

Else
DoCmd.RunCommand acCmdUndo

End If
End Sub

----------------------------------------------------------------------------

Private Sub Form_Open(Cancel As Integer)
DoCmd.GoToRecord , , acNewRec

End Sub
 
C

Carl Rapson

J. Keggerlord said:
I have one button that is the "exit" button for my data entry form. The
idea
is that people can enter data directly to the linked table (create a new
record and add info) and then return to the previous search form. For
some
reason, the information isn't being saved to the table before it returns
to
the search form. Anyone have any ideas why? Code begins (break lines
added for clarity):

Option Explicit
------------------------------------------------------
Private Sub cmdBackToSearch_Click()
On Error GoTo Err_cmdBackToSearch_Click

Dim stDocName As String
Dim stLinkCriteria As String
DoCmd.Close
stDocName = "frmSearch"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_cmdBackToSearch_Click:
Exit Sub

Err_cmdBackToSearch_Click:
MsgBox Err.Description
Resume Exit_cmdBackToSearch_Click

End Sub

--------------------------------------------------------------------------

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
strMsg = "Data has changed."
strMsg = strMsg & " Do you wish to save the changes?"
strMsg = strMsg & " Click Yes to save record or No to exit without
saving."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Save this Record?") = vbYes
Then

Else
DoCmd.RunCommand acCmdUndo

End If
End Sub

----------------------------------------------------------------------------

Private Sub Form_Open(Cancel As Integer)
DoCmd.GoToRecord , , acNewRec

End Sub

If you're adding new records, you might want to try using the BeforeInsert
event instead of the BeforeUpdate event.

Carl Rapson
 
G

Guest

Wouldn't that mean the user would have to save the record before entering any
data? Ideally, I want them to have an "opt out" feature after entering
information. Plus, even without the BeforeUpdate code string, it wasn't
saving data before closing. Is there a line I might need to add to my
cmdBackToSearch_Click code? Or do I have something out of order in my code
sequence?
 
A

Allen Browne

There is a bug with the Close action/method, such that Access silently loses
the entry (no warning) if the record cannot be saved for any reason (e.g.
required field missed, or index duplicated.)

Details and workaround:
Losing data when you close a form
at:
http://allenbrowne.com/bug-01.html
 
G

Guest

Oh god.... Okay, I feel foolish now. The database did exactly what you've
described, which is exactly what I asked it to do. One of the fields is set
to "required" in the table. I went back and was able to save records as long
as that field is filled out. Aside from putting an asterisk next to the
field and denoting the asterisked fields as being "required" in text on my
form, is there a good way to warn people that their record won't be saved if
they don't fill it out?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top