Solved

SQL as recordset in VB

Posted on 1998-09-15
8
421 Views
Last Modified: 2012-08-14
I am using a SQL to query and Access DB.  I want this data to be the chart Data for MSCHART.  What is wrong with this?

Dim WS As DAO.Workspace
Dim DB As DAO.Database
 ' Initialize a default workspace
Set WS = CreateWorkspace("TestWS", "Admin", "", dbUseJet)
Set DB = WS.OpenDatabase("c:\windows\desktop\wkformula.mdb")
Set wb = DB.OpenRecordset("CMLtable")
With wb
Dim ol As Recordset
Dim STRSQL
STRSQL = "SELECT OSBAL FROM CMLTABLE WHERE YEAR='1998'"
Set ol = DB.OpenRecordset(STRSQL)
Me!MSChart1.ChartData = ol

Thanks
0
Comment
Question by:dhill10358
8 Comments
 
LVL 14

Expert Comment

by:waty
ID: 1434985
Does your field YEAR a string or date field?
0
 
LVL 18

Expert Comment

by:deighton
ID: 1434986
What is the error message you are getting?
0
 

Author Comment

by:dhill10358
ID: 1434987
Hi,

It gives me a runtime error.

The Year is actually in the Access table, but it is a text field. I know the sql statement is ok, but I get "Bad Function Argument"  Runtime error 1101.

Any Suggestions?
0
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.

 
LVL 9

Expert Comment

by:cymbolic
ID: 1434988
I've commented out what I don't think you need, and modified it to what should work:

'Set wb = DB.OpenRecordset("CMLtable")
'With wb
Dim ol As Recordset
Dim STRSQL
STRSQL = "SELECT OSBAL FROM CMLTABLE WHERE YEAR='1998'"
Set ol = DB.OpenRecordset(STRSQL)
if not ol.eof then
 Me!MSChart1.ChartData = ol(0) 'or ol("OSBAL") or ol!OSBAL
endif
ol.close
etc...
0
 

Author Comment

by:dhill10358
ID: 1434989
Thanks for you help, but I get the exact same error message, the only difference is when I try to debug, the OL(0) or ol("osbal") has a value now.  But it has the same error msg.

Thanks
0
 

Expert Comment

by:balrogzed
ID: 1434990
I think you may need to change your sql statement to use double quotes not single quotes. ie change
STRSQL = "SELECT OSBAL FROM CMLTABLE WHERE YEAR='1998'"
to
STRSQL = "SELECT OSBAL FROM CMLTABLE WHERE YEAR=""1998"""
0
 

Author Comment

by:dhill10358
ID: 1434991
Thanks, but that is not the problem.  When I am debugging I can see the results of the SQL. I use the exact same statement in to append a recordset to excel, and it works.

Thanks
0
 
LVL 3

Accepted Solution

by:
TheAnswerMan earned 80 total points
ID: 1434992
IS that a forward only Recordset?
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

696 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