Save button

  • Thread starter injanib via AccessMonster.com
  • Start date
I

injanib via AccessMonster.com

How can I get the form to save data only by pressing the save button and not
by closing the form. Also, when I click the save button I want the form to
save the record and move on to a blank form ready for the next record and
place the cursor in the first field.

I also want the form to ask upon closing if I want to save the data if I
haven't already saved it.

Regards.
 
O

OldPro

How can I get the form to save data only by pressing the save button and not
by closing the form. Also, when I click the save button I want the form to
save the record and move on to a blank form ready for the next record and
place the cursor in the first field.

I also want the form to ask upon closing if I want to save the data if I
haven't already saved it.

Regards.

Access has two main methodologies; Access proper and Visual Basic for
Applications. Anytime you assign a recordset to a form, you are using
Access proper and must abide by Access rules. I find this very
restrictive, so I almost exclusively use VBA. It requires more
coding, but you gain a great deal of control over the finished
product.
To use VBA, open the recordset in the Form_Load event instead of
assigning it to the form recordsource property. Here is an example:
Private Sub Form_Load()
Set db = CurrentDb()
Set rsSomeTable = db.OpenRecordset("tblSomeTable", dbOpenDynaset)
If Not rsSomeTable.EOF Then
DisplayRecord
End If
End Sub

Put the following references at the top of the form code:

Private db as DAO.Database
Private rsSomeTable as DAO.Recordset

And be sure to close the database and the recordset in the Form_Unload
event:
Private sub Form_Unload()
rsSomeTable.close

End Sub
 
O

OldPro

Sorry, I wasn't quite finished...
Private sub Form_Unload()
rsSomeTable.close

End Sub

Private sub Form_Unload()
rsSomeTable.close
set rsSomeTable = Nothing
db.close
set db = Nothing
End Sub

The code to add or edit a record is simple:

Private sub cmdAdd()
rsSomeTable.Addnew ' Note: .Addnew to Add, .Edit to edit
rsSomeTable!ID=100
rsSomeTable!FirstName = txtFirstName
rsSomeTable!LastName = txtLastName
rsSomeTable.Update
End Sub
 
I

injanib via AccessMonster.com

I am sorry, but I am working on my very first Access project and am not very
familiar with it. Here is the part of your answere I don't quite understand.
Please elaborate a little more.

"To use VBA, open the recordset in the Form_Load event instead of
assigning it to the form recordsource property. Here is an example:
Private Sub Form_Load()
Set db = CurrentDb()
Set rsSomeTable = db.OpenRecordset("tblSomeTable", dbOpenDynaset)
If Not rsSomeTable.EOF Then
DisplayRecord
End If
End Sub"

How can I get the form to save data only by pressing the save button and not
by closing the form. Also, when I click the save button I want the form to
[quoted text clipped - 8 lines]
Access has two main methodologies; Access proper and Visual Basic for
Applications. Anytime you assign a recordset to a form, you are using
Access proper and must abide by Access rules. I find this very
restrictive, so I almost exclusively use VBA. It requires more
coding, but you gain a great deal of control over the finished
product.
To use VBA, open the recordset in the Form_Load event instead of
assigning it to the form recordsource property. Here is an example:
Private Sub Form_Load()
Set db = CurrentDb()
Set rsSomeTable = db.OpenRecordset("tblSomeTable", dbOpenDynaset)
If Not rsSomeTable.EOF Then
DisplayRecord
End If
End Sub

Put the following references at the top of the form code:

Private db as DAO.Database
Private rsSomeTable as DAO.Recordset

And be sure to close the database and the recordset in the Form_Unload
event:
Private sub Form_Unload()
rsSomeTable.close

End Sub
 
O

OldPro

I have added a few notes to the previous message:

I am sorry, but I am working on my very first Access project and am not very
familiar with it. Here is the part of your answere I don't quite understand.
Please elaborate a little more.

"To use VBA, open the recordset in the Form_Load event instead of
assigning it to the form recordsource property. Here is an example:
Private Sub Form_Load()

db has previously been declared as a database; here we are assigning
the current database to it, i.e. the actual Access project.
Set db = CurrentDb()

rsSomeTable (or whatever name you choose) has already been declared as
a recordset; here we are assigning it to a table. dbOpenDynaset
allows viewing and editing.
Set rsSomeTable = db.OpenRecordset("tblSomeTable", dbOpenDynaset)

We cannot display a record if the recordset is empty, so we must test
it first. Recordsets always open to the first record, if there is
one. Otherwise the End of File (.EOF) is set to true.
If Not rsSomeTable.EOF Then

Finally, you will need a function to display the record on the
screen. I put in DisplayRecord as a placeholder... you will have to
write your own function. Basically you will just assign each field in
the recordset to a textbox on the screen: txtSomeTextBox=rsSomeTable.
[SomeField]
DisplayRecord
End If
End Sub"




How can I get the form to save data only by pressing the save button and not
by closing the form. Also, when I click the save button I want the form to
[quoted text clipped - 8 lines]
Access has two main methodologies; Access proper and Visual Basic for
Applications. Anytime you assign a recordset to a form, you are using
Access proper and must abide by Access rules. I find this very
restrictive, so I almost exclusively use VBA. It requires more
coding, but you gain a great deal of control over the finished
product.
To use VBA, open the recordset in the Form_Load event instead of
assigning it to the form recordsource property. Here is an example:
Private Sub Form_Load()
Set db = CurrentDb()
Set rsSomeTable = db.OpenRecordset("tblSomeTable", dbOpenDynaset)
If Not rsSomeTable.EOF Then
DisplayRecord
End If
End Sub
Put the following references at the top of the form code:
Private db as DAO.Database
Private rsSomeTable as DAO.Recordset
And be sure to close the database and the recordset in the Form_Unload
event:
Private sub Form_Unload()
rsSomeTable.close
 

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