Link to home
Start Free TrialLog in
Avatar of paasky
paaskyFlag for Finland

asked on

Calendar control and KeyDown/KeyPress event

Does anyone know what are the KeyDown or KeyPress parameters for ActiveX calendar control? I've need to requery subform when user moves day selector with keyboard arrow keys.

Now user can select the day with mouse click, but it would be nice he/she could browse "diary" backward and forward using arrow keys.

I've already tried:

Private Sub XCalendar_KeyDown(KeyCode As Integer, Shift As Integer)

and ...

Private Sub XCalendar_KeyPress(KeyAscii As Integer)

but Access gives me error that given parameters are not right.

I would appreciate your help!

Paasky
Avatar of BrianWren
BrianWren

The key press and down parameters are identical to the same events for Access intrinsic controls.

KeyCode As Integer, Shift As Integer for KeyDown,
KeyAscii As Integer for KeyPress

If you set KeyCode = 0 in the KeyDow event, the keystroke essentiall never happened.

If the shift key is down, Shift = 1, if the control key, 2.  If the alt key, 4.

They are summed.  All three = 7.

CHR$(KeyCode) is the character pressed, and you can test for cursor keys' values, too.

   Left  = 37
   Right = 39
   Up    = 38
   Down  = 40

Brian
Avatar of paasky

ASKER

Brian,

I guess ActiveX control's make exception for that rule every parameter are same for all controls... :(

When looking at Calendar objects Event properties there are only five available (visible) events: OnUpdate (which never occurs!), GotFocus, LostFocus, OnExit and OnEnter. However it has also OnClick event, but you have to write that yourself:

Private Sub xCalendar_Click()
    Me.subform1.Requery
End Sub


I also tried place the code on Form's KeyDown event, but seems that when ActiveX control has focus the form is not responding to keyevents.

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If Me.ActiveControl.Name = "XCalendar" Then
        If KeyCode = vbKeyDown Or KeyCode = vbKeyUp Or KeyCode = vbKeyLeft Or KeyCode = vbKeyRight Then
            Me.Subform1.Requery        End If
    End If
End Sub

Paasky

Avatar of paasky

ASKER

Adjusted points to 125
Go to the module design window of the form.  At the top, there is a dropdown where you can select the objects on the form.  Select the control.  To the right is a list of events.  KeyDown, and KeyPress are there.  Select one of them form the list, and Access will build the framework of the event, with the focus in the sub.  Put your code there, and the event will trigger as billed, 'Down,' or 'Press.'

Brian
Avatar of paasky

ASKER

I've never tried looking events there... This is good thing to know.

I made event triggers like you suggested but somehow they seem not fire when I hit cursor keys (other key press is registered):

Private Sub XCalendar_KeyDown(KeyCode As Integer, ByVal Shift As Integer)
    ' Occurs when user presses for eg. 'A'...
    Debug.Print "KeyDown"
End Sub

Private Sub XCalendar_KeyPress(KeyAscii As Integer)
    Debug.Print "KeyPress"
End Sub

Maybe there's needed some other way to catch value change?

Btw, my XCalendar is not bound to table field - it's just for filtering.

Paasky
Turn the form's keypreview on, (or off, depending on it's current state).

Be sure to remove the keypress/down for the form which you said you had added.

Brian
Avatar of paasky

ASKER

Okay, I tried removing Form's KeyEvents and changed keypreview from Yes to No.. but it's still not working.

Any other suggestions?

Paasky
ASKER CERTIFIED SOLUTION
Avatar of BrianWren
BrianWren

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of paasky

ASKER

Good idea, I will try After Update on Monday...

Calendar control seems to work strange in many other ways too: If I try to set its "default" value to current day in form's OnOpen event, it doesn't work. So I've to user timer to refresh the value after 50 milliseconds... And the control corrupts someway - there seems to be two same day next each other... then I use refresh to fix the  situation and after that it changes FirstDay from Monday to Sunday... very strange indeed.

Anyway it's "free" ActiveX so better not complain too much. I wonder what happened to OutLine control which was included with MS Access 2.0 DK..?

Regards,
Paasky

Avatar of paasky

ASKER

Couldn't wait til Monday... I just get to my office and tested AfterUpdate with Calendar... and it works!

Thank you very much Brian. Glad there was a work around for this problem.

Best regards,
Paasky