简体   繁体   中英

How can I get the ID of a record when it isn't mentioned on a form in Access 2007 via VBA?

I have a multiple items form which does not mention the ID of the record. I have a button for each row which, when clicked, opens up a new form containing more details.

In the past, I've added a field for the ID but made it invisible, but this seems silly - the ID is a unique field of the original query, so I should be able to access purely with code somehow.

Does anybody know how to do this? Let me know if you want more clarification.

Update

The fastest way for me to do this is to add a field in the form for the field in the query, call it the query field name, save it all and close it, then delete the field. Though of course I'd rather just be able to do Me.ID without any of that prior nonsense.

If a form's record source includes a field named "ID", you can access the field's value as a property of the form:

Debug.Print Me.ID

That works without a control bound to the field.

In some cases, you may have to refer to Me!ID, rather than Me.ID because the field has not been added as a property of the form (discussion: http://tek-tips.com/viewthread.cfm?qid=1127364 )

When you add the ID as as control and then delete it, it becomes a property of the form, which is why your work-around works.

您可能需要编辑记录源/查询……并将该列添加为可用字段。

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