Save and Print the form

P

Patti

Hi!

Almost done with my form -- Access 97. I want to have the
last option be a message box which allows users to select
Yes to save the form or to select No to delete the
record. I've entered the following code from Jim
Allensworth in the Form's BeforeUpdate:

If Me.Dirty Then
If MsgBox ("Save changes?", vbYesNo) = vbNo Then
Me.Undo
Else
Me.Dirty = False
End If
End If

This works fine if No is selected, but I get the following
error if Yes is selected:

Run-time error 2115
The macro or function set to the BeforeUpdate or
Validation Rule property for this field is preventing
Access from saving the data in the field.

I think the problem might be some other code that I
snagged from Allen Browne's website that allows the user
to print the form:

Private Sub cmdPrint_Click()
Dim strWhere As String

If Me.Dirty Then 'Save any edits.
Me.Dirty = False
End If

If Me.NewRecord Then 'Check there is a record to print
MsgBox "Select a record to print"
Else
strWhere = "[ID] = " & Me.[ID]
DoCmd.OpenReport "MyReport", acViewPreview, ,
strWhere
End If
End Sub

I think I can combine these two codes -- Yes equals Save
and Print and No equals Delete, but I'm not sure how. Any
ideas?

Thanks!
 
A

Allen Browne

The Form's BeforeUpdate event fires just before Access writes the record to
disk. There is no need to set the Dirty property to False, because that's
what Access is about to do anyway. There is no need to test the form's Dirty
property, because the event will not fire is the form is not dirty. All that
is automatic, so the only thing you need to do is cancel the event if you
don't want the change, and undo the form if you want to destroy the change.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If MsgBox ("Save changes?", vbYesNo) = vbNo Then
Cancel = True
Me.Undo
End If
End Sub
 
P

Patti

Hi Allen,

Thanks! But how do I weave the print option in?
-----Original Message-----
The Form's BeforeUpdate event fires just before Access writes the record to
disk. There is no need to set the Dirty property to False, because that's
what Access is about to do anyway. There is no need to test the form's Dirty
property, because the event will not fire is the form is not dirty. All that
is automatic, so the only thing you need to do is cancel the event if you
don't want the change, and undo the form if you want to destroy the change.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If MsgBox ("Save changes?", vbYesNo) = vbNo Then
Cancel = True
Me.Undo
End If
End Sub

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Hi!

Almost done with my form -- Access 97. I want to have the
last option be a message box which allows users to select
Yes to save the form or to select No to delete the
record. I've entered the following code from Jim
Allensworth in the Form's BeforeUpdate:

If Me.Dirty Then
If MsgBox ("Save changes?", vbYesNo) = vbNo Then
Me.Undo
Else
Me.Dirty = False
End If
End If

This works fine if No is selected, but I get the following
error if Yes is selected:

Run-time error 2115
The macro or function set to the BeforeUpdate or
Validation Rule property for this field is preventing
Access from saving the data in the field.

I think the problem might be some other code that I
snagged from Allen Browne's website that allows the user
to print the form:

Private Sub cmdPrint_Click()
Dim strWhere As String

If Me.Dirty Then 'Save any edits.
Me.Dirty = False
End If

If Me.NewRecord Then 'Check there is a record to print
MsgBox "Select a record to print"
Else
strWhere = "[ID] = " & Me.[ID]
DoCmd.OpenReport "MyReport", acViewPreview, ,
strWhere
End If
End Sub

I think I can combine these two codes -- Yes equals Save
and Print and No equals Delete, but I'm not sure how. Any
ideas?

Thanks!


.
 
A

Allen Browne

I take it you *always* want to print the record if it saves?

If so, use the form's AfterUpdate event to print it:

Private Sub Form_BeforeUpdate
Dim strWhere As String
strWhere = "[ID] = " & Me.[ID]
DoCmd.OpenReport "MyReport", acViewPreview, , strWhere
End Sub

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Patti said:
Hi Allen,

Thanks! But how do I weave the print option in?
-----Original Message-----
The Form's BeforeUpdate event fires just before Access writes the record to
disk. There is no need to set the Dirty property to False, because that's
what Access is about to do anyway. There is no need to test the form's Dirty
property, because the event will not fire is the form is not dirty. All that
is automatic, so the only thing you need to do is cancel the event if you
don't want the change, and undo the form if you want to destroy the change.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If MsgBox ("Save changes?", vbYesNo) = vbNo Then
Cancel = True
Me.Undo
End If
End Sub

