Populating an Access combobox excluding values already in DB - ms-access

The purpose of this database is to assign "variable names" to different "tables". We are taking our data and making limited datasets, so I'm using Access to document the process. I have 3 tables:
tbl_var - containing all variable information
tbl_db - containing the names and characteristics of the limited datasets
tbl_vardb - a linking table where a table ID and variable ID are associated
I have created a form to choose which variable to add to a specific dataset. It's very simple with two dropdown boxes. One box is the names of the datasets. The other box is the names of all the variables. The problem is that if I have already selected "sex" as a variable for the "demo" dataset, I do not want "sex" to pop up as an option for future variable drop down values. After all, it's already in the dataset, no reason to add it again. Since there is more than one dataset, hard-coding a dataset name in a query isn't really working for me.
How do I populate the variable dropdown with values that are not already in a specific dataset?

For the tables [tbl_var] ...
variableID variableName
---------- ------------
1 LastName
2 FirstName
3 Sex
... [tbl_db] ...
datasetID datasetName
--------- -----------
1 Demo
2 SomeOther
... and [tbl_vardb] ...
datasetID variableID
--------- ----------
1 3
... if I have a combo box named [cbxDataset] that gets its items from [tbl_db] then I can have another combo box named [cbxVariable] whose Row Source is
SELECT variableID, variableName
FROM tbl_var
WHERE variableID NOT IN
(
SELECT variableID
FROM tbl_vardb
WHERE datasetID=[cbxDataset]
);
The After Update event of the [cbxDataset] combo box ensures that the other combo box contains the relevant choices
Private Sub cbxDataset_AfterUpdate()
Me.cbxVariable.Requery
End Sub

Related

MS Access Identical Comboboxes for Autofill

I have two tables:
'tableStudent' - a list of students given an ID number by the table, with following columns:
student_ID
last_name
first_name
and 'tableProject' which gives each project an ID and will be used to store information about the students involved in the project. Students will work in pairs on the project. This table includes the following columns:
project_ID
project_title
student1_ID
student2_ID
The columns student1_ID and student2_ID are combo-boxes that link student_ID to the student names.
I want to create a form that can be used to record students involved in a project. I want to be able to select the student IDs and have the student names autofill on a form.
I can make a form that autofills, but only with one student with this SQL:
SELECT tableProject.project_ID, tableProject.project_title, tableProject.student1_ID, tableStudent.last_name, tableStudent.first_name
FROM tableStudent INNER JOIN tableProject ON (tableStudent.student_ID = tableProject.student2_ID) AND (tableStudent.student_ID = tableProject.student1_ID);
When I add Student 2, I get a duplicate error on the student names. How do I indicate that the two names belong to different students? This is the code I'm generating:
SELECT tableProject.project_ID, tableProject.project_title, tableProject.student1_ID, tableStudent.last_name, tableStudent.first_name, tableProject.student2_ID, tableStudent.last_name, tableStudent.first_name
FROM tableStudent INNER JOIN tableProject ON (tableStudent.student_ID = tableProject.student2_ID) AND (tableStudent.student_ID = tableProject.student1_ID);
Any help appreciated!
Options for displaying related info from lookup table:
Use Access query designer to build form RecordSource. Pull tableStudent into query designer. In your case pull in twice - second instance will get named like tableStudent_1. Join each to one of the student fields. Bind textboxes to both sets of lookup table fields and set them as Locked Yes to prevent edit.
Don't include lookup table in form RecordSource at all. Include all student info in combobox RowSource. Expressions in textboxes refer to combobox columns by index, index begins with 0: =[comboboxName].Column(1)
DLookup() domain aggregate function, however, since your tables have a relationship, this is an inefficient method.
For all options, set textboxes with TabStop No.

access 365 combo box selection adding a record to rowSource

I have a two column combo box on a form. The rowsource is set to a two column table. The table is GenderRef there are two fields; GenderSaid and Gender. GenderRef is related by GenderSaid to my People table. GenderRef has two records; GenderSaid = 1 and Gender = Male, GenderSaid = 2 and Gender = Female. When I run the form and select for example: Male from the combobox drop down then proceed to the next record, the GenderRef table has a new record added, GenderSaid = 3 and Gender = Male. I have run the cleanup and repair database utility. I have tried other properties. I have tried to change the relationship and even delete it but with the same result. I would use a value list but I will also be adding a few other reference combo boxes with more than two items in the list and want to get it right with the simplest one. The only VBA code I have runs an update query to the People table updating People.GenderSaid. I have commented out the entire VBA sub and still get a new record in GenderRef.

Textbox Controlling a Query Field

