VBA CODE for ESC pressing!

FlavioT

New Member
Joined
Feb 11, 2003
Messages
40
Hi! I'm having a problem here...

I have assigned a code for each "box" I create in my form.

The problem is that if I don't want to add (like I made a mistake) the user would have to press ESC and then close the form so the Autonumber doesn't have empty values on it.

Which code would emulate the ESC key??
Thanks!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
You didn't really say what your code does. I am guessing it is adding new records?

Could you post the code from one of these boxes so that we can see how it works?
 
Upvote 0
I've changed a little a Function I found here at Mr.Excel..
What is does: Assignes codes for each box I have...


But when I add a new record and don`t want a code assigned for it i'd like to close the form without saving it, you undersant?

the code assignes for a field called COD the value of

{idempresa} - 000.0001 for the first box of this "idempresa"

for the second... {idempresa} - 000.0002
and so on...

What I'd like to have is a button that would CANCEL this reg add...

Thanks!!!

Function GetNextPKValue(IDEmpresa) As String

Dim adoRS As ADODB.Recordset, strSQL As String
Dim strPotentialValue As String, lngSuffix As Long

strPotentialValue = Format(Val(IDEmpresa), "00")


'Search to see if any record exists for the current day
'CHANGE THIS TO MATCH YOUR TABLE/FIELD NAME
strSQL = "SELECT TOP 1 Caixa.cod FROM Caixa WHERE Caixa.cod Like '" & _
strPotentialValue & "%' ORDER BY Caixa.cod DESC;"

Set adoRS = CreateObject("ADODB.RecordSet")

adoRS.Open strSQL, CurrentProject.Connection, adOpenForwardOnly, adLockReadOnly


If adoRS.BOF And adoRS.EOF Then 'There are no values for this date
GetNextPKValue = strPotentialValue & "-000.0001" 'so it must be record number 1 for the current date
adoRS.Close
Exit Function
End If

ww = Val(Right$(adoRS.Fields(0), 8))
mm = Format(Val(Left$(ww, 3)), "000") & Format(Val(Right$(ww, 4)), "0000") + 1
xx = "-" & Left(mm, 3) & "." & Right(mm, 4)
GetNextPKValue = Left$(strPotentialValue, 2) & xx
adoRS.Close

End Function
 
Upvote 0
Wow, that's some code (I haven't done much with ADO recordsets).

If you are unable to Cancel the record addition, maybe you can create some code that will call a Delete Query to delete the record that was just added.

Anyone else have an idea/suggestion?
 
Upvote 0
This is from Help in A97
You can use the Undo method to reset a control or form when its value has been changed. For example, you can use the Undo method to clear a change to a record that contains an invalid entry.

Syntax

object.Undo

The Undo method has the following argument.

Argument Description
object A Form object or a Control object.
Remarks

If the Undo method is applied to a form, all changes to the current record are lost. If the Undo method is applied to a control, only the control itself is affected.
This method must be applied before the form or control is updated. You may want to include this method in a form's BeforeUpdate event or in a control's Change event.
The Undo method offers an alternative to using the SendKeys statement to send the value of the ESC key in an event procedure.

HTH

Peter
 
Upvote 0
Peter,

Thanks for helping out on this one! The UNDO command is good to know. I may be able to implement that in some of the code I use.
 
Upvote 0

Forum statistics

Threads
1,221,798
Messages
6,162,027
Members
451,737
Latest member
MRASHLEY

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top