[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL Statement for VB2010

Posted on 2011-09-16
6
Medium Priority
?
268 Views
Last Modified: 2012-06-27
Dear Experts,

I have the SQL Statement as below to fetch records from two Access 2003 tables "Attendance" and "Employee". It works perfectly on Access 2003 Query Editor.
Unfortunately, it never gets working on VB2010.
Could you please help to review and advise ?

Best Regards,
Patrick


SQL Statement for fetching Attendance records between Sep 1, 2011 and Sep 16, 2011SELECT Employee.Name, Employee.Dept, Employee.Division, Attendance.ADate, Attendance.AOn, Attendance.AOff, Attendance.AOff2, Attendance.OVTM, Attendance.OVTM2, Attendance.WeekDay, Attendance.Holiday, Attendance.LateMin FROM Attendance INNER JOIN Employee ON Attendance.EName = Employee.Name
WHERE Employee.Name Like '*' AND Employee.Dept Like '*' AND Employee.Division Like '*'
AND Attendance.ADate Between #09/01/2011# And #09/16/2011# AND Employee.Attd=-1
ORDER BY Employee.Name, Attendance.ADate;


Coding in VB2010      
Dim cnn As New OleDb.OleDbConnection
Dim dbFullPath = "\\JPHK-FS2\Attendance\Attendance_be.mdb"
cnn = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbFullPath & ";Jet OLEDB:Engine Type=4;")

Dim strSQL As New OleDb.OleDbCommand("SELECT Employee.Name, Employee.Dept, Employee.Division, Attendance.ADate, Attendance.AOn, Attendance.AOff, Attendance.AOff2, Attendance.OVTM, Attendance.OVTM2, Attendance.WeekDay, Attendance.Holiday, Attendance.LateMin FROM Attendance INNER JOIN Employee ON Attendance.EName = Employee.Name WHERE Employee.Name Like '*' AND Employee.Dept Like '*' AND Employee.Division Like '*' AND Attendance.ADate Between #09/01/2011# And #09/16/2011# AND Employee.Attd=-1 ORDER BY Employee.Name, Attendance.ADate", cnn)

Dim AttdDataAdapter As New OleDb.OleDbDataAdapter(strSQL)
Dim AttdDataTable As New DataTable("Attendance")

AttdDataAdapter.Fill(AttdDataTable)
DataGridView1.DataSource = AttdDataTable
cnn.Close()
0
Comment
Question by:Patrick3388
  • 3
  • 2
6 Comments
 
LVL 40
ID: 36553107
First of all, those Like "*" are useless. That means everything. If you want everything, no need to specify the field in the WHERE clause.

When you say "it never gets working on VB2010", what do you mean? Do you get an error message? Is the Fill returning nothing?

What is the code after the table is filled? Maybe your way of accessing the DataTable is wrong.

Is what you give us the exact same coding that you use in VB? The dates are hardcoded in what you show us. This is very rare in a SQL command of this type. The dates usually comes from somewhere else and are concatenated into the SQL. Lot of problems arise at that point.

Are you working in 64-bits? There are issues between 64-bits applications and Access 2003 databases.
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 1000 total points
ID: 36559743
When you use ADO and/or ADO.net, * should be replaced with % and the # around dates should be replaced with '
0
 
LVL 40
ID: 36560969
@Éric

He is using Access. He should use the Access syntax since the String will be sent straight to Access.
0
Industry Leaders: 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 70

Expert Comment

by:Éric Moreau
ID: 36560989
I am sure not. ADO is an extra layer with an engine that affects the queries
0
 
LVL 40
ID: 36561074
The engine affects the queries when you use tools such as a CommandBuilder. SQL is always passed straight. There is no treatment done, because different applications use slightly different implementations. That is one of the many reasons stored procedures are preferred. There is a better chance that they will still work if you ever move the data from one database to another.

Passing dates between ' ' is never a good thing anyway, because your are then dependant on the culture. # # are better because you have a standard format (with Microsoft databases as least) of month-day-year.
0
 

Author Closing Comment

by:Patrick3388
ID: 36579023
Thanks a lot !
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month20 days, 14 hours left to enroll

864 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