Solved

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

Posted on 2006-07-14
8
294 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 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
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 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
 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

726 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