Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Select Statement

Posted on 2001-06-19
7
Medium Priority
?
252 Views
Last Modified: 2010-05-02
I want to generate a number.  I know what I wanna do, but just can't find the correct way to jot it down.  Please assist if you can.  

I want to generate a number by counting the records in my database for the current year.  Yes, that's right.  I want to count the number of records in a field called Tdate (01/06/01 - shortdate format) current year only - from a table called Travel only. I am using ADO.

Can you help me?
Your assistance is really appreciated.  

Thanks :)

Vannwms
0
Comment
Question by:vannwms
7 Comments
 
LVL 1

Expert Comment

by:llewelm
ID: 6208008
The SQL statement that you want is:

SELECT COUNT(*) FROM TableName WHERE TDate >= '01/01/2001' and TDate <= '12/31/2001'

To do this within ADO:
1) Create a new Connection object
2) Open the connection using the proper connect string (ODBC/DSN or DSN-less)
3) Execute the ExecuteSQL() method of the connection object to get the value returned from this SQL.
4) Use the Recordset.GetFieldValue() method to get the returned value.
0
 
LVL 5

Expert Comment

by:gbaren
ID: 6208036


select count(*) as Total from table where year(tdate) = year(Now)

0
 
LVL 33

Accepted Solution

by:
hongjun earned 200 total points
ID: 6208684
Try this. I am assuming you are using Access, db name is your_db.mdb, table name is your_table.

    Dim objConn As ADODB.Connection
    Dim objRs As ADODB.Recordset
    Dim intCount As Integer
    Dim strSQL As String
    Dim intYear As Integer
   
    Set objConn = New ADODB.Connection
    objConn.Open "Driver={Microsoft Access Driver (*.mdb)};" & _
                   "Dbq=" & App.Path & "\your_db.mdb;" & _
                   "Uid=Admin;" & _
                   "Pwd=;"
   
    intYear = Year(Now())
    strSQL = "select count(tDate) as cnt from your_table where tDate >= #" & DateSerial(intYear, 1, 1) & "# and tdate <= #" & DateSerial(intYear, 12, 31) & "#"
    Set objRs = objConn.Execute(strSQL)
    MsgBox "Count = " & objRs("cnt")
   
    objConn.Close
    Set objRs = Nothing
    Set objConn = Nothing

hongjun
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 33

Expert Comment

by:hongjun
ID: 6208689
If you are using other db, then simply change the connection string. See this for more information on connection strings.

http://www.able-consulting.com/ADO_Conn.htm

hongjun
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6208691
Note you need to include "Microsoft ActiveX Data Objects" library.

hongjun
0
 
LVL 6

Expert Comment

by:andyclap
ID: 6209820
Could also use between
strSQL = "select count(tDate) as cnt from your_table where tDate between #" & DateSerial(intYear, 1, 1) & "# and #" & DateSerial(intYear, 12, 31) & "#"
0
 

Author Comment

by:vannwms
ID: 6215611
Great Solution Hongjun! Works like a charm. I appreciated your assistance.

Vannwms  
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question