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

Endless loop: How to trap the esc key? 1

Status
Not open for further replies.

gusbrunston

Programmer
Feb 27, 2001
1,234
US
Hi:

I need to force a user to enter a string in a msg box, "Why is this check being voided?"

I've used outer and inner loops to poll for the string entry. I think it works, but each time it results in an endless loop!

How do I trap for the escape key? And why won't my code kick user out when an answer is entered in the msg box?
Code:
'User must enter a reason in the msg box
Dim strReason As String
Dim KeyCode As Integer
Dim checkReason
checkReason = True
strReason = ""
Do                             'Outer loop
  Do While strReason = ""     'Inner loop
    strReason = InputBox("Why is this check " _
    "being voided?", _
    "Why void check?")
    'trap for escape key to avoid endless loop
    If KeyCode = vbKeyEscape Then
      'the default reason
      strReason = "Misprinted"
      checkReason = False      'exit outer loop
      Exit Do                  'exit inner loop
    End If
    If strReason = "" Then
      checkReason = True
    End If
  Loop
Loop Until checkReason = False
 
Me.txtReason = strReason 'the reason for this!

1. Is there a better way?
2. Why doesn't the escape key get me out?
3. Why won' user's data entry exit loops?

Appreciate your comments very much.

[glasses][tt]Gus Brunston - Access2000(DAO)
Skill level based on 1-10: 7
Webmaster: www.rentdex.com[/tt]
 
Hi!

Hi think I'll make a go at #1 ;-)
- don't know if it's better, there are several ways of such testing/validation. I don't quite get your logic, so what's below is a variation of what I use. I've made two ways out of the loop (the user can quit the routine without selection. If that's not an option, replace the msgbox/if/vbok with the msgbox line)

[tt]dim sResp as String
dim iAns as Integer
iAns=vbCancel
Do
sResp=InputBox("Blah","Blah", "MyDefault")
if sResp = vbNullstring Then
' user has either pressed cancel or ok with no entry
if msgbox("Not valid, wanna quit?", _
vbokcancel+vbdefaultbutton2)=vbOk then
iAns=vbOk
end if
' or replace the above three lines with:
' msgbox "Invalid entry, try again"

else
iAns=vbOk
end if
Loop Until iAns = vbOk

' Check for value...
if sResp<>vbNullstring then
' User has inputted data, perform actions
else
' User has not inputted data...
end if[/tt]

Some notes:
* don't think you can trap for cancel within this event
* if user presses cancel in the input box - or OK without any data, the result is &quot;&quot; (vbNullstring) - so that's cancel/ESC - which will only &quot;get you out&quot; of the input box, not the routine
* providing a default value reduces the likelihood of no data

Report back if something is unclear.

Roy-Vidar

(regarding the other thread, and placement of publics - long time since I've done FE/BE things, but regarding both performance, referencing -> FE)
 
[tt]
Many thanks, Roy-Vidar:

I'll have time tomorrow to enter your code and see how it works. Will let you know what I find out.

I'll keep my modules in the front end.[/tt]

[glasses][tt]Gus Brunston - Access2000(DAO)
Skill level based on 1-10: 7
Webmaster: www.rentdex.com[/tt]
 
[tt]
Why was I not surprised? Your code worked just right the first time.

Thank you very much.

Cheers,[/tt]

[glasses][tt]Gus Brunston - Access2000(DAO)
Skill level based on 1-10: 7
Webmaster: www.rentdex.com[/tt]
 
Here's some sample code which demonstrates how you can break out of a loop from within a form, without any prompt for user input.

Private Sub Form_Open(Cancel As Integer)
Dim F As Form: Set F = Me
F!StayInLoop = True
End Sub

Private Sub TestLoopBreakout_Click()
Dim F As Form: Set F = Me
Do
DoEvents
If Not F!StayInLoop Then
Exit Do
End If
'--------------------------
'Your normal code goes here
'--------------------------
Loop
MsgBox &quot;Were out of the loop now&quot;
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)
Dim F As Form: Set F = Me
If KeyAscii = 27 Then
F!StayInLoop = False
End If
End Sub

Notes:
(a) Create a hidden text control on your form; call it StayInLoop.

(b) In the Form_Open event, initialise it to False.

(c) Use the KeyPress event to trap key presses; specifically when the escape key is pressed, which will change the StayInLoop control to True.

(d) Set the KeyPreview property of the form to Yes (this is very important; otherwise the escape key trapping wont occur when a control has the focus).

(e) I've added a button, and its associated OnClick event to test the keytrap logic. For your purposes, you'd just need to implant the lines:

DoEvents
If Not F!StayInLoop Then
Exit Do
End If

within one of your loops.

Hope this does it,



Steve Lewy
Solutions Developer
steve@lewycomputing.com.au
(dont cut corners or you'll go round in circles)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top