How to get “Lookup” functionality in Access when l

2019-07-16 05:25发布

问题:

I am building a SQL database which will have an Access 2010 front-end.

I would like some of the fields to be lookups in Access (ie the user clicks on the field in Access and a drop down populates). It is fairly straightforward to make a field a lookup for another table in Access but I can't seem to know how to do it in SQL and then propagate the changes.

My SQL knowledge is very basic. Here's an example of how I am creating my SQL tables:

CREATE TABLE RequestTypes (
    RequestType varchar(50) PRIMARY KEY
);
INSERT INTO RequestTypes (RequestType) VALUES ('Val 1');
INSERT INTO RequestTypes (RequestType) VALUES ('Val 2');
INSERT INTO RequestTypes (RequestType) VALUES ('Val 3');

CREATE TABLE Projects (
    ID int IDENTITY(1,1) PRIMARY KEY,
    RequestStatus varchar(50) FOREIGN KEY REFERENCES RequestStatus(RequestStatus),
    Quantity varchar(50)
);

I then connect to the database through the ODBC connection in Access.

How can I create my tables in SQL so that the RequestStatus field of my Projects table to have the same functionality a lookup table does? For example, being able to click on the RequestStatus attribute of a Project and select "Val 1" or "Val 2" or "Val 3" from a list. The above does require the tables to match but does not provide the "dropdown" lookup functionality.

回答1:

Create the table in SQL Server, link to it, then use that table as the row source property for the desired combo box / drop down.

This is the very basic syntax to create a table in SQL Server:

CREATE TABLE LookupTest 
(
    ID INT NOT NULL,
    LookupValue varchar(255)
);


回答2:

When using datasheet view to SQL server, you cannot use a lookup.

However what you can do is create a continues form, and then simply drop in a combo box and use the wizard (or by hand) build the drop down and lookup you require. From the users point of view, they not really be able to discern the difference between editing data in that continues form as opposed to a continues form.

So the continues form will be based on the ONE table. The combo box column will be a long Number, and the combo box will have a row source of the table that provides the list of choices (the lookup values).

So you can build a screen like this:

Note how in above we have a continues form, but also some buttons etc. So a continues form is more flexible then a datasheet, but this “multiple items” forms result in much the same user interface.

Note how the first column is a combo box. In fact as above shows, you can drop in near any kind of control into these continues forms. The combo box can/will display a text column of your choice, but “behind” the scenes the combo box will store the PK id of the value you choose from the combo box into a single column in your forms table.

The result and look and feel is the same as a lookup column in an Access datasheet.



回答3:

OPTION 1:

If you have linked the table to access, you can set the Lookup on the linked table. To do so, go to design view of the table. Select the field you want the Lookup against. Click on the Lookup tab. Choose Combo Box for the Display Control. In the Row Source specify the SQL. For example:

If I am on the CustomerName field I might want the following SQL:

SELECT DISTINCT Customer.CustomerName FROM Customer

OPTION 2:

I do not recommend handling it this way though. You are better off added a Combo Box to a form, setting the Row Source Type as "Table/Query" and the Row Source to the above query. lookup fields should be avoided in Access.