Help With Function

AlanW

Registered User.
Local time
Today, 14:15
Joined
Aug 11, 2001
Messages
25
I found this Function on the Internet which uses Plus/Minus keys to act as spin controls in a numeric or text field. I have created a module but don’t know what to do next in order to make it work.

Can anyone help?

'**************** Code Start **************
'Code Courtesy of
'Jason Looney
'
Public Function PlusMinus(intKey As Integer, strFormName As String, Optional _
strSubformName As String = "", Optional strSubSubFormName As String = "") _
As Integer

'Allows a date or number field on a form or subform to respond to plus/minus keys
'Sample Usages (in the Keypress event):
' Call PlusMinus(KeyAscii, Me.Name)
' Call PlusMinus(KeyAscii, Me.Parent.Name, Me.Name)
' Call PlusMinus(KeyAscii, Me.Parent.Parent.Name, Me.Parent.Name, Me.Name)

On Error GoTo TheHandler
Dim ctl As Control

If strSubformName <> "" Then
If strSubSubFormName <> "" Then
Set ctl = Forms(strFormName).Controls(strSubformName).Form.Controls(strSubSubFormName).Form.ActiveControl
Else
Set ctl = Forms(strFormName).Controls(strSubformName).Form.ActiveControl
End If
Else
Set ctl = Forms(strFormName).ActiveControl
End If

ctl = CDate(ctl)

Select Case intKey
Case Is = 43 'the '+' key
ctl = ctl + 1
intKey = 0
Case Is = 45 'the '-' keys
ctl = ctl - 1
intKey = 0
Case Is = 61 'the '='/'+' key next to Backspace
ctl = ctl + 1
intKey = 0
End Select

ExitHandler:
PlusMinus = intKey
Set ctl = Nothing
Exit Function

TheHandler:
Select Case Err.Number
Case Is = 94 'Invalid use of null
Case Is = 13 'Type mismatch
Case Else
MsgBox Err.Number & ": " & Err.Description
intKey = 0
End Select
Resume ExitHandler
End Function
'**************** Code End **************
 
What you need to do next is place a call to the procedure on your form. It must be a number field or a date field. This will not work on any other type of fields. You need to place the following code in the 'On Key Press' event, located on the bottom of the 'Event' tab of the properties of the number or date control.

'Begin Call***************
Private Sub YourControl_KeyPress(KeyAscii As Integer)
Call PlusMinus(KeyAscii, Me.Name)
End Sub
'End Call*****************
Where YourControl is the name of the number or date control you are wishing to use +/- keys on.

Hope this helps!
 
Jason

Thanks for your help and your email. I got it to work fine. The only problem is that if I enter the date manually after having used the +/- keys, the text box won’t allow me to do so. (It goes a bit weird and only allows me to enter a single digit.) Is this a problem that can be remedied?

Thanx
Alan
 

Users who are viewing this thread

Back
Top Bottom