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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Operation Not Allowed

Status
Not open for further replies.

sanan

Technical User
Apr 15, 2004
139
IR
Hi Every Body
I have an Easy question for some of you Experts, so Please excuse me.
Here is the Scenario.
I have a Form, which is made of 2 Text Boxes, Text Box A and B, I want to stop the Operation or not to be able to close my Form when ever the Subtraction of the Values of my 2 Text Boxes are not equal to Zero, The Following should work But I just can not make work for me for now:
Private Sub Form_Close()
If A.Value - B.Value <> 0 Then

MsgBox ("The Entries are Not Equal")
Select Case Err.Number
Case 3219 'operation not allowed``
End select
End If
End Sub

Could some one let me know what is it that I am doing wrong?

Best Regards
Sanan
 
I think you want DoCmd.CancelEvent but you have to use it on the unload or beforeupdate events rather than close.
HTH
Peter
 
Add the following line in the Form_Unload event

Cancel = True

to prevent the form from closing
 
Hi hneal98, and PJStephenson

Thank you for your replies
Particularly PJStephenson, I tried your suggestion, and It worked.
But sort of disaster has happened, In Actuality the 2 Text Boxes (A, B) are located in a Sub Form of My Original form.
And Using the PJStephenson’s Suggestion I used DoCmd.CancelEvent in Unload Event of my Original form, and It worked, But If the Result of Subtraction of the 2 Textboxes Were not Equal to Zero, I just Could not Go On, and Even I could not go to my Design View in order to do some Changes on my Codes, I just had to make the value of the 2 Boxes equal to each other in order to be able to Proceed,
But I also experienced another problem with this Technique, as I explained the 2 Text Boxes are in a Sub Form and As I moved to create a new Record, Even if the Previous Entries in the 2 Text Boxes of my Sub Form were not Equal, I still could make a new Record and then close my Original Form. Therefore It works If I want to close my Original form and every thing would Froze and my MsgBox of “A is not Equal to B” would show up, But if I just go to make a new Record, Then I could Close my Original Form and It would not work.

But here is the Disaster that have happened, as I was experimenting with different Events of my subform and basically using the same set of codes in different events and try to work it out, using some unequal values for my Textboxes. I closed the Original form then I opened the Subform, and since it had some Unequal entries, Now I JUST CAN NOT CLOSE MY SUBFORM, NO MATTER WHAT I DO, I CAN NOT EVEN TURN OFF MY COMOPUTER.

Please HELP
Best Regards
sanan
 
Try doing the <CTRL> <BREAK> combination. That should break you out of the situation. Also, I would not add the code to the above by itself in the unload event. You need to have something also there that states if the two text boxes are not equal, then cancel, otherwise, let it go through.
 
Hi hneal98, and PJStephenson

Thank you for your replies
I had some success with your suggestion But I am not quite there yet.
Here is the problem I am experiencing with it.
First I explain the situation, I have a Form and a Subform in it. My SubForm Propertie’s default View is DataSheet. There are 2 Text Boxes in My SubForm that The Entries on them must be Equal. In Order to make my Users Enter the Equal entries Incase the Entries are not equal, they are not supposed to be able to Close the Original Form Nor they should be able to Add a New Record to Original Form (Some thing Like froze should Happen, “Operation Not Allowed”)
If the Entries are not Equal, I succeeded on Don’t let them to Close the Form But they still could Add a New Record, In Order to fix this I removed My Original Navigation Buttons, and I added my Own Buttons, And in the OnClick Event of my Add New Record Button I basically Added the Following Codes:

Dim strMsg As String, strTitle As String
strMsg = "Entries Are Not Equal."
strTitle = "Admission10"
intStyle = vbOKOnly

If Sd.Value - SC.Value <> 0 Then
MsgBox strMsg, intStyle, strTitle
DoCmd.CancelEvent
End If

Which are the Exact Codes as my Original form’s Unload event.

The above works only when I Use the Tab Button After Entering into my 2 Text Boxes, But if after entering some value in the Second Text Box I do not use the Tab Button, I still can Add a New Record even if my Entries are not Equal, And Finally Close the Form.

Any suggestions?

