Hi Crystal,
I tried to write the code to combine cmdSave and Form_BeforeUpdate together
in Form_BeforeUpdate, but I could not make them to work well. I would like to
save the record if the user fills in the ClassID, ClassName and auto sign
the StudentID, otherwise, the recode will be deleted.
Here is the code I have now as a separate code. It is works but may not
looks good. Do you have any advice? Thanks!
Private Sub cmdSaveClass_Click()
On Error GoTo Err_cmdSaveClass_Click
If IsNull(Me.ClassID) Then
MsgBox ("Class ID is required, please fill it in")
Me.ClassID.SetFocus
Exit Sub
End If
If IsNull(Me.ClassName) Then
MsgBox ("Class Name is required, please fill it in")
Me.ClassName.SetFocus
Exit Sub
End If
Me.StudentID = Forms!fStudents.StudentID
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
Exit_cmdSaveClass_Click:
Exit Sub
Err_cmdSaveClass_Click:
MsgBox Err.Description
Resume Exit_cmdSaveClass_Click
End Sub
____________________________________________________________________
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.ClassID) Or IsNull(Me.ClassName) Or
IsNull(Me.StudentID) Then
Cancel = True
End If
End Sub
:
Hi Ac (what is your name?)
"I have a Save button on the
fClass Form and the code to handle if the user left the StudentID as empty"
you should put code on the form BeforeUpdate event so the update can be
canceled if required information is not filled out -- them, there is no
need to delete the record as it will not be saved <smile>
to validate a record and prevent it from being saved, put code in the
form BeforeUpdate event
'~~~~~~~~~~~~~~~~~~~~
'----------------- make sure all required data is filled out
'make sure SomeControlName is filled out
If IsNull(me.SomeControlName) then
'if it is not filled out, then move the focus to that control
me.SomeControlName.setFocus
'give the user a message
msgbox "You must enter Some Data",,"Missing Data"
'if this is a combobox, drop the list for them
me.SomeControlName.dropDown
'don't save the record yet
Cancel = true
'quit checking and give them a chance to fill it out
exit sub
end if
'~~~~~~~~~~~~~~~~~~~~
if you have a SAVE button on your form, here is some code:
'~~~~~~~~~~~~~~~~~~~~
if me.dirty then me.dirty = false
'~~~~~~~~~~~~~~~~~~~~
"dirty" is a form property that gets set to TRUE if a record has changes
that need to be saved
if me.dirty then
is the same thing as
if me.dirty=True then
me.dirty = false
is just another way to save a record
Warm Regards,
Crystal
Access Basics
8-part free tutorial that covers essentials in Access
http://www.AccessMVP.com/strive4peace
*
have an awesome day
*
Ac wrote:
Hi Crystal,
I will.
I have another problem related to this program. I have a Save button on the
fClass Form and the code to handle if the user left the StudentID as empty;
but if a user input only few information on the Class Form, instead of saving
it, he closes the form and left the SudentID as blank; how can I delete this
record from Class table with the StudentID as blank?
:
"you have a very good Access tutorial website"
thank you, Ac
"Do you have a SQL Server 2005 tutorial website too?"
no, I do not -- but if you find a good one, please let me know!
strive4peace2006 at yahoo.com
Warm Regards,
Crystal
Access Basics
8-part free tutorial that covers essentials in Access
http://www.AccessMVP.com/strive4peace
*
have an awesome day
*
Ac wrote:
Thanks again.
I appreciate your information; you have a very good Access tutorial website.
Do you have a SQL Server 2005 tutorial website too? Thanks.
:
Hi Ac (what is your name?)
you're welcome
"dirty" is a property that gets set to TRUE if a record has changes that
need to be saved
if me.dirty then
is the same thing as
if me.dirty=True then
me.dirty = false
is just another way to save a record
currentdb.tabledefs.refresh
refreshes the table definitions of the current database with changes
made by other users or processes
Warm Regards,
Crystal
Access Basics
8-part free tutorial that covers essentials in Access
http://www.AccessMVP.com/strive4peace
*
have an awesome day
*
Ac wrote:
Hi Crystal,
Thank you for your great help, it works well!
I also want to learn some part of your code which I have never used before.
1. What is the “dirty†(if me.dirty then me.dirty = false)? and
2. What is the “Tabledefs†(currentdb.tabledefs.refresh)?
Thanks again.
:
put this code behind the form for the Click event of your commend button
(the property sheet will say [Event Procedure])
'~~~~~~~~~~~~~~~
'save record if changes have been made
if me.dirty then me.dirty = false
if me.NewRecord then
msgbox "You are on a new record",,"Cannot delete"
exit sub
end if