KeyPress concept

Ferdi_nl

New Member
Joined
Sep 20, 2011
Messages
8
Hello

I am trying to grasp the concept of KeyPress. It seems simple. I understand all code examples I've seen. Yet I have difficulty getting even the most basic version working. I keep getting the 'procedure declaration does not match description of event or procedure having the same name'. Can someonte tell me what I'm doing wrong? Any tips greatly appreciated

I tried the example from http://msdn.microsoft.com/en-us/library/aa211401%28v=office.11%29.aspx

I created a form with just one textbox called ShipRegion. The only code behind the form is the code from the MSDN site:

Private Sub ShipRegion_KeyPress(KeyAscii As Integer)
Dim strCharacter As String

' Convert ANSI value to character string.
strCharacter = Chr(KeyAscii)
' Convert character to upper case, then to ANSI value.
KeyAscii = Asc(UCase(strCharacter))
End Sub

Running this results in the error message as mentioned. I can´t see why (fairly new to VBA, obviously ) So, my question is not about what this sub is supposed to be doing, but how I can get it to work.

Thank you
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I didn't get that error. Where did you put the code exactly?

However all that can be accomplished in one line in the Change event:

Code:
Private Sub ShipRegion_Change()
ShipRegion = UCase(ShipRegion)
End Sub
 
Upvote 0
I didn't get that error. Where did you put the code exactly?

However all that can be accomplished in one line in the Change event:

Code:
Private Sub ShipRegion_Change()
ShipRegion = UCase(ShipRegion)
End Sub

My plans for using KeyPress are completely different from the example I posted. But before I dig deeper, I want to be able to run this simple code, so I´m sure I´ve grasped the concept.

I created a new form, added just one textbox, named it ShipRegion and then double clicked the form. In the code window I pasted the example code. I pressed F5. That´s when I get the error message. Thanks for your help.
 
Upvote 0
I don't have Acces right now, but the Excel declaration is different

Just use the meat of the procedure; not the 'Sub' line

Code:
Dim strCharacter As String
' Convert ANSI value to character string.
strCharacter = Chr(KeyAscii)
' Convert character to upper case, then to ANSI value.
KeyAscii = Asc(UCase(strCharacter))
 
Me.Caption = UCase(strCharacter) & " " & KeyAscii
 
Upvote 0
pressing F5 as in pressing the green triangle to run the code.

Is pasted the code in the code window and wanted to test it... I´ve made simple forms before and they work fine. This code however refures to run, same story with other KeyPress examples I´ve download. There must be something I´m not getting. I just don+t see why this example gives that error message.
 
Upvote 0
@Tweedle

It´s not about what the sub actually does, but about getting KeyPress to work generally, so I can then start to build from there. Your suggestion seems to skip the KeyPress part, or do I misunderstand you? Thanks
 
Upvote 0
Well; the example is an M$ Access example and declares the event as
Code:
Private Sub ShipRegion_KeyPress([I]KeyAscii As Integer[/I])

My Excel 2007 declares the event as
Code:
Private Sub ShipRegion_KeyPress([I]ByVal KeyAscii As MSForms.ReturnInteger[/I])

They are different.

My working Excel sub looks like
Code:
Private Sub ShipRegion_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Dim strCharacter As String
' Convert ANSI value to character string.
strCharacter = Chr(KeyAscii)
' Convert character to upper case, then to ANSI value.
KeyAscii = Asc(UCase(strCharacter))
Me.Caption = UCase(strCharacter) & " " & KeyAscii
End Sub

EDIT: Same declaration statement, but 'not what is expected' according to Excel; hence your error.
 
Upvote 0
Running a macro in Excel is Alt-F8. But your code is Event-driven code, it will run when you type in the textbox.
 
Upvote 0
@Tweedle: That did the trick. I wasn't aware that Excel and Access require a different approach and that example code can't be used 'as is', without knowing what Office app. it is designed for (how can you tell if it isn't specified?) Anyway, happy to have taken this hurdle

@ Hot Pepper: it wasn't a macro but a form. I have been able to use F5 for forms to run. This example gave the error, however. But now I know why.

Thank you both very much for your time & patience.

Regards, Ferdi
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,741
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top