473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Sum in a dynamic query

I adapted the code below from steve.minnaar (can't thank you enough) for a
table that contains data dating back to 2002 and it works excellent for my
hours and amount fields, but the table also contain two other fields that are
numbers; the month period number (ITDPdNo) and the year (ITDYr) that are
summing up as well. These two fields are vital for my end users to select the
data they need.
Is there anyway to modify the code so that it does not sum these two field. I
can't change the fields to text as my sorts and parameters would not work
correctly. There is only the one table in this applications and it does not
contain a key id.
Would appreciate any suggestions,
Thanks in advance,
dry
Finally here's the grouping solution (work got in the way of play)

Option Compare Database
Option Explicit

Private Sub Form_Open(Cancel As Integer)
' Populate the TableList List Box with all table names
Dim db As Database, Tbl As TableDef
Dim s As String
Set db = CurrentDb
s = ""
For Each Tbl In db.TableDefs
With Tbl
If Left$(.Name, 4) = "MSys" Then
' Skip system tables
Else
s = s & .Name & ";"
End If
End With
Next Tbl
Me!TableList.RowSource = s
End Sub

Private Sub TableList_AfterUpdate()
' Populate the FieldList List Box with all field names from the selected
table
Me!FieldList.RowSource = Me!TableList
End Sub

Private Sub btnGroupQuery_Click()
' Build the SQL to display the selected fields

' Create DynamicQ manually as, say, SELECT ID FROM Table1; and save it.
' This code will replace the SQL with each usage.
Dim db As Database, qd As QueryDef, ctl As Control, s As String, Item As
Variant
Dim Tbl As TableDef, Fld As Field, FieldName As String, Tail As String
Set db = CurrentDb
Set qd = db.QueryDefs("DynamicQ")
If IsNull(Me!TableList) Then
MsgBox "No table selected"
Else
Set Tbl = db.TableDefs(Me!TableList)
' Field Data type values
' 1 dbBoolean Boolean
' 2 dbByte Byte
' 3 dbInteger Integer
' 4 dbLong Long
' 5 dbCurrency Currency
' 6 dbSingle Single
' 7 dbDouble Double
' 8 dbDate Date / Time
' 9 dbBinary Binary
' 10 dbText Text
' 11 dbLongBinary Long Binary (OLE Object)
' 12 dbMemo Memo
' 15 dbGUID Guid
' 16 dbBigInt Big Integer
' 17 dbVarBinary VarBinary
' 18 dbChar Char
' 19 dbNumeric Numeric
' 20 dbDecimal Decimal
' 21 dbFloat Float
' 22 dbTime Time
' 23 dbTimeStamp Time Stamp
' Note that numeric types range from 2 to 7
s = "SELECT "
Tail = " GROUP BY "
' SELECT SalesPerson, Region, Sum(Amount) AS SumOfAmount
' FROM Sales
' GROUP BY SalesPerson, Region;
Set ctl = Me!FieldList
For Each Item In ctl.ItemsSelected
FieldName = ctl.ItemData(Item)
If Tbl.Fields(FieldName).Type 1 And Tbl.Fields(FieldName).Type
< 8 Then
s = s & "Sum([" & FieldName & "]) AS [SumOf" & FieldName &
"], "
Else
s = s & "[" & FieldName & "], "
Tail = Tail & "[" & FieldName & "], "
End If
Next Item
If s = "SELECT " Then
MsgBox "No fields selected"
Else
s = Left$(s, Len(s) - 2) & " FROM [" & Me!TableList & "]"
If Tail <" GROUP BY " Then s = s & Left$(Tail, Len(Tail) - 2)
qd.SQL = s & ";"
DoCmd.OpenQuery "DynamicQ"
End If
End If
End Sub

Private Sub btnSelectQuery_Click()
Dim db As Database, qd As QueryDef, ctl As Control, s As String, Item As
Variant
Set db = CurrentDb
Set qd = db.QueryDefs("DynamicQ")

s = "SELECT "
Set ctl = Me!FieldList
For Each Item In ctl.ItemsSelected
s = s & "[" & ctl.ItemData(Item) & "], "
Next Item
If s = "SELECT " Then
MsgBox "No fields selected."
Else
qd.SQL = Left$(s, Len(s) - 2) & " FROM [" & Me!TableList & "];"
DoCmd.OpenQuery "DynamicQ"
End If
End Sub

--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200705/1

May 14 '07 #1
0 1462

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jack | last post by:
Hi, I am trying to test a sql statement in Access which gives me the error as stated in the heading. The sql statement is built as a part of asp login verification, where the userid and password...
5
by: Terri | last post by:
I have a form with a multi-select combo. I dynamically build a SELECT statement, open a report, and set the recordsource to my dynamic SELECT statement. I count the records returned in the report...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
3
by: Peter Bailey | last post by:
Could someone please tell me how to pass criteria as if it were a parameter. I have a routine now that creates the sql string (well almost). at present the parameter is so I can pass one item ie...
0
by: starace | last post by:
I have designed a form that has 5 different list boxes where the selections within each are used as criteria in building a dynamic query. Some boxes are set for multiple selections but these list...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
2
by: deejayquai | last post by:
Hi I'm trying to produce a report based on a dynamic crosstab. Ultimately i'd like the report to actually become a sub report within a student end of year record of achievement. The dynamic...
10
by: jflash | last post by:
Hello all, I feel dumb having to ask this question in the first place, but I just can not figure it out. I am wanting to set my site up using dynamic urls (I'm assuming that's what they're...
3
by: creative1 | last post by:
Here is how you create a complex data report that involves parent and child commands and you can update information at runtime. Its pretty straight forward to work with simple queries; however,...
2
by: Mucahit ikiz | last post by:
I cant make a full dynamic query in LINQ I have 2 situation methods (only_exp_query, only_tbl_query) those are working. .... using System.Linq.Dynamic; using System.Data.Linq; .... string...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.