User Defined Function help please (1 Viewer)

HealthyB1

Registered User.
Local time
Today, 13:07
Joined
Jul 21, 2013
Messages
82
Hi There,
I am trying to clean up repetitive code in an application I wrote many years ago. So far this process has been working well for reducing repetitive code volume and making the application easier to maintain.
But I am having trouble with the attached Subroutines that I would like to consolidate/simplify..
Note that I am working on data entry forms. Most date input fields have a default value set to 'Date()'
Simply put if I have a date field and when I enter a Period (.) it inserts todays date. I can increment date by pressing the plus (+) key or decrement date field by using a minus (-) key. This action is triggered by the "OnKeyPress" event which is looking at the value of the Ascii key pressed. See examples below for 'OrderDate' field and' PromisedByDate' field

EDITED BY THE_DOC_MAN: ADDED CODE TAGS, INDENTED THE CODE TO BE READABLE. NO OTHER CHANGES.

Code:
Private Sub OrderDate_KeyPress(KeyAscii As Integer)
'Toggle date up & Down using + & - Keys
  Select Case KeyAscii
    Case 61 ' Equals key
        KeyAscii = 0
       Screen.ActiveControl = Screen.ActiveControl + 1
    Case 43 ' Plus key
        KeyAscii = 0
        Screen.ActiveControl = Screen.ActiveControl + 1
    Case 45 ' Minus key
        KeyAscii = 0
        Screen.ActiveControl = Screen.ActiveControl - 1
    Case 46 'Period Key
        KeyAscii = 0
        OrderDate = Date
  End Select
End Sub


Private Sub PromisedByDate_KeyPress(KeyAscii As Integer)
'Toggle date up & Down using + & - Keys
  Select Case KeyAscii
    Case 61 ' Equals key
        KeyAscii = 0
        Screen.ActiveControl = Screen.ActiveControl + 1
    Case 43 ' Plus key
        KeyAscii = 0
        Screen.ActiveControl = Screen.ActiveControl + 1
    Case 45 ' Minus key
        KeyAscii = 0
        Screen.ActiveControl = Screen.ActiveControl - 1
    Case 46 'Period Key
        KeyAscii = 0
        OrderDate = Date
  End Select
End Sub
So how do I create a simple function that I can call when a Plus, Minus, Period or Equals key is pressed on nominated date fields at key press time?

Thanks in advance
 
Last edited by a moderator:

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 23:37
Joined
May 21, 2018
Messages
6,430
two ways
Code:
Public Function ChangeDate (Ctrl as Access.Contrl, KeyAscii As Integer)
  'Toggle date up & Down using + & - Keys
        Select Case KeyAscii
          Case 61, 43 ' Equals key
             if not isdate(ctrl) then
               ctrl = date
             else
              ctrl = ctrl + 1
             end if
          Case 45 ' Minus key
             if not isdate(ctrl) then
               ctrl = date
             else
              ctrl = ctrl - 1
             end if
          Case 46 'Period Key
             ctrl = date = Date
          End Select
End Function


Private Sub PromisedByDate_KeyPress(KeyAscii As Integer)
  changedate me.promisedbydate, keyascii
End Sub
 

bob fitz

AWF VIP
Local time
Today, 04:37
Joined
May 23, 2011
Messages
4,455
Paste the following code into a new module:
Code:
Public Sub DateAdjKey(KeyAscii As Integer)
 Const conPlus = 43 'the  + key
 Const conPlus2 = 61 'the =/+ key
 Const conMinus = 45 'the - key
 
On Error GoTo ErrorHandler

 Select Case KeyAscii
   Case conPlus
     Screen.ActiveControl = Screen.ActiveControl + 1
     KeyAscii = 0
   Case conPlus2
     Screen.ActiveControl = Screen.ActiveControl + 1
     KeyAscii = 0
   Case conMinus
     Screen.ActiveControl = Screen.ActiveControl - 1
     KeyAscii = 0
   Case Else
    Exit Sub
 End Select
 
ErrorHandler:
 KeyAscii = 0
 Exit Sub
 
End Sub
In the KeyPress event of the text box use the following line of code:
Code:
    Call DateAdjKey(KeyAscii)
 

HealthyB1

Registered User.
Local time
Today, 13:07
Joined
Jul 21, 2013
Messages
82
Hi Bob,
Many thanks for the quick turnaround and a great solution.

I had to modify code to accept Period key if I wanted to default back to todats Date. (Thanks MajP)


Cheers

Public Sub DateAdjKey(KeyAscii As Integer)

'Subroutine to Adjust Date Field up or down.
Const conPlus = 43 'the + key
Const conPlus2 = 61 'the =/+ key
Const conMinus = 45 'the - key
Const conPeriod = 46 'the . Key

On Error GoTo ErrorHandler

Select Case KeyAscii
Case conPlus
Screen.ActiveControl = Screen.ActiveControl + 1
KeyAscii = 0
Case conPlus2
Screen.ActiveControl = Screen.ActiveControl + 1
KeyAscii = 0
Case conMinus
Screen.ActiveControl = Screen.ActiveControl - 1
KeyAscii = 0
Case conPeriod
Screen.ActiveControl = Date
KeyAscii = 0
Case Else
Exit Sub
End Select

ErrorHandler:
KeyAscii = 0
Exit Sub

End Sub
 
Last edited:

bob fitz

AWF VIP
Local time
Today, 04:37
Joined
May 23, 2011
Messages
4,455
Hi Bob,
Many thanks for the quick turnaround and a great solution.
Cheers
You're welcome. Glad to help :) but in all honesty the code isn't something that I wrote.
It was something I acquired from a forum or book many years ago, although I did adapt it in an app so that users could increase/decrease a Quantity field by one with each press of the plus or minus keys.
 

Users who are viewing this thread

Top Bottom