Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does my Save Command make other command buttons stop worki? 4

Status
Not open for further replies.

tgikristi

Technical User
Jul 31, 2002
39
US
I have several forms with this problem...when a Save command button using code below is clicked, my other command buttons (which close the form or add a new record) won't work. I don't get an error message or anything, unless I click on Save again, and then I am told that Save is unavailable.

The Close and Add commands first use a true Me.Dirty statement to check req'd fields for null, force users to enter required information, and then save the record before closing or adding or they go straight to the close/add commands if Me.Dirty is false. These commands work if I haven't click the Save command first. Here is the Save code:

Private Sub Save_Click()
On Error GoTo Err_Save_Click

Dim Fld1 As Control '(etc)
Set Fld1 = Me("Product")
'...and so on for the rest of the fields

If IsNull(Fld1) Then
MsgBox "Record cannot be saved until required fields have data.", vbOKOnly
Fld1.SetFocus
Exit Sub
'...and so on for the rest of the fields

Else
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
End If

Exit_Save_Click:
Exit Sub
Err_Save_Click:
MsgBox Err.Description
Resume Exit_Save_Click
End Sub

Thanks in advance for any thoughts,
tgikristi
 
Hiya,

I do wish that you'd use correct structure - without 'Exit Sub'........

Private Sub Save_Click()
On Error GoTo Err_Save_Click
Dim Fld1 As Control '(etc)
Set Fld1 = Me("Product")
'...and so on for the rest of the fields

If (Not(IsNull(Fld1))) Then
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
Else
MsgBox "Record cannot be saved until required fields have data.", vbOKOnly
Fld1.SetFocus
Endif


Exit_Save_Click:
Exit Sub
Err_Save_Click:
MsgBox Err.Description
Resume Exit_Save_Click
End Sub

This will create exactly the same error, but at least the structure is correct....

Regards,

Darrylle "Never argue with an idiot, he'll bring you down to his level - then beat you with experience."
 
tgikristi,

I'm guessing here, but I would suspect that the problem lies with the code behind the Add and Close buttons.

When you save the record, the form's dirty property (ie. me.dirty) is automatically set to false once the record is saved. Thus if the Close and Add buttons are testing the dirty property after the save, they will always receive a dirty = false value. Thereafter, what happens will depend on your code behind the buttons.

Steve Lewy
Solutions Developer
steve@lewycomputing.com.au
(dont cut corners or you'll go round in circles)
 
Well, I got it to work in case anyone is interested, it involved changing my If Me.Dirty statements to If Not Me.Dirty and putting the 'close' or 'add' code directly below that, then adding the rest of the code (to check for nulls if dirty and save if no nulls) within the Else part.

I don't understand the logic of why this works and it didn't the other way (having the 'Close' or 'Add' code within the Else statement of a Me.Dirty), but I'm glad to have gotten it to work.

I tried removing all the Exit Sub's from my Is Null checks, and then the checks weren't performed, so I'm not sure how to solve that one.

Thanks for all the help,
tgikristi
 
Hi!

A short message for Darrylle. The structure you are suggesting will not work for what tgikristi is doing since the record will save if any of the fields are not null. If you want to do it without the Exit Subs then you would need to nest the if statements which would look a little untidy but will get rid of the Exit Subs. Obviously opinion goes both ways on this topic and I agree with you that the Exit Subs are an annoyance but, if someone else is maintaining the code, I usually put up with them since it makes the code more readable.

Jeff Bridgham
bridgham@purdue.edu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top