By the time I got to it, yu had it sorted. Well done.
There are several conventions about SQL statements that are best observed:
1. All SQL statements must be terminated by ; i.e. semi-colon. ORACLE's
SQL*PLUS requires it but Microsoft is not tto fussy. As you learn more ADO,
you will learn that some PROVIDERS can return multiple recordsets in the same
SDODB.Recordset object: in that context, ; is mandatoty.
2. strings embedded within SQL statements must be enclosed within single
quotes: Microsoft accepts double quotes most others do not. Literals such as
O'Shea must have the single quoted replicated ie. O''Shea
3. Always name calculated fields i.e Select SALARY *1.10 FROM EMPLOYEE does
not have a named colum (the driver assigns a name automatically) whereas
SELECT SALARY * 1.10 AS NEWSalary does.
4. All SQL's composed in UPPERCASE.
5. Different drivers/providers use other symbols for wrapping date literals
etc.
And so on...
The conventions come to matter a great deal when an application supports
multiple databases and the developer has to cater for variations in SQL
dialects; just an example, ORACLE strings are case sentitive whereas SQL
Server is not, by default.
Good luck with your future exploits with SQL.
Mr B said:
Well, I finally got it to work to return the value I was needing, using the
correct sql statement. The syntax is much different when used through this
ODBC connection.
Here is the statement that actually works:
strSql = "SELECT Max(PYEAR) AS MaxOfPYEAR FROM BPCSF.GPM " _
& "WHERE POPNCL = 'Y' and PAROFF <'21'"
Notice the single quotes.
Thanks again for all the help you provided.
Mr B
:
Could you take a look at the follow sql statment? When I use the simple sql
statment that I posted, the recordset is returned, but when I attempt to use
the following statement, it fails:
strSql = "SELECT Max(PYEAR) AS MaxOfPYEAR FROM [BPCSF.GPM] " _
& "WHERE (((POPNCL)=""Y"") AND ((PAROFF)<21))"
This statement works from Access. I have made the corrections to the table
name.
Mr B
:
Your connection string does not specify a Password: Isn't one needed?
:
Is BPCSF_GPM the table/query name? or is it BPCSF GPM?
You might try enclosing the table name in [] i.e. try the following:
select * from [BPCSF_GPM]
select * from [BPCSF GPM]
and:
select * from [BPCSF_GPM$]
select * from [BPCSF GPM$]
I looks like the table name is incorrect somehow. If you know Access, try:
File | Get External Data | Import Tables
From the dialogue, drop the Files of Type box and select ODBC DataSources(),
specify your source and your table. (gives you an opportunity to see all the
available tables/queries).
:
First, I want to thank you for your response. I do need help. I have been
able to accomplish this from Access with no problems, but for some reason I
have a mental block on this one.
I now have the following:
Dim strConn As String
Dim strSql As String
Dim myconn As ADODB.Connection
Dim rs As ADODB.Recordset
strConn = "Provider=MSDASQL.1; " _
& "Persist Security Info=False; " _
& "User ID=ACCTING; " _
& "Data Source=ACCOUNTING; " _
& "Initial Catalog=S408"
Set rs = CreateObject("ADODB.Recordset")
strSql = "SELECT * FROM BPCSF_GPM"
rs.Open strSql, strConn
Dim varTest
varTest = rs.Fields("PYEAR").Value
rs.Close
I have tested the connection string though the ODBC Admin and it reports the
the connection is good and successful.
When I step through the code and get to:
rs.Open strSql, strConn
I get the: "Run-time error '-2147217911 (80040e09)':
Automation error
Nothing I have tried has let me actually return a recordset. Is there
anything wrong with the statement being assigned to the "strSql" variable?
Mr B
:
In this scenario, you do not need a connection Object, just a Recordset Object:
Set RS = CreateObject("ADODB.Recordset")
strConn = "Provider=MSDASQL.1; " _
& "Persist Security Info=False; " _
& "User ID=ACCTING; " _
& "Data Source=ACCOUNTING; " _
& "Initial Catalog=S408"
strSql = "SELECT * FROM BPCSF_GPM"
RS.Open strSql,StrConn
When you've finished with RS,
RS.Close
Set RS = Nothing.