Selecting data from a table and auto-filling fields - ms-access

I'm working on a small little project, at the moment just a test for a larger project, but I've hit a block that I can't find a solution too.
I have a list of items in a table, and they contain information such as the item name and the price. I have another table with sales. I have a form that will allow you to select a item name, however, then I would like the rest of the form to be auto-filled with information from the items table.
I can make a message box appear when the item name field has been changed, however, through my searching, I am unable to find a way to search the items table for the value of the field, then read the price value, then insert the price value into a field in this form.
First table "items":
itemname: text
price: currency
Second Table "sales":
itemname: text
price: currency
date: time/date
I hope I have explained myself well enough, if not, just ask and I will try to clarify.

Have a look at the Northwind datatabase (northwind.mdb) that ships with Access. They have several examples of how to update a form based upon a combobox changing values.
Also, you might want to look at the table design in the Northwind Database as well. Generally speaking, it is a bad idea to use the product name as the key field in both of your tables. What will happen (and it will eventually) is that the product name is going to change for one of the products and you'll have to update all of the tables that reference that product name.
The better design is to use a key field (I'd recommend an AutonumberField) in your products table and then reference the key field in the sales table. This way, if the product name changes you only have to make the change in one location, not many.
Here's a sample table layout to illustrate my point:
Table Items:
ItemID (Autonumber - Primary Key on the table)
ItemName (Text - Name of product)
Price (Currency)
Table: Sales
ItemID (Integer - Foreign Key to Items.ItemID)
Quantity (Integer - # of units ordered)
Price (Currency)
OrderDate (Date/Time)

Most answers here are posting way too much code and all kinds of SQL statements. As such, these answers are creating world poverty.
The most simple approach is to let the wizard build a combo box on your form to select a given item. This combo box will thus based on the items table. The first column of the combo box will be the PK (autonumber ID) of the items table. This combo box will thus be bound to the ItemID column in sales. The wizard will also “hide” the id, but you WANT to include the other columns in this combo box such as price etc. And you likely should have the 2nd column of this combo box the description of the item from the item table. As noted, you ALSO want to include the price column.
Thus, after you select a item, then to have auto matic fill out the price column, use this code:
This code goes in the AFTER update event of the item combo box:
Me.Price = me.ItemComboBox.column(2)
So when you select a item, the price field will to auto filled for you. And note how you only had to write one line of code.
So all of this can be done using built in wizards and one line of code.

Add an event procedure (code builder) to the dropdown box's onchange event. Right click the dropdown in design view and choose properties. then on the event tab in the properties window click in the on change line and click the '...' button.
In the code for this event you'll need to query the DB (using the dropdown box's index or ID field) to pull the items details into a recordset. The query would look somethign like
"SELECT * FROM Items WHERE ItemID = " & dropdownboxname.value
theres plenty of examples of how to do this on the web
Then you can update the various textboxes in the form with the required fields in the recordset. again theres plenty of examples of this on the web
EDIT:in response to comments below
you'll need to do somethign along the lines of...
Dim rsItems AS DAO.Recordset
Set rsItems = CurrentDB.OpenRecordset("SELECT * FROM Items WHERE ItemID = " & dropdownboxname.value)
If not rsItems.EOF Then
textbox1.text = rsItems![fieldname1]
textbox2.text = rsItems![fieldname2]
end if
Set rsItems = nothing
let me know if thats any help ;-)

Related

Concat ComboBox choices into textbox

I have 3 tables:
tbl1Artist:
ID - (PK),
ArtistName,
ArtistSKU
tbl1Medium:
ID - (PK),
MediumType,
MediumSKU
tbl1Artwork:
ID - (PK),
Artist_ID - (FK),
Medium_ID - (FK),
PieceName,
DateCompleted,
ArtWorkSKU,
Thumbnail
I want to create a form that has a ComboBox for choosing an Artist and a separate ComboBox for choosing the medium type. There will be a TextBox for inputting the name of the piece and a date picker (ComboBox). I need to concatenate the ArtistSKU based on the choice from the Artist with the MediumSKU based on the choice from the Medium ComboBox, and the first 5 letters of the PieceName from the TextBox.
I have done some research to try and figure out how to achieve this but with no luck. Can this be achieved inside of Access with queries or is VBA required to achieve the intended outcome?
I am not that familiar with Access and appreciate all the assistance and guidance.
Let me know if you need further information about this request.
This can be accomplished with expression in query or textbox. Use Left() function to extract first 5 characters.
Example in textbox:
=[cbxArtist] & [cbxMedium] & Left([tbxPiece],5)
If the value you want is actually in another column of combobox list, reference that column by its index. Index begins with 0.
=[cbxArtist] & [cbxMedium].Column(1) & Left([tbxPiece],5)
If you want to save calculated value to table, that would require code (macro or VBA), however, advise not to save. Saved calculated values can become 'out of sync' with raw data. This value can be calculated when needed. But if you must, then code would be like:
Me!ArtWorkSKU = Me.tbxArt
The real trick is figuring out what event to put this code into. Try the form BeforeUpdate event.

MS Access Auto Populate form based upon a different field's selection

I have a couple of tables in my database.
workout_moves - ID, workout Type, Exercise, Reps
session_moves - ID, Exercise(from workout_moves table), Weight, Notes
I have created a form called workout session which has created a subform for the session_moves table.
The session_moves subform looks like this :
This is fine -but when I select my exercise from the list, I want it to autopopulate the associated Reps value from the workout_moves table rather than forcing me to pick from the list, which is not quite what I would like.
As you can see in the workout_moves table, each exercise is matched to a reps column so this should be pretty easy:
I just cannot work out what the "Row Source" should be for the Reps column in this form in order to auto pull in the data associated with the selection in the Exercise column. Please help.
Set the control source to
=Dlookup("Reps";"workout_moves";"[workout Type] = '" & Nz(cboExercise;"") & "'")
in the Reps control. cboExercise is the controlname of the exercise combobox.
Think about changing foreignkey in table session_moves from Exercises to ID (use primarykeys as foreignkeys).

