Call a function direct from KeyPress event

IfButOnly

Registered User.
Local time
Today, 14:49
Joined
Sep 3, 2002
Messages
236
I have a substantial number of integer fields on a form - say 60 fields.

I have the fields locked and use the keypress event to only allow updating of the fields by using the "+" and "-" keys - all works okay.

So that I don't have 60 subs to maintain that all just call the one function, I would like to be able to call the function directly from the KP event.

Does anyone know if this is possible and, if so, how do I pass the KeyAscii value to the function?

Any thoughts would be most appreciated.

Peter
 
Put the public function in a module and then "call" it via the KeyPress event on every form as necessary.
 
Ilkhoutx,
If I understand you correctly, I still don't know how to get the KeyPress value to the function.

one of the functions is :-
---------------------
Public Function upd_score(KeyAscii As Integer)
Dim ctl As Control
Set ctl = Screen.ActiveControl
If KeyAscii = 45 Then
Me(ctl.name).Value = Me(ctl.name).Value - 1
End If
If KeyAscii = 61 Then
Me(ctl.name).Value = Me(ctl.name).Value + 1
End If
End Function
------------------------

What do I put in the KeyPress event that passes the KP value across?

upd_score(KeyAscii) doesn't work and I have tried all sorts of combinations with the event and parameters but no luck.

If you were inferring that I should be using the Module property, then I may need to you work me through it.

Further assistance would be greatly appreciated.... Peter
 
Peter,

Another option, which may or may not suit your project, is to use the form's keypress event. Set the default value of your textboxes to some number (so they are never null) and create a naming convention. That is, for those textboxes on the form you want changed using the form's keypress event, give them names that share a prefix, like "Chg" -- ChgTxtBox1, ChgTxtBox2, ChgTxtBox3, and on...

Code:
Private Sub Form_KeyPress(KeyAscii As Integer)

 Dim MyCtrl As Control
 Set MyCtrl = Screen.ActiveControl

   If TypeOf MyCtrl Is TextBox Then
      If Left(MyCtrl.Name, 3) = "Chg" Then      
         Select Case KeyAscii
            Case 43  ' +
               MyCtrl.Value = MyCtrl.Value + 1
            Case 45  ' -
               MyCtrl.Value = MyCtrl.Value - 1
         End Select         
         'Eat the key.
            KeyAscii = 0
      End If
   End If

End Sub

One relatively simple slab of code does it all.

Regards,
Tim
 
Thanks for the responses.

I tried the [=upd_score(KeyAscii as Integer) this morning but with or without the 'as Integer' part it gave me an error. Moved on then to setting the KeyPreview event to true and utilising the KeyPress event - worked great - and basically exactly the same Tim suggested. So thanks a lot for that, guys.

Once I got it going I got a bit more ambitious and decided as well as the '-' and '+' keys I would increase or decrease the field values by the Left and Right mouse buttons.

Once again this works fine for a single field but am struggling to find a form wide solution like the KeyPress event. From reading the Help, I am not sure if it is possible - Does any know for sure?

Tim, I noticed in your solution you didn't use the KeyPreview event but set KeyAscii to 0 - I will have a look at that when I get offline.

Anyway, many thanks Ilkhoutx and Tim for the help to date and any help on the mouse click problem would be greatly appreciated.

Peter.
 
The great thing about senility is that I am meeting new people every day. On the down side, while I had done various searches for the KeyPress thing, I only just realised I hadn't done any on the Mouse problem.

It doesn't get any better because the only similar question was from me a while back when I tried to do something similar - Duh!!

Anyway no solution came back at that point and I took another approach. Though Pat Hartman responded with 'I haven't done it so I don't have any examples but I think you would create a class module and specify "with events"'

I have tried to understand class modules before but they were a bit beyond me - any help on this approach or any other would, once again, be appreciated.

Peter
 
I don't read every post on this board but I actually remember you asking about this way back when... You can easily adopt the code all ready written to make this happen.

Code:
Private Sub chgText0_MouseDown(Button As Integer, Shift As Integer, _
                                        X As Single, Y As Single)
'Call the sub.
   ChangeValue Screen.ActiveControl, Button
End Sub

Sub ChangeValue(cCtrl As Control, iBtn As Integer)
   Select Case iBtn
      Case 1
         cCtrl.Value = cCtrl.Value + 1
      Case 2
         cCtrl.Value = cCtrl.Value - 1
   End Select
End Sub

One note: Unless this app is for personal use only, I'd be inclined to use "Up-Down" controls (you can fashion your own using two command buttons) to move the values up or down in each textbox. The left and right mouse clicks might confuse the average user...

Regards,
Tim
 
Tim,

The app is for personal use only - it records details from my golf game. As I use it quite a lot, I like to enhance it every now and again and use it as a learning tool. So what I have got so far is fine but want to see what can be done.

The form gathers score, putts, fairways hit, greens in regulation, green saves and sand saves for each of the 18 holes on a course - so while it looks okay, I try to keep down the number of extraneous controls and movements. With the control buttons for up & down, it is too messy to put on each control and too much movement if I just have two controls off to the side.

I moved away from the mouse clicking last time and went to have a little pop-up form for changing the values for each hole - which worked okay but was a bit messy and not very intiutive. With the form as I currently have it you can use the - + keys to change the value or type the value in directly etc. The left and right mouse click would be great as it cuts down on a lot of keyboard interaction.

---

I have the same problem with the mouse solution as I had originally with the - and + keys, that is, it needs at least 108 subs to maintain - one for each field. The KeyPress and Form_KeyPress solved the first problem very nicely and I am looking for a similar solution for the mouse.

Many thanks for your input so far and any further assistance from you or the Access community would be greeeeaatly appreciated.

Peter
 

Users who are viewing this thread

Back
Top Bottom