Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

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

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
JoeWaynick
Asked:
JoeWaynick
  • 4
  • 2
  • 2
2 Solutions
 
leclairmCommented:
...
  Set rs = dbHist.OpenRecordset("SELECT *
        FROM tblHistoricalData
        WHERE Symbol = '" & rsPctChg.Fields("Symbol") & "' AND DateFld >= #" & rsPctChg.Fields("DateFld") & "#
        ORDER BY DateFld, Symbol ", 2)
...


0
 
JoeWaynickAuthor Commented:
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
 
leclairmCommented:
You're welcome.

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

 
leonstrykerCommented:
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
 
JoeWaynickAuthor Commented:
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
 
JoeWaynickAuthor Commented:
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
 
leonstrykerCommented:
>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
 
JoeWaynickAuthor Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now