Textbox Controlling a Query Field - ms-access

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

Related

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.

MS Access Database - Chart Drawing From Lookup Control

I am trying to create a pie chart in a MS Access report. The problem is that the field in the table from which I am trying to draw the data is a lookup field, drawing its data from another table. So when the pie chart is rendered, the labels are coming up as the numerical id for the entries rather than the text labels.
The structure is something like this:
Discrepancies Table:
column1 - ID
column2 - Name
column3 - Category - lookup from categories table
Categories Table:
ID
category (name of the category)
In the discrepancies form I have the category field bound to the "category" column (column1), so the text label appears in the table.
However, in the pie chart, which draws from the category field in the Discrepancies table, the label is the numeric ID of the category rather than the name.
How can I get the name to appear as the label rather than the ID?
I figured it out by creating and basing my pie chart on a query, rather than by pointing it a row source.
The query was SELECT DISTINCTROW categories.category, Count(*) AS [Count Of discrepancies]
FROM categories INNER JOIN discrepancies ON categories.[ID] = discrepancies.[category]
GROUP BY categories.category;

Populating an Access combobox excluding values already in DB

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

Conditional DropDown List with SSRS

I created a SSRS report with a dropdown to parameterize data being reported. This works fine. I now need to add another parameter to filter the report a little further. For example, I have a location dropdown that shows area, country, region, etc. I need to add another dropdown that is dependant on the first dropdown. So if I select "country" in the 1st dropdown, I show the list of countries in the 2nd dropdown or if I select Region - I show list of Regions in the 2nd dropdown. Country, Area, Region data is stored in different tables. So basically my query needs to be smart enough to run the appropriate sql based on 1st dropdown selection.
Thanks so much for any assistance given.
One of the powerful features of Reporting Services is that everything is an expression, including the dataset's SQL statement.
Let's say your first parameter (the one that describes what to select) is called Location and it selects a list of locations such as country, region, etc. Perhaps you get that from a table which has a LocationId and a Description like so:
SELECT LocationId, Description FROM Locations
You hook up the Location parameter to this query to get your drop down list of location selectors for the Location parameter.
Now create a second parameter called Select where we want to select from a list of countries or regions. To keep it simple, I'm going to assume there are only two locations: Country with a LocationId of 1 and Region with a LocationId of 2.
Create a new dataset called Selections and manually add fields to it called Id and Description. Hook up your Select parameter to this dataset. Now for the SQL Statement for the Selections dataset, enter the following expression:
=IIF(Parameters!Location.Value = 1,
"SELECT CountryId AS Id, CountryName AS Description FROM Countries",
"SELECT RegionId AS Id, RegionName AS Description FROM Regions")
So, where the Location parameter is set to 1 (Country) you select from the Countries table otherwise you select from the Regions table. You alias the field names so you get consistently named fields for your dataset for use in the Select parameter query. Obviously, you can extend this to more selections as required.
You get the idea but this is a little fragile - whenever you want to add a new location type you have to go through all your reports and update the SQL statement for the Selections dataset. That's tedious and no one wants that job. What we want is an automated system where all reports get the new selections whenever they are added.
So let's add a column to the Locations table, called SQLStatement. For the Country row, this will have the value:
SELECT CountryId AS Id, CountryName AS Description FROM Countries
For the Region row, the SQLStatement field has the value:
SELECT RegionId AS Id, RegionName AS Description FROM Regions
Now the Locations table has the value of the SQLStatement in it for the Selections dataset. You can't use this directly (your dataset would just return the value of the SQL statement field, not execute it) but you want to have something that returns this string as the expression to use for the SQL statement for the Selections dataset. Custom code can be used to do this. So the expression to use for the Selections dataset will be something like this:
=Code.GetSQLStatement(Parameters!Location.Value)
And then you have custom code function like this (significant parts left out):
Public Function GetSQLStatement(LocationId As Integer) As String
Dim SQL As String
SQL = "SELECT SQLStatement FROM Locations WHERE LocationId = " + CStr(LocationId)
' Connect to database, get SQLStatement field
GetSQLStatement = <Field Value>
End Function
When you want to add another location selection, for example continents, all you have to do is add another row to the Locations table, say LocationId = 3, Description = Continent and SQLStatement = SELECT ContinentId AS Id, ContinentName AS Description FROM Continents and now every report you have that selects by location will be able to use Continents.
First dataset add this SQL
Select ContryName, CountryID From Country
Assuming the name the above dataset's parameter is #country add the following SQL on the second dataset
Select RegionName, RegionID From Region
Where CountryID IN( #country)
It is very simple. Let’s assume that the data of the table “ONE” as follows:
**Location_Type** **Location**
Country India
Country Sri Lanka
Country China
Country Japan
City Bangalore
City Hyderabad
City Delhi
Ex:- Query for the Report parameter (#Location_Type1):
Select Distinct Location_Type from One
Query for the second Report Parameter:
Select Location from ONE where Location_Type = #Location_Type1.
Please let me know if it is not clear.

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.