MS access2013 datasheet shows display values, but listbox only shows ID

I have a table named "customers", and a table named "cases". These relate to real estate transactions. On the table "cases" I have a field for "other party", which lets you enter a new customer or pick from an existing one if we have already had that person in our database from a different transaction.
I have created a form with a listbox so I can search all the case records via the customer and other party fields (I also show some other info about the case but am not needing to search that info). In formview the listbox shows the customer name, but only shows the ID number for the other party. When I go to the rowsource, buildevent, and see the query in datasheet view, the records display the way I want them to -by name, not showing ID numbers.
Why won't it display correctly on the listbox??? Please help! I have spent way to many hours trying to figure this out :( This is my first time using access and I am just figuring it out as I go.
this is the SQL from the query in the listbox rowsource:
SELECT Query3.Customers.ID, Query3.[Last Name], Query3.[First Name], Query3.[Other Party], Query3.[Property Address], Query3.[Assigned To], Query3.Lawyer FROM Query3;
and this is the SQL from Query3:
PARAMETERS [ [forms]]![FRM_SearchMulti]![SrchText] Text ( 255 );
SELECT CasesALL.*
FROM CasesALL
WHERE (((CasesALL.[Last Name]) Like "*" & [forms]![FRM_SearchMulti]![SrchText] & "*")) OR (((CasesALL.[First Name]) Like "*" & [forms]![FRM_SearchMulti]![SrchText] & "*")) OR (((CasesALL.[Other Party]) Like "*" & [forms]![FRM_SearchMulti]![SrchText] & "*"));
Listboxes and combo boxes have a hidden column feature, usually a primary/foreign key of underlying table. Users generally do not know which primary key they should pick, hence the connecting name/item displays for their selection. Your situation sounds like the key is not being hidden but displaying and with two columns designated, you do not see the Other Party field.
To fix, under Property Sheet / Format Tab of the listbox, adjust Column Count and corresponding Column Widths according to your query (i.e., recordsource of listbox). As shown below the first column of query is hidden with 0 inches of display while the next two fields will display at 2 inches:
Column Count: 3
Column Widths: 0"; 2"; 2"
...
List Width: 4"
Be sure column widths add up to your List Width or there will be cut-offs. Also, inches are automatically added when you enter numbers. Finally, do note the data value of the listbox will be whatever is designated in Bound Column under Property Sheet / Data tab:
Bound Column: 1
Usually, the bound column is the hidden field or it wouldn't be hidden or used!
You probably miss to set columnwidth(s) of the listbox to: 0

MS ACCESS - Adding items to Lookup Tables from a Master Query

A seemingly simple question that has me stumped.
I have a table [Inventory] with the following fields: [Category], [ItemName], [Price].
ex: "Plumbing", "ACME Showerhead", $43
I have a table [Categories] with the fields [Category], [SubCategory]
ex. "Plumbing", "Hardware" or
ex. "Flooring", "Wood"
I wish to have a form (ideally a standard grid) that allows me to enter new Inventory Records.
On this form would be the fields Inventory.Category, Categories.SubCategory, ItemName, Price.
When navigating to the field Inventory.Category, a drop down should allow me to select from Unique Categories.Category values. Once selected, the Subcategory field should allow me to select from valid subcategories of that Categories.Category. For example, if I chose "Plumbing" in the category field, I would only be presented with teh subcategory "Hardware" and not the subcategory "Wood".
I would also like the ability to add new Category Records while in this form, so I am not limited to whats in the dropdown. So for example, when adding a record to Inventory, if I want to create a new Category/SubCategory pair of say, "Plumbing", "Pipes", I should be able to do that without having to switch to another form/table or whatever.

Filtering A Lookup Field Based On Another Field

I have a lookup field in my table based on another table. I'm having trouble filtering those values based on another field that is entered prior to the field.
Is it possible to filter a lookup field based on another field?
EDIT
Let me try and clarify my original question, sorry about that. Ok, so I have a table1 that has the following fields: ID, Name, Logo.
If a user enters a specific name in the Name field, when they click on the Logo field, it'll only display those values associated that are similar to the name entered. Does that make any sense? If it does make sense, would there be an easier suggesion on accomplishing this task?
If you're talking about inside a table, the answer is "No". You can create cascading combo boxes on a form, but you can't base a lookup value in a field of a table off of a different field in that table (or the field in any other table).
Here is an example of how to handle filtering a combo box based on the value selected in another combo box:
I have the following form:
The combo boxes are named cboIntPN and cboManPN.
The Row Source for cboIntPN is set to: SELECT uniq_key, part_no, revision FROM inventor. The Row Source for cboManPN isn't set to anything.
When the user selects a value for Internal PN the following AfterUpdate Event is triggered:
Private Sub cboInternalPN_AfterUpdate()
[cboManPN].RowSourceType = "Table/Query"
[cboManPN].RowSource = "SELECT uniqmfgrhd, mfgr_pt_no FROM invtmfhd " & _
"WHERE uniq_key = '" & cboIntPN.value & "'"
End Sub
It sounds like he is having the same issue as me. I also wanted to filter a field in a table for data entry on another field's input and my conclusion is "it is time I stopped entering data manually in tables and begin to create Data entry forms. I was putting this task off until later, but if I don't do it now, I might make worse trouble for myself later.
Btw, what an old thread.