I have to manage various outfalls for stormwater discharge. The issue is that there are various names that have to me used and maintained. Because of this when the reports are generated they have to use the naming convention requested by the client.
First I created a table with the following fields:
outfall_ID
name1
name2
name3
name4
name5
The table may have more names in the future but for now 5 is the maximum I am dealing with
I then created a form which acts as a setup form allowing the user to select name1 through name5 identifying the current name needed for the report.
Here's the part where I am having issues.
In the query I bring in the outfall table and I can choose the ID and the Name fields. but I only want to have the name field active that matches the text field in the setup form (which the information is stored in a table for referencing).
Nothing I have tried or looked up has worked
You can’t really desing a form that works with columns name1 to name5.
Access is a database, not a spreadsheet. So when working with data, you work with rows of data, not columns of data.
So make a table that holds the names you want to select.
Outfall_ID | Name.
Your table will have two columns like above. You can then build a form, and drive a combo box (or listbox) that is based on the above table. That will present a list or combo box that allows you to select the name.
On this form, below the combo box (or listbox), you then have a button to launch your report. You can filter the report by the selected name like this:
DoCmd.OpenReport "rptOutfalls", acViewPreview, , "outfall_ID = " & Me.MyComboBox
The above will filter the report by the given (selected) outfall_ID.
If you need to filter the report by the actual text name you select in the combo box (or list box), we assume that the combo box displays both outfall_id, and the name. So the name is the second columd of the combo box.
If we are to filter the reports by the selected name, then the openreprot command will look like:
DoCmd.OpenReport "rptOutfalls", acViewPreview, , "outfallName = '" & Me.MyComboBox.column(1) & "'"
So the above report will now only filter to the name you selected in the combo box.
Edit
Edit:
SQL can’t translate columns to diffent columns. You need to normailize the data, so such updates can be done with SQL.
Your table will thus have to look like this:
mytrans
ID Myset SetID TranslateTo
1 Name1 1 A
2 Name1 2 B
3 Name1 3 C
4 Name2 1 X
5 Name2 2 Y
6 Name2 3 X
Your combo box will thus be based on this query:
select distinct MySet from myTrans
Your sql to “translate” the ID based on above would then be
Select ID, Field1, Field2, outfall_ID,
(select TranslateTo from mytrans
where mytrans.SetID = outfall_id and myset = 'Name1’) as outfallText
from theDataTable

Getting values from form as query criteria

I have a library database. There are some kind of data (books, magazines and etc.).
I have a form on base of report of these data. In this example I'm trying to add in combobox just one value - Name of the book. I'm doing it by such a query in row source of the combobox:
SELECT [Forms]![books_list]![name] AS id;
books_list - the name of the form and name is the text box called name.
It returns only the first name of the book in all comboboxes - 123.
I need to show only these name values in different comboboxex, which has reference by the row.
To make it clearer - in first combobox - 123, second - Прошлое, third - May be next time.
As I understand, you want to take the value from the text box name, and use a query to select the same value in the combo box?
SELECT <insert your column name here>
FROM <insert your table name here>
GROUP BY <insert your column name here>
HAVING ((<insert column name here>)=[Forms]![books_list]![name] AS id);

Microsoft Access question

I have a combo box which has 3 pieces of information
COMBO BOX 210: Materials ID, Name of Product, Cost, Description
After I update the combo box the cost figure is stored in another field in the table, so far so good, so in selecting "Apples" the cost "$1" stores.
Now, what I want to is have a different field where the description of the apple (the text) is stored yet I only update the combo box once, in other words after updating the combo box Field 1 stores the price and Field 2 the description of the apple.
I will offer you a different example which I hope is similar to what you're asking.
My form includes a combo named cboNames. This is the query for its row source:
SELECT f.id, f.fname, f.date_added
FROM food_test AS f
ORDER BY f.fname, f.date_added;
In the combo's After Update event I can access the values of those 3 columns in the selected combo row by referring to the Column index. Notice the column index numbering starts with zero.
Private Sub cboNames_AfterUpdate()
Debug.Print Me.cboNames.Column(0)
Debug.Print Me.cboNames.Column(1)
Debug.Print Me.cboNames.Column(2)
End Sub
So if I wanted to put the date_added value into another data control, I could add this to the combo's After Update event.
Me.SomeOtherControl = Me.cboNames.Colummn(2)
However I wouldn't actually store both id and date_added in a row of another table. In my food table, each id is associated with a unique combination of fname and date_added. So I would store only the id, and use a SELECT with a JOIN to the food table to look up the associated fname and date_added values whenever I needed them.