Save record immediately?

J

Jay

I have a simple database with a data entry form for adding records to the
main table [with a Date field, default value Date() ]

On that form is a command button which runs a query to show the records
added that day. However, if the user has completed the fields on the form
but not navigated to the next new record, that record does not show in the
query.

How can I get a record still showing in the form (just entered) to be
included in a query of records added that day?

Any help gratefully received.

Cheers......Jason
 
S

Stefan Hoffmann

hi Jay,
How can I get a record still showing in the form (just entered) to be
included in a query of records added that day?
You may use:

If Me.Dirty Then
Me.Dirty = False
End if

Setting the Dirty property to False causes Access to save the record.

Use it in your procedure like this, as errors may occure:

Private Sub cmdShowQuery_Click()

On Local Error GoTo LocalError

If Me.Dirty Then
Me.Dirty = False
End If

DoCmd.OpenQuery "yourQuery"

Exit Sub

LocalError:
MsgBox "Need error handling here.", vbCritical

End Sub


mfG
--> stefan <--
 
G

Guest

Hi Jason,

This should be very easy. You simply need to save (commit) the record before
opening the query. Of course, your users will first need to fill in any
required fields. Something like this, for a command button named
"cmdShowNewRecords":


Private Sub cmdShowNewRecords_Click()
On Error GoTo ProcError

If Me.Dirty = True Then 'Save the record first
Me.Dirty = False
End If

DoCmd.OpenQuery "NameOfQueryInQuotes"

ExitProc:
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in procedure cmdShowNewRecords_Click..."
Resume ExitProc
End Sub



Tom Wickerath
Microsoft Access MVP
https://mvp.support.microsoft.com/profile/Tom
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
 
G

Guest

PS. For bound forms (a bound form is a form that has a record source
indicated in the record source property), you should put similar code to
check for the form's dirty property in the click event procedure for a button
to close the form. Here's why:

Losing data when you close a form
http://allenbrowne.com/bug-01.html


Do not try to test for the .dirty property for unbound forms (ie. forms that
have no record source). Doing so will generate a run-time error.


Tom Wickerath
Microsoft Access MVP
https://mvp.support.microsoft.com/profile/Tom
http://www.access.qbuilt.com/html/expert_contributors.html
 

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