Solved

DO until keypress is "q"

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using "ScreenUpdating" 6 73
Sending a email via excel using vba 6 100
How to set the sa password in a vb6 code for sql connection 9 60
Problem to With line 4 63
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

830 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