Solved

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

Posted on 2006-07-14
8
288 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
  • 4
  • 2
  • 2
8 Comments
 
LVL 11

Assisted Solution

by:leclairm
leclairm earned 150 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
 
LVL 29

Accepted Solution

by:
leonstryker earned 50 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now