Form Code (1 Viewer)

Kundan

Registered User.
Local time
Today, 01:29
Joined
Mar 23, 2019
Messages
118
In the attached DB When I want to cancel a record I am prompted as - "Are you sure you want to CANCEL the record?" When I click "No" the Donor No. should revert to the original Donor No. But it remains "1" (i.e. --CANCELLED--).
How to revert it to the original Donor No.?
 

Attachments

  • Van Col.zip
    131.8 KB · Views: 52

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:29
Joined
May 7, 2009
Messages
19,232
Code:
Private Sub Combo139_Change()
On Error GoTo Err_code
Dim ssql
Dim db As DAO.Database
If (Me.Combo139 = 1) Then
If MsgBox("Are you sure you want to CANCEL the record?", vbDefaultButton2 + vbYesNo + vbQuestion) = vbNo Then
[COLOR="Blue"]Me.Combo139 = Me.Combo139.OldValue[/COLOR]
Exit Sub
End If
Set db = CurrentDb
 ssql = "DELETE FROM [Tbl_ALLOCATION-I] WHERE [CR_NO] =" & Me.Employee_ID
       db.Execute ssql
Set db = Nothing
End If
Me.Refresh
Exit Sub
Err_code:
MsgBox Error$
End Sub
 
Last edited:

Minty

AWF VIP
Local time
Today, 09:29
Joined
Jul 26, 2013
Messages
10,368
How are you cancelling a record? There is no code attached to your Delete Order button?

EDIT: Ok- having read the reply from Arne I see you are using the combo. Bit of an odd way to delete a record?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:29
Joined
Oct 29, 2018
Messages
21,454
Hi. You might be better off using the BeforeUpdate event instead of the Change event. Just a thought...
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 04:29
Joined
Feb 19, 2002
Messages
43,223
The Change event is used if you want to examine each character as it is typed so it runs multiple times. Once for each character that is entered.

The BeforeUpdate event of the control runs only ONCE when the control loses focus or the record loses focus and so needs to be saved.

I really have no idea what will happen with the code suggested by arnel. You are modifying a bound control so Access is going to want to save the record you changed but you are using that event to delete a dirty record???? My head is spinning.

Use the wizard to add a button to the form. One of the options is to delete a record. This will generate an embedded macro. Convert it to code and then add whatever validation you want to the code.

PS, For your own sanity and that of anyone who has to take on this application after you leave, please use meaningful names for controls. If you create a control by dragging it from the ribbon, Access will generate a name like combo139 or text48. If you immediately change the Name property of the control to cboDNO or txtSomeField, then when you add event code, the events will have meaningful names and if you reference the controls in code or queries, you will have some idea what you are referring to.

Even worse than controls named combo139 is one named [Employee ID] which is bound to a field named CR_NO. That should be fixed sooner rather than later. Many experts prefix their control names to distinguish them from the bound fields they hold so the bound field name is DNO but the control is cboDNO and the bound field is CR_NO but the control is named txtCR_NO. Notice that the control name includes the actual bound field as part of its name.
 
Last edited:

Kundan

Registered User.
Local time
Today, 01:29
Joined
Mar 23, 2019
Messages
118
Code:
Private Sub Combo139_Change()
On Error GoTo Err_code
Dim ssql
Dim db As DAO.Database
If (Me.Combo139 = 1) Then
If MsgBox("Are you sure you want to CANCEL the record?", vbDefaultButton2 + vbYesNo + vbQuestion) = vbNo Then
[COLOR="Blue"]Me.Combo139 = Me.Combo139.OldValue[/COLOR]
Exit Sub
End If
Set db = CurrentDb
 ssql = "DELETE FROM [Tbl_ALLOCATION-I] WHERE [CR_NO] =" & Me.Employee_ID
       db.Execute ssql
Set db = Nothing
End If
Me.Refresh
Exit Sub
Err_code:
MsgBox Error$
End Sub

Thanks a lot. GOD BLESS YOU!!!!!!!!!
 

Kundan

Registered User.
Local time
Today, 01:29
Joined
Mar 23, 2019
Messages
118
The Change event is used if you want to examine each character as it is typed so it runs multiple times. Once for each character that is entered.

The BeforeUpdate event of the control runs only ONCE when the control loses focus or the record loses focus and so needs to be saved.

I really have no idea what will happen with the code suggested by arnel. You are modifying a bound control so Access is going to want to save the record you changed but you are using that event to delete a dirty record???? My head is spinning.

Use the wizard to add a button to the form. One of the options is to delete a record. This will generate an embedded macro. Convert it to code and then add whatever validation you want to the code.

PS, For your own sanity and that of anyone who has to take on this application after you leave, please use meaningful names for controls. If you create a control by dragging it from the ribbon, Access will generate a name like combo139 or text48. If you immediately change the Name property of the control to cboDNO or txtSomeField, then when you add event code, the events will have meaningful names and if you reference the controls in code or queries, you will have some idea what you are referring to.

Even worse than controls named combo139 is one named [Employee ID] which is bound to a field named CR_NO. That should be fixed sooner rather than later. Many experts prefix their control names to distinguish them from the bound fields they hold so the bound field name is DNO but the control is cboDNO and the bound field is CR_NO but the control is named txtCR_NO. Notice that the control name includes the actual bound field as part of its name.

Thanks! I will adhere to your suggestions. GOD BLESS YOU!!!!!!!!!
 

Kundan

Registered User.
Local time
Today, 01:29
Joined
Mar 23, 2019
Messages
118
Hi. You might be better off using the BeforeUpdate event instead of the Change event. Just a thought...

In Before Update I receive a message - Your code prevents Access from updating the record.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:29
Joined
Oct 29, 2018
Messages
21,454
In Before Update I receive a message - Your code prevents Access from updating the record.

That can probably be fixed. I won't know until I try. But if you already got it working, there's no need to mess with it anymore. Cheers!
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 04:29
Joined
Feb 19, 2002
Messages
43,223
If you want us to debug code, you need to post it. Even better, post the database with instructions on how to get the error.
 

Users who are viewing this thread

Top Bottom