By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,183 Members | 1,179 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,183 IT Pros & Developers. It's quick & easy.

allowing the <escape> key to break out of a routine

P: n/a
ARC
I know this should be simple, but in a sub-routine that's printing a large
batch of reports, how do you code the routine to look for an escape key to
allow the user to break out?

Thanks!

Andy
Apr 7 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"ARC" <PC*****@PCESoft.invalidwrote in
news:ZX***************@newssvr19.news.prodigy.net:
I know this should be simple, but in a sub-routine that's printing a
large batch of reports, how do you code the routine to look for an
escape key to allow the user to break out?

Thanks!

Andy
Private Declare Function GetKeyboardState Lib "user32" _
(pbKeyState As Byte) As Long
Private Declare Function SetKeyboardState Lib "user32" _
(pbKeyState As Byte) As Long
Private Const VK_ESCAPE = &H1B

Sub test()
Dim c As Long
Dim s As String
Dim aKeys(0 To 255) As Byte
Do
' check to see if escape key is pressed
DoEvents
GetKeyboardState aKeys(0)
If aKeys(VK_ESCAPE) And 1 Then
aKeys(VK_ESCAPE) = aKeys(VK_ESCAPE) And Not 1
SetKeyboardState aKeys(0)
MsgBox "Loop Terminated With Escape Key"
Exit Do
End If
' do work here here
' the next lines are just to give something to see
' and an example
Select Case c Mod 3
Case 0
s = "Back"
Case 1
s = "and"
Case 2
s = "Forth"
End Select
MsgBox s
c = c + 1
Loop
End Sub
Apr 7 '08 #2

P: n/a
ARC
Hi Rich,

I'm printing a number of separate reports in a subroutine. It's actually a
"reprint invoices" function, which could print many many invoices, each one
a separate report. So I'm not meaning to break out while it's actually
printing, but between print jobs. Seems like I remember a visual basic
command from years ago that would allow you to break out of a routine if a
key was pressed.

Andy

P.S. Here's a sample of the code:

Do Until exitout = -1
Set db = CurrentDb()
Set rs = db.OpenRecordset("tReprintInvoices", DB_OPEN_DYNASET)
If rs.BOF Then
MsgBox "No invoices were found where the Invoice Printed box
was unchecked.", vbInformation
rs.Close
Set db = Nothing
Exit Function
End If
rs.FindFirst "PrintedYN = 0"
If rs.NoMatch Then
rs.Close
Set db = Nothing
exitout = -1
Else
Forms!frmOpt.Form!InvoiceNo = rs!InvoiceNo
rs.Edit
rs!PrintedYN = -1
rs.Update
'If Command() = "t" Then
' MsgBox "Printing Invoice #" & rs!InvoiceNo
'End If
rs.Close
Set db = Nothing
If Command() = "t" Then
'don't print
Else
DoCmd.OpenReport Forms!frmOpt.Form!RunName.Caption,
acViewPreview, , , acWindowNormal
DoCmd.PrintOut
DoCmd.Close acReport, Forms!frmOpt.Form!RunName.Caption,
acSaveNo
If Err = 2501 Then 'report cancelled
'Exit Function
End If
End If
End If
Loop

Apr 7 '08 #3

P: n/a
ARC
Thanks, Lyle. Seems like back in the VB 3.0 days, there was an even easier
way via code without declaring functions.

"lyle fairfield" <ly******@yah00.cawrote in message
news:Xn***********************@216.221.81.119...
"ARC" <PC*****@PCESoft.invalidwrote in
news:ZX***************@newssvr19.news.prodigy.net:
>I know this should be simple, but in a sub-routine that's printing a
large batch of reports, how do you code the routine to look for an
escape key to allow the user to break out?

Thanks!

Andy

Private Declare Function GetKeyboardState Lib "user32" _
(pbKeyState As Byte) As Long
Private Declare Function SetKeyboardState Lib "user32" _
(pbKeyState As Byte) As Long
Private Const VK_ESCAPE = &H1B

Sub test()
Dim c As Long
Dim s As String
Dim aKeys(0 To 255) As Byte
Do
' check to see if escape key is pressed
DoEvents
GetKeyboardState aKeys(0)
If aKeys(VK_ESCAPE) And 1 Then
aKeys(VK_ESCAPE) = aKeys(VK_ESCAPE) And Not 1
SetKeyboardState aKeys(0)
MsgBox "Loop Terminated With Escape Key"
Exit Do
End If
' do work here here
' the next lines are just to give something to see
' and an example
Select Case c Mod 3
Case 0
s = "Back"
Case 1
s = "and"
Case 2
s = "Forth"
End Select
MsgBox s
c = c + 1
Loop
End Sub
Apr 7 '08 #4

P: n/a
On Apr 7, 5:00*pm, Rich P <rpng...@aol.comwrote:
Hi Andy,
But to do it
programmatically in Access, the only way to break out of a loop would be
to add a routine within the loop which would read
It's a wise man indeed who knows the only way.
Apr 7 '08 #5

P: n/a
DFS
lyle wrote:
On Apr 7, 5:00 pm, Rich P <rpng...@aol.comwrote:
>Hi Andy,
>But to do it
programmatically in Access, the only way to break out of a loop
would be to add a routine within the loop which would read

It's a wise man indeed who knows the only way.

Does that apply to wise men as well?

"Anytime, my friend, that Lyle and I agree, you should take heed, because we
differ on a great many matters of opinion, so our areas of agreement are
more often than not, matters of fact."
Larry Linson
Microsoft Access MVP"
Apr 7 '08 #6

P: n/a
Disclaimer:

"the only way" implies "The only way that I know how to do it". "the
only way" is the abbreviated form.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Apr 7 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.