Best Regards
Sanan

 
Ok, here is some code I wrote to check if details had been changed and if so log them. It uses the BeforeUpdate event which runs before a form is closed or before the record is changed ie next, previous, newrec,
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
DoCmd.SetWarnings False
If Me.Dirty = True And Me.NewRecord = False Then
    If MsgBox("Do you want to save changes?", vbYesNo) = vbNo Then
       Me.Undo
      ' DoCmd.CancelEvent
    Else
        i = Me.Controls.Count
        For j = 0 To i - 1
            If Me.Controls(j).ControlType <> 100 And Me.Controls(j).ControlType <> 104 Then
                If Me.Controls(j).Value <> Me.Controls(j).OldValue Then
                    'MsgBox Me.Name & ", " & Me.Controls(j).Name & ", was: " & Me.Controls(j).OldValue & " ,is now: " & Me.Controls(j).Value & ", Changed by: " & Environ("USERNAME")
                    FillChangeControl Environ("USERNAME") & " at " & Format(Now(), "dd/mm/yyyy HH:nn:ss"), Me.Name, Me!BrokerID.Value, Me.Controls(j).Name & ", was: " & Me.Controls(j).OldValue & " ,is now: " & Me.Controls(j).Value
                End If
            End If
        Next
    End If
End If
DoCmd.SetWarnings True
End Sub
see if you can make something out of that
note me.undo, me.dirty and me.newrecord
although I haven't used cancelevent you will want to.
HTH
Peter
 
Hi Peter
Thanks a lot for reply let me look at carefully, And I will let you know.
Best regards
Biajn
 
Hi PJStephenson
Thanks again for your reply, But I am not so advance in VB so I still can not what you ae doing.
Off course what I want to do Should not be so complicated.

If I have 2 Buttons (One for Refreshing my Sub form, and the other for Adding a new Record to my Original form every thing works fine, I tried it with 2 Buttons)
Basically my challenge is to do the Job of 2 Buttons by Only 1 (my Adding a New Record Button).

I have the Following Codes, I do not understand why it does not work:
Private Sub Command108_Click()
DoCmd.DoMenuItem , , [A Details], 5, 70
If Me!Sd <> Me!SC Then
MsgBox "Entries are out of Balance", vbOKOnly, "Admin101"
DoCmd.CancelEvent
Else
DoCmd.GoToRecord , , acNewRec
End If
End Sub

[A details] is my sub Form
As you can see the following line should Refresh my Sub Form:
DoCmd.DoMenuItem , , [A Details], 5, 70

And the rest of codes should check if the value of my text Boxes are equal or not.
I should mention, that They work if they are in 2 Different Sub.

But It just Dose not work or Add new Record right at the beginning.

Best Regards
sanan
 
In the code you have posted there is no need to cancel event. I am assuming that this is your add new record button script. To refresh your subform you might need to use me.[subform].refresh or me.[subform].requery depending on what you are trying to do, try both and use help to make it work.
Peter
 
Hi PJStephenson
Thanks again for your reply.
Following is the code for my Add New Record Button:
And It works and adds new Record when it is Pushed,
About canceling event, I do want to cancel event, because my Users are not supposed to Enter Un Equal Values in my Text Boxes.

Private Sub Command108_Click()


If Me!Sd <> Me!SC Then
MsgBox "Entries are out of Balance", vbOKOnly, "Admin101"
DoCmd.CancelEvent
Else
DoCmd.GoToRecord , , acNewRec
End If
End Sub

As I add the following line of codes: (Codes to refresh my SubForm)
DoCmd.DoMenuItem , , [A Details], 5, 70
Or your Suggestions of me.[subform].refresh To my Previous Add New Record Button’s Click Event,:
Private Sub Command108_Click()
me.[subform].refresh
me.Form.refresh
If Me!Sd <> Me!SC Then
MsgBox "Entries are out of Balance", vbOKOnly, "Admin101"
DoCmd.CancelEvent
Else
DoCmd.GoToRecord , , acNewRec
End If
End Sub

My Add New Button Stops adding new Records. Why is it so? How can I fix that?

Best Regards
Sanan

 
Hi PJStephenson
I have a new Idea.
It seems like, We can not do 2 Events after Each other, I have an Idea or suggestion the Could work as well.
And That is that we have to make the Users Push the Enter Button or Use the Tab Button after or Upon entering nay Values in These text boxes, we could also show the a MsgBox asking them to Use the enter or Tab Button, and If the do not, basically Entries are not allowed.

I just do not know how to Code this, Any Idea?

Best Regards
Sanan
 
Sanan
CancelEvent is only of use in the event part of a form or report. For example if you have an on_unload sub then it can check if criteria are met and if not cancel the close event. Your code runs off the on_click event and if criteria are met goes to a new record, if not displays a msgbox, in this case CancelEvent is not necessary.

Are you sure your code is working correctly? use code breaks and step through your code. Have you turned warnings off that alert you if code errors occur? again try stepping through or use msgbox to alert you to which part of the code you are in.

If you want to try your new Idea have a look at the control's on_exit event but make sure you work out your logic first.

HTH
Peter
 
Hi PJStephenson
One question on new Idea, What the Enter Button or Tab Button is called? Oterwise how can I use them in my code a little sample codes helps a lot.

Best regards
Sanan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top