Solved

SQL Statement for VB2010

Posted on 2011-09-16
6
250 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 250 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

831 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