Using DSUM on a Recordset

C

Chaplain Doug

Excel 2003. I have the following code that queries an SQL database and fills
up a recordset called "rs." Is there a way to then use the DSUM function on
the recordset? I get an error when I try using the DSUM. The connection
works fine and the recordset is filled without error.

Set cn = New ADODB.Connection
cn.Open "Provider=sqloledb;" & _
"Data Source=HQServer;" & _
"Initial Catalog=Good_News_FE;" & _
"User ID=FEOpen7;" & _
"Password=fe"
sql = "SELECT GL7PROJECTS.PROJECTID AS ProjectID, GL7PROJECTS.DESCRIPTION
AS [Project Description], GL7ACCOUNTS.ACCOUNTNUMBER AS Account,
GL7ACCOUNTS.DESCRIPTION AS [Account Description], GL7FISCALPERIODS.SEQUENCE
AS [Month], GL7PROJECTBUDGETDETAILS.AMOUNT AS [Month Amt],
GL7PROJECTBUDGETS.AMOUNT AS Total, GL7FISCALPERIODS.STARTDATE,
GL7FISCALPERIODS.ENDDATE " & _
"FROM ((((GL7PROJECTBUDGETS INNER JOIN GL7PROJECTBUDGETDETAILS ON
GL7PROJECTBUDGETS.GL7PROJECTBUDGETSID =
GL7PROJECTBUDGETDETAILS.GL7PROJECTBUDGETSID) INNER JOIN GL7ACCOUNTS ON
GL7PROJECTBUDGETS.GL7ACCOUNTSID = GL7ACCOUNTS.GL7ACCOUNTSID) INNER JOIN
GL7FISCALPERIODS ON (GL7PROJECTBUDGETDETAILS.GL7FISCALPERIODSID =
GL7FISCALPERIODS.GL7FISCALPERIODSID) AND
(GL7PROJECTBUDGETDETAILS.GL7FISCALPERIODSID =
GL7FISCALPERIODS.GL7FISCALPERIODSID)) INNER JOIN GL7PROJECTS ON
GL7PROJECTBUDGETS.GL7PROJECTSID = GL7PROJECTS.GL7PROJECTSID) " & _
"INNER JOIN GL7FISCALYEARS ON GL7FISCALPERIODS.GL7FISCALYEARSID =
GL7FISCALYEARS.GL7FISCALYEARSID " & _
"WHERE (((GL7FISCALPERIODS.STARTDATE)>='1/1/2006') AND
((GL7FISCALPERIODS.ENDDATE)<='12/31/2006')) " & _
"ORDER BY GL7PROJECTS.PROJECTID, GL7ACCOUNTS.ACCOUNTNUMBER,
GL7FISCALPERIODS.SEQUENCE;"

Set rs = cn.Execute(sql, , adCmdText)

*THIS IS WHAT DOES NOT WORK

IFL = DSum("[Month Amt]", rs, "[Account]='1-4100'")
 
T

Tom Ogilvy

There are several thinks wrong with your attempt to use DSUM in VBA, but the
biggest constraint is that it only works on data in a range:

From help on DSUM:

DSUM(database,field,criteria)

Database is the range of cells that make up the list or database. A
database is a list of related data in which rows of related information are
records and columns of data are fields. The first row of the list contains
labels for each column.

Field indicates which column is used in the function. Field can be given
as text with the column label enclosed between double quotation marks, such
as "Age" or "Yield," or as a number that represents the position of the
column within the list: 1 for the first column, 2 for the second column, and
so on.

Criteria is the range of cells that contains the conditions you specify.
You can use any range for the criteria argument, as long as it includes at
least one column label and at least one cell below the column label for
specifying a condition for the column.

===========

If you are using SQL, you shouldn't need to use DSUM. Adjust your query to
return the summary information you seek.

--
Regards,
Tom Ogilvy


Chaplain Doug said:
Excel 2003. I have the following code that queries an SQL database and fills
up a recordset called "rs." Is there a way to then use the DSUM function on
the recordset? I get an error when I try using the DSUM. The connection
works fine and the recordset is filled without error.

Set cn = New ADODB.Connection
cn.Open "Provider=sqloledb;" & _
"Data Source=HQServer;" & _
"Initial Catalog=Good_News_FE;" & _
"User ID=FEOpen7;" & _
"Password=fe"
sql = "SELECT GL7PROJECTS.PROJECTID AS ProjectID, GL7PROJECTS.DESCRIPTION
AS [Project Description], GL7ACCOUNTS.ACCOUNTNUMBER AS Account,
GL7ACCOUNTS.DESCRIPTION AS [Account Description], GL7FISCALPERIODS.SEQUENCE
AS [Month], GL7PROJECTBUDGETDETAILS.AMOUNT AS [Month Amt],
GL7PROJECTBUDGETS.AMOUNT AS Total, GL7FISCALPERIODS.STARTDATE,
GL7FISCALPERIODS.ENDDATE " & _
"FROM ((((GL7PROJECTBUDGETS INNER JOIN GL7PROJECTBUDGETDETAILS ON
GL7PROJECTBUDGETS.GL7PROJECTBUDGETSID =
GL7PROJECTBUDGETDETAILS.GL7PROJECTBUDGETSID) INNER JOIN GL7ACCOUNTS ON
GL7PROJECTBUDGETS.GL7ACCOUNTSID = GL7ACCOUNTS.GL7ACCOUNTSID) INNER JOIN
GL7FISCALPERIODS ON (GL7PROJECTBUDGETDETAILS.GL7FISCALPERIODSID =
GL7FISCALPERIODS.GL7FISCALPERIODSID) AND
(GL7PROJECTBUDGETDETAILS.GL7FISCALPERIODSID =
GL7FISCALPERIODS.GL7FISCALPERIODSID)) INNER JOIN GL7PROJECTS ON
GL7PROJECTBUDGETS.GL7PROJECTSID = GL7PROJECTS.GL7PROJECTSID) " & _
"INNER JOIN GL7FISCALYEARS ON GL7FISCALPERIODS.GL7FISCALYEARSID =
GL7FISCALYEARS.GL7FISCALYEARSID " & _
"WHERE (((GL7FISCALPERIODS.STARTDATE)>='1/1/2006') AND
((GL7FISCALPERIODS.ENDDATE)<='12/31/2006')) " & _
"ORDER BY GL7PROJECTS.PROJECTID, GL7ACCOUNTS.ACCOUNTNUMBER,
GL7FISCALPERIODS.SEQUENCE;"

Set rs = cn.Execute(sql, , adCmdText)

*THIS IS WHAT DOES NOT WORK

IFL = DSum("[Month Amt]", rs, "[Account]='1-4100'")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top