Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using keyboard to change dates 1

Status
Not open for further replies.

ind

Programmer
Mar 9, 2000
121
US
I have been trying to use the "+" or "-" button the change the date of a textbox.

Here's the code:

Private Sub DateEntry_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 43 ' Plus key
KeyAscii
Screen.ActiveControl = Screen.ActiveControl + 1
Case 45 ' Minus key
KeyAscii = 0
Screen.ActiveControl = Screen.ActiveControl - 1
End Select
End Sub

I keep getting a compile error:

Expected Sub,Function or Property

what's going on here

Please help,,,,,,,anyone

thanks
 
I don't think that you can directly assign a value to "Screen.ActiveControl"... since you are in the date text box's "KeyPress" procedure, reference the date text box "DateEntry" instead. You cannot just put KeyAscii after the first Case statement. If you are not re-assigning KeyAscii to another value, VB already assumes to pass the value pressed, you don't have to do anything. The above code should look like this:

Private Sub DateEntry_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 43 ' Plus key
DateEntry = DateEntry + 1
Case 45 ' Minus key
KeyAscii = 0
DateEntry = DateEntry - 1
End Select
End Sub

However, you can't just add or subtract 1 to change the day value, you will need to use the date functions to manipulate the date. There was a thread recently on here about adding 30 days to the current date, search for that thread to get the correct syntax.


Rob Marriott
rob@career-connections.net
 
Minus button works great
Plus button didn't
It placed a "+" in the textbox

wonder why
any addition help is appreciated
 
you can use Screen.ActiveControl. The problem you might be encountering, is that DataEntry is a property of the form, therefore you'll notice that if you step through the code it will display True instead of the value in the text box. Change the name of the text box.

PaulF

 
you need to add the KeyASCII = 0 to the Add code

PaulF
 
Interesting... can you post the final code when you get it to work? I have never tried this - it may come in handy some time.

Rob Marriott
rob@career-connections.net
 
This works for date fields only, so I'd add some code to check for it

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyAdd ' Plus key
KeyCode = 0
Screen.ActiveControl = DateAdd("d", 1, Screen.ActiveControl)
Case vbKeySubtract ' Minus key
KeyCode = 0
Screen.ActiveControl = DateAdd("d", -1, Screen.ActiveControl)
End Select

End Sub

PaulF
 
BTW you can use
Screen.ActiveControl = Screen.ActiveControl + 1
and
Screen.ActiveControl = Screen.ActiveControl - 1

and it will work for both dates (not date and time) and numerics

PaulF
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top