473,499 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to add prices together.

17 New Member
I am kind of new to Access but I am trying very hard. I have a pretty good grasp of making tables, forms, and queries, but I am having a very difficult time with getting a certian thing done. I am trying to create a form from a simple table that I have created to pull multiple items out of the table and total the costs together to get a total costs of the items selected. Basically I have a table with 2 fields titled Item and Cost. What I want is to have one control in the form for "item" that I can pick multiple selections from. I also want to be able to choose a quantity for each of those items and then get a grand total cost at the end. Any help you can give will be much appreciated.
Sep 7 '07 #1
7 1609
Stwange
126 Recognized Expert New Member
For a beginner, this is a pretty big task to take one, but all the better for your learning curve :)
I had two ideas for this, add the values to a temporary table which is cleared first, or add them to a listbox.
I decided on the second one, because then you can see your order, but I recommend you try to understand the code I give you
rather than just using it. Here goes:

First, create an unbound combobox, when the wizard comes up, click cancel (as with all the other controls). Call it cmbItem
Go to the properties of cmbItem (right click on it -> properties) and ensure the Row/Source Type is set to "Table/Query"
Then, in the Row Source, enter the following SQL: "SELECT item FROM ENTERYOURTABLENAMEHERE ORDER BY item;"
Next, add an unbound text box next to it for the quantity, call this txtQuantity.
Next, add a listbox at the bottom, called lstOrder.
Next, add a command button somewhere called cmdTotal and set the caption to "Total".
Finally, add a command button at the side of txtQuantity called cmdAdd. Change its caption to "Add", and then double click on it
so that the On Event window comes up. Next to "On Click", click the "..." at the right hand side, click Code Builder, and
click OK.

Delete all the code that comes up, and paste all of the following (removing line numbers):
Expand|Select|Wrap|Line Numbers
  1. Dim total As Single
  2. Private Sub Form_Load()
  3.     Dim i As Integer
  4.     For i = 0 To lstOrder.ListCount - 1
  5.         lstOrder.RemoveItem (0)
  6.     Next i
  7.     total = 0
  8. End Sub
  9.  
  10. Private Sub cmdAdd_Click()
  11.     If Nz(txtQuantity.Value, "") = "" Then txtQuantity.Value = "1"
  12.     If Nz(cmbItem.Value, "") <> "" Then
  13.         Dim rs As DAO.Recordset
  14.         Set rs = DBEngine(0)(0).OpenRecordset("SELECT Cost FROM INSERTTABLENAMEHERE WHERE Item = '" & removeApostrophes(cmbItem.Value) & "';")
  15.         If Not rs.EOF Then total = total + CSng(rs!Cost)
  16.         lstOrder.AddItem (txtQuantity.Value & "x " & cmbItem.Value)
  17.     End If
  18. End Sub
  19.  
  20. Private Sub cmdTotal_Click()
  21.     MsgBox "Your total so far is: £" & Round(total, 2), vbInformation, "Your order so far:"
  22. End Sub
  23.  
  24. Public Function removeApostrophes(ByVal s As String) As String
  25.     removeApostrophes = s 'Please note, this function will cause a stack overflow if passed a String with more than 2878 apostrophes in it.
  26.     If LenB(Nz(s, "")) <> 0 Then
  27.         Dim pos As Integer: pos = InStr(s, "'")
  28.         If pos <> 0 Then removeApostrophes = left(s, pos) & "'" & removeApostrophes(Mid(s, pos + 1))
  29.     End If
  30. End Function
  31.  
PLEASE NOTE: This code is UNTESTED, but if it doesn't work it should at least give you a rough idea.
Sep 7 '07 #2
mdpems
17 New Member
Thank you for the reply. I have done everything as insructed by in line 11 of the code supplied is kicking back an error stating object required. I can't figure out where this is coming from. Can you help further.

