Solved

DO until keypress is "q"

Posted on 2004-04-07
1
423 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

691 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