How to detect if a user hit <Esc> to exit loop

S

SAG

The DoEvents function looks like what I need. I looked
for an example of what I need in Access help and the MS
Knowledge base but could not find anything other than
# of forms open. Please give me a sample of the vba code
I can use to do the following:

do until rs.eof
Code process.........
if escape key pressed then
msgbox "You cancelled this process!"
exit sub
end If
rs.MoveNext
loop

Thanks,

Steve
 
T

Tim Ferguson

Please give me a sample of the vba code
I can use to do the following:

do until rs.eof
Code process.........
if escape key pressed then
msgbox "You cancelled this process!"
exit sub
end If
rs.MoveNext
loop

You actually need three things:

' Form declaration section:
Private m_fEscPressed As Boolean

' in the form event code:

Private Sub Form_Down( keycode as et....)
' sorry can't remember the arguments from the top of my head

' basically, the plan is to set the flag if it detects an Esc
' key
If KeyCode = vbEscape Then
m_fEscPressed = True
End If

End Sub


' and then your processing loop

Do While Something
' you need this so that the form can see the KeyPress if
' there is one.
DoEvents

' now look for the flag
If m_fEscPressed Then
Exit Do

End If


' carry on here...


Similarly, you can use the cmdQuit_Click() event to set the same flag, etc.
There is no equivalent of the GetKey routine in VBA because it's a control-
event oriented system.

HTH


Tim F
 
D

Dirk Goldgar

Tim Ferguson said:
Private Sub Form_Down( keycode as et....)

Make that "KeyDown", not just "Down":

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
 

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

Top