Private Sub cmdAdd_Click()
If Nz(txtQuantity.Value, "") = "" Then txtQuantity.Value = "1"
If Nz(CmbItem.Value, "") <> "" Then
Dim rs As DAO.Recordset
Set rs = DBEngine(0)(0).OpenRecordset("SELECT Cost FROM [Medical Supplies] WHERE Item = '" & removeApostrophes(CmbItem.Value) & "';")
If Not rs.EOF Then total = total + CSng(rs!Cost)
lstOrder.AddItem (txtQuantity.Value & "x " & CmbItem.Val
Sep 8 '07 #3
mdpems
17 New Member
Thank you for the reply. I have done everything as insructed by in line 11 (If Nz(txtQuantity.Value, "") = "" Then) of the code supplied is kicking back an error stating object required. I can't figure out where this is coming from. Can you help further.

Thanks

Private Sub cmdAdd_Click()
If Nz(txtQuantity.Value, "") = "" Then txtQuantity.Value = "1"
If Nz(CmbItem.Value, "") <> "" Then
Dim rs As DAO.Recordset
Set rs = DBEngine(0)(0).OpenRecordset("SELECT Cost FROM [Medical Supplies] WHERE Item = '" & removeApostrophes(CmbItem.Value) & "';")
If Not rs.EOF Then total = total + CSng(rs!Cost)
lstOrder.AddItem (txtQuantity.Value & "x " & CmbItem.Val
Sep 8 '07 #4
missinglinq
3,532 Recognized Expert Specialist
For line 11 try

Expand|Select|Wrap|Line Numbers
  1. If IsNull(Me.txtQuantity.Value) Then Me.txtQuantity.Value = "1"
Welcome to TheScripts!

Linq ;0)>
Sep 8 '07 #5
mdpems
17 New Member
The form is working now my mistake on spelling. It took a little while to figure it out. I have run into another problem. The form is working as it should by listing the info, but it will not multiply the quantity by the cost of the item. Any suggestions.

Thanks,
You guys are great.


Private Sub cmdAdd_Click()
If Nz(TextQuantity.Value, "") = "" Then TextQuantity.Value = "1"
If Nz(CmbItem.Value, "") <> "" Then
Dim rs As DAO.Recordset
Set rs = DBEngine(0)(0).OpenRecordset("SELECT Cost FROM [Medical Supplies] WHERE Item = '" & removeApostrophes(CmbItem.Value) & "';")
If Not rs.EOF Then total = total + CSng(rs!Cost)
lstOrder.AddItem (TextQuantity.Value & "x " & CmbItem.Value)
End If
End Sub

Private Sub cmdTotal_Click()
MsgBox "Your total so far is: $" & Round(total, 2), vbInformation, "Your order so far:"
End Sub

Public Function removeApostrophes(ByVal s As String) As String
removeApostrophes = s 'Please note, this function will cause a stack overflow if passed a String with more than 2878 apostrophes in it.
If LenB(Nz(s, "")) <> 0 Then
Dim pos As Integer: pos = InStr(s, "'")
If pos <> 0 Then removeApostrophes = Left(s, pos) & "'" & removeApostrophes(Mid(s, pos + 1))
End If
End Function
Sep 8 '07 #6
Stwange
126 Recognized Expert New Member
Sorry, try this:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdAdd_Click()
  2. If Nz(TextQuantity.Value, "") = "" Then TextQuantity.Value = "1"
  3. If Nz(CmbItem.Value, "") <> "" Then
  4. Dim rs As DAO.Recordset
  5. Set rs = DBEngine(0)(0).OpenRecordset("SELECT Cost FROM [Medical Supplies] WHERE Item = '" & removeApostrophes(CmbItem.Value) & "';")
  6. If Not rs.EOF Then total = total + (CSng(rs!Cost) x CInt(TextQuantity.Value))
  7. lstOrder.AddItem (TextQuantity.Value & "x " & CmbItem.Value)
  8. End If
  9. End Sub
  10.  
Sep 10 '07 #7
mdpems
17 New Member
Thanks so much. Works perfectly!!!
Sep 11 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
2057
by: PR | last post by:
Searchable product images + prices database? What system should I use to put a database on the WEB which shows images and allows searches, including searches within given price ranges for...
15
2628
by: Colin | last post by:
I have a query that calculates the selling price of products on customer orders. Selling prices are calculated based on the average cost of the items when purchased. As I make new purchases, the...
0
1284
by: chris | last post by:
Cell Phone Repair & Unlocking Service, Fast Turn-Around & Great Prices! The Cell Shop http://www.cellphonerepair.org
4
1344
by: Bruce One | last post by:
Well folks, considering the high prices for the current VSTS MSDN Premium subscription, I propose we take a pause and look of what each one of the 3 possible package DOES NOT have: Team Edition...
0
1800
by: robietrader | last post by:
Get trading software, systems, books at the best prices with GUARANTEED delivery. We have the software, we have proof and will NEVER scam you! All software is tested and working. Numerous satisfied...
6
5397
by: Pete Kane | last post by:
Hi All, does anyone know a site that I can query share prices programmatically ( possibly passing in a stock symbol )?
0
1033
by: service0043 | last post by:
Some bargain prices can be found on jewelry that is sold in bulk and at auction houses on the internet. While the condition of the items might be used, some fine jewelry pieces will be sold as new....
0
866
by: xiaobb16 | last post by:
golden suppliers,lowest prices .cover all brand products accept PAYPAL . Prices are at the lowest notch.air jordan air max R3 R4 NZ TN shoes www.cheapnetstore.cn
6
1364
by: Paul Herber | last post by:
I'm using the code below to retrieve price data from my e-commerce provider. The plan being to only have prices stored in one location and for there to be correct real-time currency conversions....
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7014
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7229
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
4921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
311
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.