Avatar of Akhater
Akhater
Flag for Lebanon asked on

Access SQL / VBA how to display name instead of ID with relationships

Hi
Assuming I have 2 tables
Table1
Table1ID (Autonumber)
Name (Text)

Table 2
ID (Autonumber)
Table1ID (for table relationship)
wathebver (text)


I want to create table 2 programatically

CREATE TABLE Table2
(
   IDAUTOINCREMENT PRIMARY KEY,
   Table1ID INTEGER NOT NULL CONSTRAINT FK_Table1ID  
        REFERENCES Table1 (Table1ID),
    NoteDate DATETIME
)

this is creating the relationship but the display in the table is the ID from table 1, how can i change that to be the "Name" field in Table1 like when i do it from the wizard in Access?

thank you
Microsoft Access

Avatar of undefined
Last Comment
Akhater

8/22/2022 - Mon
Ryan Chong

>>Table1ID INTEGER NOT NULL CONSTRAINT FK_Table1ID  
        REFERENCES Table1 (Table1ID)

you had already referred to the field name, which is Table1ID
Ryan Chong

meaning to say...

FK_Table1ID  
        REFERENCES Table1 (yourfield)

in your case, change "yourfield=" to "name="
Ryan Chong

typo. in your case, change "yourfield" to "name"
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Akhater

ASKER
It was the first thing I tried but it doesn't work, "NO Unique index Found for the referenced field in the primary table"

the ID is the unique but i want the name to dispaly

thanks
Hamed Nasr

The relation is right.
And this is the stage of collecting data.

Next stage is to display and use data.
To display the name, you need to join the two tables, and include the name in the recordset.

SELECT table1.Id, table2.[name]
FROM table1 INNER JOIN table2  ON table2.table1ID=table1.table1ID

Open in new window

ASKER CERTIFIED SOLUTION
Akhater

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Hamed Nasr

This suggests the properties of a combo box, and comes under data display.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ryan Chong

had to change ColumnCount to 2 and ColumnWidth to 0";1"
ok... think it was a bit vague and the issue was not being explained clearly.

anyway, glad that you found the solution.
PatHartman

Combos do not belong on tables.  They belong on forms.  There are lots of expert warnings to not use this "feature".  You will run into trouble with VBA and queries.  Since users never open queries or tables directly, only the developer ever actually would see this combo on the table and if the developer needs to see the lookup value, he can create a query that joins the main table to the lookup table.
Akhater

ASKER
Thank you for the help
solved it myself
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck