Microsoft Access question - ms-access

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.

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.

How to do a lookup or index/match in Access?

I'm new to access and I'm struggling on how to use lookups since vlookup is not available in Access. I want to get the values that correspond to a certain ID/tagging.
I have 2 tables.
Table A contains values let's say Product Number, Product Type, Price, Remaining Stock #, Product Type+Product Number Tag. Let's say product number is not unique but combining it with its product type, it will be unique so I created that tag.
Table B contains Seller's Name, # of items Sold, Product Number, Product Type, Product Type+Product Number Tag.
Now using Table A and Table B, how can I create a query/table that will allow me to use that "Product Number + Product Type" Tag when I try to get the price of that certain item so that I can get the total revenue of each seller.
I hope you understand what I'm trying to say. What I just want to do is I want to use this "Type+Number" Tag as a reference point in getting data of that respective item when I try to create queries/tables. It's just like an INDEX/MATCH in Excel. But how to do it in Access?
Please tell me if it's unclear.
Thank you!
You need to join your two tables based on the relationship between the Product Number & Product Type fields in both tables.
The two fields in Table A should be marked as the composite Primary Key (select both fields and in the Design ribbon click the Primary Key icon).
In Table B they will be Foreign Keys - a seller could sell those products more than once, so duplicates are allowed here.
You don't need the Product Type+Product Number Tag field.
The SQL for your query would then be:
SELECT *
FROM [Table A] LEFT JOIN [Table B] ON [Table A].[Product Number] = [Table B].[Product Number] AND
[Table A].[Product Type] = [Table B].[Product Type]
This will return all records from Table A and only those records from Table B that match the Primary Key.
Finally.... don't think of an Access table as an Excel spreadsheet. Access is all about the relationship between pieces of data - for a start queries can be expressed in plain English a lot easier.
E.g return all records from table B where seller name is "Dave" and date is between 1st Jan and 31st Jan would be written as:
SELECT *
FROM [Table B]
WHERE [Seller Name]='Dave' AND
[Sale Date] Between #01/01/2018# AND #01/31/2018#
(SQL only deals in US date format).
You can use DlookUp Function.
It can be used on queries as a calculated field, or in code in VBA:
For example, to get the price of a PRoduct, you could use something like:
DlookUp("[Price]";"Table A";"[Product Type+Product Number Tag]='" & Value & "'")
From my point of view, the complex part of DlookUp is the third argument, the WHERE clausule. If you have any experience with SQL, you will have no problem. If you don't, don't worry, just read some info and if you get stuck, come here to SO
You can use DlookUp to get any value of any field, based on a criteria (criteria applied to a unique field, ofc).
And yes, you can use it to get values from tables or from queries. In the link I provided before, it explains how the arguments works.
The most complex part is the criteria part. You must write as if you were typing a WHERE clausule on SQL more info here
About the criteria, always remember this:
If your criteria is a numeric value, then just type [field_criteria]=my_numeric_criteria
If your criteria is a text value, you must use single quotes. For example, [field_criteria]='my_text_criteria'
SQL requires single quotes around text values.
Try it!

MS Access Form: How to show all records that reference current record

I have a form showing Customers. Now I would like to also display all Orders that the currently open customer has made.
The Order table has a foreign key to Customer.
I have tried using a list box with Multi Select set to Simple, but somehow it shows me all Orders instead of just the ones of the current customer.
More Details:
My list box has
Control Source: ID
Row Source: SELECT customer.id, order.info FROM customer
INNER JOIN order ON customer.ID = order.customer_id
If I set the Multi Select to None it always marks the first Order that matches the current customer, but not all matching orders.
Can someone point me in the right direction?
Thank you.
P.S. I don't necessarily want the list to be functional for the creation of a new Customer. If it would also work then that's a bonus.
Assuming your form is bound to Customers table, then you need to add code to the form's Current event:
lstOrders.RowSource = "SELECT id, info FROM order WHERE customer_id = " & Me.id
lstOrders.Requery
The list box (called lstOrders) should have its column count set to 2. If you don't want to see the order.id column then set the "Column widths" property to 0 (this will set the width of the first column to 0 and let the second column, order.info, fill the remaining width of the list). Set the "Bound column" to 1 - this means that the "value" of the list box will be the order.id

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

DLookup to return prices

Good afternoon all,
I am in the process of creating a database in which I want to enter a product name and a dose and a price will be returned. I have created two tables (tbllog and tblcosting). The tblcosting contains the products along with all the available doses and the prices they cost. Is there a way of inputting the product and dose onto one form and having a box that returns the price based on these two criteria?
Any advice you could pass on would be very much appreciated.
Select product from a combobox. Make the combobox RowSource multi-column:
SELECT ProductID, ProductName, Dose, Cost FROM tblCosting ORDER BY ProductName;
Have textboxes reference the combobox columns by index to display the associated info. Index begins with 0 so Cost is in column index 3:
=[cbxProduct].Column(3)