Hi!

Almost done with my form -- Access 97. I want to have the
last option be a message box which allows users to select
Yes to save the form or to select No to delete the
record. I've entered the following code from Jim
Allensworth in the Form's BeforeUpdate:

If Me.Dirty Then
If MsgBox ("Save changes?", vbYesNo) = vbNo Then
Me.Undo
Else
Me.Dirty = False
End If
End If

This works fine if No is selected, but I get the following
error if Yes is selected:

Run-time error 2115
The macro or function set to the BeforeUpdate or
Validation Rule property for this field is preventing
Access from saving the data in the field.

I think the problem might be some other code that I
snagged from Allen Browne's website that allows the user
to print the form:

Private Sub cmdPrint_Click()
Dim strWhere As String

If Me.Dirty Then 'Save any edits.
Me.Dirty = False
End If

If Me.NewRecord Then 'Check there is a record to print
MsgBox "Select a record to print"
Else
strWhere = "[ID] = " & Me.[ID]
DoCmd.OpenReport "MyReport", acViewPreview, ,
strWhere
End If
End Sub

I think I can combine these two codes -- Yes equals Save
and Print and No equals Delete, but I'm not sure how. Any
ideas?

Thanks!
 
P

Patti

Thanks so much, Allen! That's exactly what I was looking
for!
-----Original Message-----
I take it you *always* want to print the record if it saves?

If so, use the form's AfterUpdate event to print it:

Private Sub Form_BeforeUpdate
Dim strWhere As String
strWhere = "[ID] = " & Me.[ID]
DoCmd.OpenReport "MyReport", acViewPreview, , strWhere
End Sub

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Hi Allen,

Thanks! But how do I weave the print option in?
-----Original Message-----
The Form's BeforeUpdate event fires just before Access writes the record to
disk. There is no need to set the Dirty property to False, because that's
what Access is about to do anyway. There is no need to test the form's Dirty
property, because the event will not fire is the form is not dirty. All that
is automatic, so the only thing you need to do is cancel the event if you
don't want the change, and undo the form if you want to destroy the change.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If MsgBox ("Save changes?", vbYesNo) = vbNo Then
Cancel = True
Me.Undo
End If
End Sub

Hi!

Almost done with my form -- Access 97. I want to have the
last option be a message box which allows users to select
Yes to save the form or to select No to delete the
record. I've entered the following code from Jim
Allensworth in the Form's BeforeUpdate:

If Me.Dirty Then
If MsgBox ("Save changes?", vbYesNo) = vbNo Then
Me.Undo
Else
Me.Dirty = False
End If
End If

This works fine if No is selected, but I get the following
error if Yes is selected:

Run-time error 2115
The macro or function set to the BeforeUpdate or
Validation Rule property for this field is preventing
Access from saving the data in the field.

I think the problem might be some other code that I
snagged from Allen Browne's website that allows the user
to print the form:

Private Sub cmdPrint_Click()
Dim strWhere As String

If Me.Dirty Then 'Save any edits.
Me.Dirty = False
End If

If Me.NewRecord Then 'Check there is a record to print
MsgBox "Select a record to print"
Else
strWhere = "[ID] = " & Me.[ID]
DoCmd.OpenReport "MyReport", acViewPreview, ,
strWhere
End If
End Sub

I think I can combine these two codes -- Yes equals Save
and Print and No equals Delete, but I'm not sure how. Any
ideas?

Thanks!


.
 
A

Allen Browne

Great. Looks like you figured it out, even though I had BeforeUpdate in the
example.

BTW, if you only want to print after a new record (not after any change),
the form's AfterInsert event could be better still.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Patti said:
Thanks so much, Allen! That's exactly what I was looking
for!
-----Original Message-----
I take it you *always* want to print the record if it saves?

If so, use the form's AfterUpdate event to print it:

Private Sub Form_BeforeUpdate
Dim strWhere As String
strWhere = "[ID] = " & Me.[ID]
DoCmd.OpenReport "MyReport", acViewPreview, , strWhere
End Sub


Hi Allen,

Thanks! But how do I weave the print option in?
 

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

Similar Threads


Top