简体   繁体   中英

Ms access vba sink (catch) keydown event from a formless subform

I am developing a program in access for maintaining test data for other applications. It is a generic program, not for a specific application. The test data is assumed to be stored in database tables. Because my program does not know in advance which tables are part of the application to be stated, it reads the meta data from the application database and creates linked tables for each table. For entering and maintaining the data, I have a form with a subform control. This subform control does not have a predefined source object. By setting the source object property to "table.[linkedtablename]" the subform can be used for data entry. When the user selects another application table from a list, that other table is assigned to the subform. This works pretty good. Now I need to handle events from this subform. Because the subform source object is not a form, this is not obvious. It is no problem to catch events of the subform when they have no arguments. You can write the following code:

Me.subformname.form.oncurrent = "=customoncurrent()"

But how to catch a keydown event? This event is raised with 2 arguments (key code, shift).

I cannot write:

Me.subformname.form.onkeydown = "=customkeydown()"

In runtime, VBA will raise an error that arguments are missing in the event procedure declaration.

I have tried to use the 'withevents' clause but without success. Perhaps I know too little about the possibilities of this clause.

Can this problem be solved and how?

Mark

You have a nice, minimalistic solution going here. But, since you added the requirement for capturing subform events, it became too minimalist!

You could go several ways, but I would suggest this:

  • Build a generic subform, set to display in DataSheet mode
  • Add your event handling explicitly to the subform's class module
  • Place a bunch of textboxes in it with generic names, like Field01, Field02, etc. The number should exceed the maximum number of columns in your largest table
  • When you link to a new table, your code would put the table field names in the textbox ControlSource properties, set the captions in the associated labels, and set other properties as required
  • Lastly, write code to hide the unused text boxes

This is likely to be less work than other approaches..

BTW don't use WithEvents in form class modules. That is just begging for trouble.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM