Solved

DO until keypress is "q"

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

757 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

19 Experts available now in Live!

Get 1:1 Help Now