MS-Access - DLookup using values entered on form - ms-access

I am a complete novice with no experience of building a database. I have been following a few online tutorials to help with a little project I want to work on.
Here is a pic of my current table relationships:
I have a form where I want to enter invoice details. I want to be able to select a product from a drop-down and for it to automatically populate a text box with the cost. The problem i have is, depending on the customer, the cost can be different.
Here is a pic of my form:
I have tried using DLookUp in the Unit Price field but i just can't seem to get the hang of it.
Basically, in pseudo code, i want the unit price field to:
Select From tblPrices Where PriceID =
I've tried:
=DLookUp([Product],"tblPrices","PriceID = " & [Forms]![tblInvoice]![SalesTypeID])
but it is returning the ID of the selected from product in the form from the Products table ?!

Related

How to enter a value in a text box controlled with a Dlookup in MS Access

I have a Form the fields are UPC, PurDate and Price.
The Price is controlled by a Dlookup to get the most recent price from the Price Table. It works by the user scanning the barcode of the product that fills the UPC field and the Price is automatically filled. This works good so far.
The problem is that some products do not have barcodes so I want to be able to manually fill the price for that item. Is there anyway to 'override' the Dlookup function or use some vba code to fill the Price field with user input?
Or maybe setup a UPC code eg. 0001 and when that code is selected the user can enter a price?
A control with an expression in ControlSource does not allow edit by user. Instead, use code (macro or VBA) to lookup price and populate field. Bind textbox to field. Now user can type over value retrieved. The real trick is figuring out what event to put code into.

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.

Populate two rows of a combobox with a query Access 2007

I'm working on an Access 2007 database and I'm having a problem with a query.
I have a table named Vehicles, which contains data such as ID, license plate and fuel type for each vehicle in it.
I'm trying to make a query which will populate a combobox in a Form with each vehicle's fuel type, based on the license plate chosen by the user beforehand.
The thing is, we have some some cars that work with two types of fuel and I cant' find a way to display them separately in the combobox.
So far it kinda works, code follows:
CheckDiesel: IIf([Diesel]="Yes";"Diesel";IIf([Gasoline] AND [Ethanol]="Yes";"Gasoline"+ "Ethanol";IIf([Ethanol]="Yes";"Ethanol";IIf([Gasoline]="Yes";"Gasoline";""))))
If you look at the second IIf, I have a condition for a bi-fuel car. I want to display Gasoline and Ethanol separately, each one in a row.
I've tried using "&Chr(10) Chr(13)&" and "\r\n" but I had no success so far.
Can anyone help me?
Storing multiple pieces of data in a single field rarely ends well. A few options I see
A series of binary fields for gasoline type. So you'd have a True/false for gasoline, ethanol, and diesel. This is easy to show with checkboxes on the form.
If you know there will only be certain combinations, like if there will not be a 'diesel and ethanol' without gasoline, you can build it as a single value combobox.
You will probably need some VBA to do what you need to do. Try something like this:
Dim R as String
R=""
if (Me.Diesel) then R = R & "Diesel;"
if (Me.Gasoline) then R = R & "Gasoline;"
… {add any other types of fuel}
Me.MyComboBox.Rowsource = R
Me.MyComboBox.Requery
This can be added tot he form's OnCurrent event so it will update the combo box every time a new record is displayed.
If your form does not include the various fuel types as fields, either add them as hidden fields or use DLookup to read them off the table.

Replacement for DLookup to autofill a form with multple values in Access

I currently have a form which contains a seperate combo box that is related to each table I have created.
These include:
- Business Process Area
- Title
- ReportDescription
- ProcessChain
- MultiProvider
- InfoProvider
I'm having multiple issues, but for now I'll just narrow it down to the one.
I want the form to autofill if you select a lower combo box, so for example if you select a Report Description it will fill Title and Business Process Area. I am using DLookup to do this currently and this is working fine. The issue occurs further down the form.
There are many-to-many relationships between ReportDescription and ProcessChain as well as between ProcessChain and MultiProvider. So currently you can select a MultiProvider, and it will just select the first ProcessChain and keep filling the form from there.
My Current DLookup code on my bottom combo box is:
If IsNull(cmbMultiProvider) Then
cmbMultiProvider = DLookup("MultiProviderID", "MultiProvider", "MultiProviderID =" & Me.cmbInfoProvider.Column(2))
End If
If IsNull(cmbProcessChain) Then
cmbProcessChain = DLookup("ProcessChainID", "ProcessChainMultiProvider", "ProcessChainID =" & Me.cmbMultiProvider.Column(2))
End If
If IsNull(cmbReportDesc) Then
cmbReportDesc = DLookup("ReportID", "ReportDescription", "ReportID =" & Me.cmbProcessChain.Column(2))
End If
And so fourth.
So I would like to replace these DLookup statements with something that will stop at the combo box if the selection below relates to multiple of the field above, as in if a MultiProvider selected in a combo box relates to many Process Chains then the combo box will drop down and only contain the related fields.
Thank you in advance for any help.
So you're going "backwards" up the cascading combos? That is, in most cases you would select these items in descending order, but you're moving up in Ascending order. For example, if you wanted to drill down to an automobile, you generally first select the Year, then the Make, then the Model, etc etc.
Instead, you want to select the Make, and have the Model auto-populate (assuming there is only one Make for that specific Model), and then have the Year left blank (since there could be several Years for a Make and Model).
If so, can you tell us more about your data structure? How are Report Description, Process Chain and MultiProvider related in your tables?

Selecting data from a table and auto-filling fields

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 ;-)