473,508 Members | 2,247 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

To SUM a table by month in VB6

3 New Member
I will have two text boxes where one will display the month (January for example) and after clicking on a command button, the second text box will sum the total sales under that month.

The database is CHEF_HELPER1.MDB
The month table name is month
The sales table is housesales

I have code that will sum the sales in housesales, but I want to sum it by the month in text1.text. So if January is in text2.text, it will sum all the sales in housesales where January is the month. The same for the rest of the months.

I will use the code for two tables in two separate command buttons.
one for table housesales, and the other for table bar_sales.

Code listed below is for bar_sales.

I also would like the text1.text formatted as "#########.00"

Could some kind soul please help me with the code?

What I have now which works is the following.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim MyCon As New ADODB.Connection
  3.  Dim housesales As New ADODB.Recordset
  4.  Dim sConnect As String
  5.  Dim food_sales
  6.  food_sales = Format("#########.00")
  7.  
  8.  
  9.  text2.Text = Format$("#########.00") 'Format$(formula2, "#,###,000.00")
  10.  
  11. sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  12.  "Data Source=" & _
  13.  App.Path & "\CHEF_HELPER1.MDB"
  14.  
  15. MyCon.Open sConnect
  16.  housesales.Open "select sum(food_sales) from housesales", MyCon, adOpenDynamic, adLockPessimistic
  17.  text2.Text = housesales.Fields(0)
  18.  housesales.Close
  19.  MyCon.Close
  20.  

It sums all the data in table bar_sales successfully.

Thank you.
Kevin
Jul 5 '12 #1
4 3109
kevrich69
3 New Member
I made a mistake in the description, I will just be using one table, housesales, and two fields... food_sales, and bar_sales.
Jul 5 '12 #2
Killer42
8,435 Recognized Expert Expert
It sounds as though you just need to add a WHERE clause in your SQL, to limit it to records matching the month.

However, what I don't understand is this code...
Expand|Select|Wrap|Line Numbers
  1. Dim food_sales
  2. food_sales = Format("#########.00")
  3. text2.Text = Format$("#########.00")
  4.  
As far as I can tell, all you're doing there is setting an otherwise-unused variable (food_sales, of type variant) and a textbox, to the value "#########.00". The Format function is used to take a numeric value and convert it to some display format. Since you haven't supplied a value, presumably it's taken to be zero.

(Format and Format$ are the same thing, by the way. The latter is simply for backward compatibility with very old code.)
Jul 7 '12 #3
kevrich69
3 New Member
Thank you for your reply.
I have removed the code you mentioned because it was left in when I was trying some different code which didn't work....

But I'm not sure how the correct code should be using the where clause. I have tried it before and get an error, probably because I'm leaving something out.

Could you give me an example of using the where clause with month? Would it be something like this?

Expand|Select|Wrap|Line Numbers
  1. On Error Resume Next
  2. Dim MyCon As New ADODB.Connection
  3.  Dim housesales As New ADODB.Recordset
  4.  Dim sConnect As String
  5.  
  6. sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  7.  "Data Source=" & _
  8.  App.Path & "\CHEF_HELPER1.MDB"
  9.  
  10. MyCon.Open sConnect
  11. housesales.Open "SELECT * FROM housesales WHERE month Like '" & TextBox3.Text & "%'"
  12.  
  13.  housesales.Open "select sum(food_sales) from housesales", MyCon, adOpenDynamic, adLockPessimistic
  14. Text4.Text = Format$(housesales.Fields(0), "#########00.00")
  15.  housesales.Close
  16.  MyCon.Close
  17.  
This isn't working. I type in July 2012 in text3.text and it sums June 2012 and July 2012 in text4.text
Jul 7 '12 #4
Killer42
8,435 Recognized Expert Expert
Sorry for the delay, I don't get to the site very often these days. Other forums here are much more active, of course; the pool of people who remember much about these 12-years-and-older VB versions probably isn't that great these days.

To provide a definite answer to this I'd need to know a bit more about your file. Most importantly, how is the month stored in the sales table? I mean, the field type and what values do you hold there? (Keep in mind I'm not expert with Access and SQL, I've just dabbled a bit over the years - the real experts hang out over in the Access/VBA forum).

If it's a Text field with values like "July 2012" stored there, then you can probably just change "Like" to "=", drop the "%" and it'll work.

For the sake of argument, I'll assume it's a Date/Time type, with the actual date (and possibly the time of day) that each sale was made. A query something like this might work:
Expand|Select|Wrap|Line Numbers
  1. housesales.Open "SELECT * FROM housesales WHERE month between #1 Jul 2012# AND #31 Jul 2012#"
Obviously this would require your code to generate the start and end values for the range. I've just hard-coded the start and end of July here.


Or alternatively:
Expand|Select|Wrap|Line Numbers
  1. housesales.Open "SELECT * FROM housesales WHERE Format(month,'mmmm yyyy') = 'July 2012'"
This one might allow you to use your value from TextBox3 directly.



Note, there's a fair chance that the first sample above would actually miss sales on 31st July. It's fiddly working with date/time fields, because they store time as well as date. If you're storing an actual time of day, the end of the range would need to be something like #31 Jul 2012 23:59:59#.
Jul 10 '12 #5

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

Similar topics

0
1414
by: jason | last post by:
Hi Everyone, I would really appreciate some thoughts on how best to tackle an availability calender in ASP for our yacht fleet. I need to be able to show the booking STATUS of a particular...
1
2233
by: Jim | last post by:
For some reason the compiler is telling me that I must declarethe variable @costcenter_tmp on lines 74 and 98...but if i put a select statement in ther (for testing) before the loop I get data back...
8
3609
by: Zero.NULL | last post by:
Hi, We are using Month-year tables to keep the history of long transaction of our application. For example: We capture the details of a certain action in table...
6
2973
by: Hasanain F. Esmail | last post by:
Hi all, I sincerly thank you all in advance for your help to solve this problem. I have been trying to find a solution to this problem for sometime now but have failed. I am working on a...
0
962
by: Brent Mondoux | last post by:
Hey everyone, I'm trying to get a recordset that looks like this: SQL: SELECT Year(financial_date) AS num_financial_year, DateName(m, financial_date) AS name_financial_month, DatePart(m,...
10
1251
by: Jim | last post by:
I'm sure this has been asked before but I can't find any postings. I have a table that has weekly inspections for multiple buildings. What I need to do is break these down by the week of the...
2
10003
Vasuki Masilamani
by: Vasuki Masilamani | last post by:
Hi, I have a SQL query like this. DECLARE @months table(Month int identity (1,1)) DECLARE @i int set @i = 0 WHILE(@i<12) BEGIN INSERT @months default values SET @i = @i + 1
5
3815
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
1
1061
by: lyter | last post by:
hi..i'm newbie in sql..i have a big problem..i'm used db access and asp code in my system..hope someone can help me to fix this issue.. here i have two table..1) eachclick 2)month 1st (in table...
2
1973
by: Sumaiya Patel | last post by:
i have a table Month which contains two columns month(type integer) and closed_mon(type bit). column month contains values such as 201004,201005.... where 2010 is year and 04 is the month (yyyymm...
0
7231
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
7132
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
7336
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
7401
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...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5059
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
3211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
432
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.