Solved

Select Statement

Posted on 2001-06-19
7
243 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 50 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

713 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