Find control with primary key

  • Thread starter Thread starter Kevin Ayton
  • Start date Start date
K

Kevin Ayton

I'm trying to write some code that will handle several forms. Each form is
populated by a different query.

Is there any way from within the code to find the control on the form that
is linked to the primary key of the underlying query?

TIA

Kevin
 
Simplest way would be to use the tag property of the control (set it to "PK")
and then step throught the form's controls looking for that.

I assume that you DO NOT have more than one table in the query, since every
table should have a primary key. Of course, you could be asking for the primary
key of the "parent" record.
 
Kevin

Out of curiosity only, are you using "several forms" to display variations
on the same underlying data, and using "different queries" to provide those
different selection criteria? Or do you have multiple, differing underlying
recordsets (i.e., not all from the same table), and using one form for each?

Just curious... (with a reason <g>)

Jeff Boyce
<Access MVP>
 
Jeff Boyce said:
Kevin

Out of curiosity only, are you using "several forms" to display variations
on the same underlying data, and using "different queries" to provide those
different selection criteria? Or do you have multiple, differing underlying
recordsets (i.e., not all from the same table), and using one form for each?

Just curious... (with a reason <g>)

Jeff Boyce
<Access MVP>

I have several tables, with one query for each table and one form for each
query.
The VBA code does the same thing for each form (and hence query, table). I
want to find the control on the table that has the primary key of the table
as its ControlSource.

Thanks

Kevin
 
Kevin Ayton said:
I have several tables, with one query for each table and one form for each
query.
The VBA code does the same thing for each form (and hence query, table). I
want to find the control on the table that has the primary key of the table
as its ControlSource.

Thanks

Kevin

You realize that a table can have a multi-field primary key? Ie. several
fields/controls - not just one?

This code tells you the name of the field to which each control is bound. At
least that's a start.

(untested)

dim ctl as control, s as string
for each ctl in me.controls
on error resume next
s = ctl.controlsource
if err.number = 0 and instr (s, "=") <> 1 then
on error goto 0
debug.porint ctl.name, s
else
on error goto 0
endif
next

HTH,
TC
 
Back
Top