Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Syntax error on "Select . . . Where" statement in VB6, Access 2000, DAO environment

Posted on 2006-07-14
8
Medium Priority
?
296 Views
Last Modified: 2013-12-25
Hello Experts;

I'm attempting to create two recordsets for use in processing my table with the following code:

    Set dbHist = OpenDatabase("c:\Documents and Settings\Joe\Data Files\MS-Visual Basic\SMA\HistoricalData.mdb")

    Set rsPctChg = dbHist.OpenRecordset("SELECT TOP 25 *
        FROM tblHistoricalData WHERE DateFld = #" & dteDateVar & "# ORDER BY PercentChange Desc", 2)

    Set rs = dbHist.OpenRecordset("SELECT *
        FROM tblHistoricalData
        WHERE Symbol = rsPctChg.Fields("Symbol") AND DateFld >= #" & rsPctChg.Fields("DateFld") & "#
        ORDER BY DateFld, Symbol ", 2)

However, at runtime I get a syntax error on the "Set rs =" statement when I say WHERE Symbol = rsPctChg.Fields("Symbol"). If I remove the reference to Symbol, the SQL statement works. However, I want to filter out all symbols that aren't included in my rsPctChg recordset. Symbol is a field in my table. The rsPctChg recordset is set just prior to setting the rs recordset.

What am I doing wrong?

Joe
0
Comment
Question by:JoeWaynick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 11

Assisted Solution

by:leclairm
leclairm earned 600 total points
ID: 17110276
...
  Set rs = dbHist.OpenRecordset("SELECT *
        FROM tblHistoricalData
        WHERE Symbol = '" & rsPctChg.Fields("Symbol") & "' AND DateFld >= #" & rsPctChg.Fields("DateFld") & "#
        ORDER BY DateFld, Symbol ", 2)
...


0
 

Author Comment

by:JoeWaynick
ID: 17110494
Thanks leclairm! It works like a champ.

Now, as a new vb programmer, I totally spaced on figuring out the proper syntax on that select statement. Can you explain to me "why" my code didn't work? Also, can you refer me to any sources where I can get CLEAR explainations and examples of various ways to code SQL select statements? Thanks!

Joe
0
 
LVL 11

Expert Comment

by:leclairm
ID: 17110559
You're welcome.

Can you explain in more detail what you are trying to return with the above queries??
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 29

Accepted Solution

by:
leonstryker earned 200 total points
ID: 17110591
Get in the habit of assigning your SQL to a variable before executing it. You should also take a look at it in the debug window to see if it is valid:


strSQL = "SELECT *
        FROM tblHistoricalData
        WHERE Symbol = '" & rsPctChg.Fields("Symbol") & "' AND DateFld >= #" & rsPctChg.Fields("DateFld") & "#
        ORDER BY DateFld, Symbol "

'/ For debuging
Debug.Print strSQL

Set rs = dbHist.OpenRecordset(strSQL, 2)

Leon
0
 

Author Comment

by:JoeWaynick
ID: 17110699
Hi Leclairm;

I'm a new VB programmer and I'm writing an application to manipulate stock market data. I have an Access 2000 database that I process using VB6, referencing DAO. I want to declare two record sets and using one recordset as an input stream, cycle through the second recordset and process the data.
0
 

Author Comment

by:JoeWaynick
ID: 17110728
Hi Leon;

Thanks for the advice. I hate to bother you with such easy questions, but can you explain why I should assign my SQL to a variable before executing it? I looked over your code and I'll try it later today. But a little background would help. Thanks in advance for your assistance!

Joe
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 17110762
>but can you explain why I should assign my SQL to a variable before executing it?

Sure, it makes it easy to find/read/debug.

Leon
0
 

Author Comment

by:JoeWaynick
ID: 17114193
Leon;

Thanks for responding, and I'll certainly follow your advice. I'll split the points with you and leclairm for helping me. Stay tuned because I have plenty of other issues with which I could use help.

Joe
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Suggested Courses

688 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