Solved

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

Posted on 2006-07-14
8
289 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Specify ip address in my connection string for sas 2 54
Windows 10 start screen issues 9 52
Sending a email via excel using vba 6 72
Problem to With line 4 43
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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

867 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

15 Experts available now in Live!

Get 1:1 Help Now