Recordset Help!

G

Guest

Hello, I am in need of some help. I am using Access 97 and I am currently
building a module. The module is looking at a table (name:14_Table) and
pulling certain criteria and placing the data into a template in excel. The
criteria I am pulling is using a OpenRecordset function: (my code example)
Dim rst As Recordset

strSQL = ("SELECT * FROM 14_Table WHERE FACILITY_GROUP = 'Baptist'
AND" & " LAG >=0 And LAG <=15")
Set rst = CurrentDb.OpenRecordset(strSQL)
If rst.RecordCount = 0 Then
.Range("C23").Value = "0"
Else
rst.MoveLast
.Range("C23").Value = rst.RecordCount
EndIf

Ok, that is how I am pulling criteria for a count of total records and
placing them in a excel template. Now the question is, how can I create
another recordset type code to pull the SUM of the field "TOTAL_BILLED" from
those records the above code pulls??? Any ideas?
 
G

Guest

lngTotBilled = nZ(DSum("[TOTAL_BILLED]", "14_Table", "[FACILITY_GROUP] =
'Baptist' AND LAG BETWEEN 0 AND 15"),0)
Note the Dsum surrounded by the Nz function. That is so if no records match
the criteria and Null is returned, it will be converted to 0.
 
G

Guest

BTW, you could also use the DCount function instead of a recordset to do what
you are doing.

.Range("C23").Value = Nz(DCount("*", "14_TABLE", _
"[FACILITY_GROUP] = 'Baptist' AND [LAG] BETWEEN 0 AND 15"),0)
 
G

Guest

Klatuu, thank you so much! You guys are awesome, that's why I always come
here for help! Thanks again, have a good one!!!

Klatuu said:
lngTotBilled = nZ(DSum("[TOTAL_BILLED]", "14_Table", "[FACILITY_GROUP] =
'Baptist' AND LAG BETWEEN 0 AND 15"),0)
Note the Dsum surrounded by the Nz function. That is so if no records match
the criteria and Null is returned, it will be converted to 0.

MCyn said:
Hello, I am in need of some help. I am using Access 97 and I am currently
building a module. The module is looking at a table (name:14_Table) and
pulling certain criteria and placing the data into a template in excel. The
criteria I am pulling is using a OpenRecordset function: (my code example)
Dim rst As Recordset

strSQL = ("SELECT * FROM 14_Table WHERE FACILITY_GROUP = 'Baptist'
AND" & " LAG >=0 And LAG <=15")
Set rst = CurrentDb.OpenRecordset(strSQL)
If rst.RecordCount = 0 Then
.Range("C23").Value = "0"
Else
rst.MoveLast
.Range("C23").Value = rst.RecordCount
EndIf

Ok, that is how I am pulling criteria for a count of total records and
placing them in a excel template. Now the question is, how can I create
another recordset type code to pull the SUM of the field "TOTAL_BILLED" from
those records the above code pulls??? Any ideas?
 

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