Link to home
Create AccountLog in
Avatar of Akhater
AkhaterFlag 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
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

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

you had already referred to the field name, which is Table1ID
meaning to say...

FK_Table1ID  
        REFERENCES Table1 (yourfield)

in your case, change "yourfield=" to "name="
typo. in your case, change "yourfield" to "name"
Avatar of 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
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
Avatar of Akhater
Akhater
Flag of Lebanon image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
This suggests the properties of a combo box, and comes under data display.
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.
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.
Avatar of Akhater

ASKER

Thank you for the help
solved it myself