Solved

DO until keypress is "q"

Posted on 2004-04-07
1
416 Views
Last Modified: 2010-05-18
i remember at school it was as easy as my title all i had to do was use keypress = get$....

Hi, i am trying to teach myself visual basic and so far have got off to a pretty good start. Just now i am just trying to make a fun little annoying program that when opened will start beeping. When the correct key or key combination is pressed the beeping will stop. The problem that i have is that unless i have the code in the form initialize part of the program the "stop" key won't work. Here is what i have just now...

Dim test1 As Boolean

Private Sub UserForm_Initialize()
Do
DoEvents

 If test1 = True Then Exit Do
        If Application.WindowState <> xlMinimized Then Application.WindowState = xlMinimized
            Beep
            newHour = Hour(Now())
            newMinute = Minute(Now())
            newSecond = Second(Now()) + 1
            waitTime = TimeSerial(newHour, newMinute, newSecond)
            Application.Wait waitTime
Loop

Application.DisplayAlerts = False
Application.Quit

End Sub

Private Sub userform_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Chr(ascii) = "q" Then test1 = True
End Sub

This works fine but because its under initialize you can't see the form. I was planning to make a pretend button to turn it off that would run around the form as the mouse went over etc. Thats not the issue tho, could someone tell me where i am going wrong? i tried the same code above for userform click and even commandbutton click but none of them recognise that i have pressed a key (checked using STOP command) test1 was always false. I am using VB in excel as my work doesn't let us have access to VB6.

Thanks

Scott

ps the other reason i want to put the main code somewhere else other than initialize is so that i can hide the application while still able to see the form (if i can't see it i can't use the keypress to quit)
0
Comment
Question by:scotster
1 Comment
 
LVL 17

Accepted Solution

by:
zzzzzooc earned 50 total points
ID: 10780840
>> This works fine but because its under initialize you can't see the form.

You can set FormName.Visible=True or call FormName.Show to avoid that.

>>  but none of them recognise that i have pressed a key

Set .KeyPreview = True

........

Form1:
=========

Private bPress As Boolean
Private Sub Form_KeyPress(KeyAscii As Integer)
    If Chr(KeyAscii) = "q" Then
        bPress = True
    End If
End Sub
Private Sub Form_Load()
    Me.KeyPreview = True
    Me.Visible = True
    Do Until bPress = True
        DoEvents
    Loop
    MsgBox "Pressed 'q'!"
End Sub
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now