Solved

DO until keypress is "q"

Posted on 2004-04-07
1
419 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Concatenate 2 userform comboboxes to a userform text box 4 61
using Access 8 68
Adding to a VBA? 6 69
SLMGR Switches Are Not Working On KMS Host 3 88
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

815 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

12 Experts available now in Live!

Get 1:1 Help Now