DO until keypress is "q"
Posted on 2004-04-07
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()
If test1 = True Then Exit Do
If Application.WindowState <> xlMinimized Then Application.WindowState = xlMinimized
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 1
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.DisplayAlerts = False
Private Sub userform_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Chr(ascii) = "q" Then test1 = True
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.
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)