PC Review


Reply
Thread Tools Rate Thread

ADO - Retrieve record with InStr criteria

 
 
Ron
Guest
Posts: n/a
 
      30th Jan 2009
I am trying to pull a record set from our financial software's database. I
only want GL accounts that are associated with a certain location. The GL
Accounts use the sturcture GLCODE-LOCATION. I am using the OLE DB driver to
access the data. Here is a snippet of my code

Const CONNECTION As String = "Provider=PervasiveOLEDB;Data
Source=DB;Location=SERVER;"
Dim CN As ADODB.connection
Dim rsQuery As ADODB.Recordset
Dim rsDetails As ADODB.Recordset
Dim qryDetails As String


'open a connection to the database
Set CN= New ADODB.connection
CN.Open CONNECTION


'Create Query to retrieve transaction details
qryDetails = "SELECT BATCHNBR, ENTRYNBR, ACCTID, TRANSNBR, FISCALPERD,
Sum(TRANSAMT) AS TRANSAMOUNT " & _
"FROM GLPJD " & _
"WHERE BATCHNBR >= '363361' " & _
"GROUP BY BATCHNBR, ENTRYNBR, ACCTID, TRANSNBR, FISCALPERD
" & _
"HAVING InStr(ACCTID, 'FR') <> 0 " & _
"ORDER BY BATCHNBR ASC;"
'"HAVING InStr(ACCTID, 'FR') <> 0 "

Set rsDetails = New ADODB.Recordset
Set rsDetails.ActiveConnection = CN
rsDetails.Source = qryDetails
rsDetails.LockType = adLockReadOnly
rsDetails.CursorType = adOpenKeyset
rsDetails.Open

I am unable to upen the recordset. I get the following error:

-2147467259 -->[LNA][ODBC Engine Interface] Error in expression:
InStr(ACCTID, 'FR').

I have also tried "HAVING CharIndex('FR', ACCTID) <> 0 " and that doesn't
work either. anyone have a solution to my error?

Thanks in advance.
 
Reply With Quote
 
 
 
 
Douglas J. Steele
Guest
Posts: n/a
 
      30th Jan 2009
You can't use VBA functions when you're running queries against other DBMS.

If you're looking for those accounts that have FR anywhere in them, use LIKE
'%FR%'

Incidentally, you'd be far better off putting that in the WHERE clause
rather than in the HAVING clause. WHERE clauses are evaluated before the
aggregation is done, HAVING clauses are evaluated after. That means your
query may be doing far more accumulation than necessary.

qryDetails = "SELECT BATCHNBR, ENTRYNBR, ACCTID, TRANSNBR, " & _
"FISCALPERD, Sum(TRANSAMT) AS TRANSAMOUNT " & _
"FROM GLPJD " & _
"WHERE BATCHNBR >= '363361' " & _
"AND ACCTID LIKE '%FR%' " & _
"GROUP BY BATCHNBR, ENTRYNBR, ACCTID, TRANSNBR, FISCALPERD " & _
"ORDER BY BATCHNBR ASC;"


--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please)


"Ron" <(E-Mail Removed)> wrote in message
news:F6FAE141-523A-4490-B9CF-(E-Mail Removed)...
>I am trying to pull a record set from our financial software's database. I
> only want GL accounts that are associated with a certain location. The GL
> Accounts use the sturcture GLCODE-LOCATION. I am using the OLE DB driver
> to
> access the data. Here is a snippet of my code
>
> Const CONNECTION As String = "Provider=PervasiveOLEDB;Data
> Source=DB;Location=SERVER;"
> Dim CN As ADODB.connection
> Dim rsQuery As ADODB.Recordset
> Dim rsDetails As ADODB.Recordset
> Dim qryDetails As String
>
>
> 'open a connection to the database
> Set CN= New ADODB.connection
> CN.Open CONNECTION
>
>
> 'Create Query to retrieve transaction details
> qryDetails = "SELECT BATCHNBR, ENTRYNBR, ACCTID, TRANSNBR, FISCALPERD,
> Sum(TRANSAMT) AS TRANSAMOUNT " & _
> "FROM GLPJD " & _
> "WHERE BATCHNBR >= '363361' " & _
> "GROUP BY BATCHNBR, ENTRYNBR, ACCTID, TRANSNBR, FISCALPERD
> " & _
> "HAVING InStr(ACCTID, 'FR') <> 0 " & _
> "ORDER BY BATCHNBR ASC;"
> '"HAVING InStr(ACCTID, 'FR') <> 0 "
>
> Set rsDetails = New ADODB.Recordset
> Set rsDetails.ActiveConnection = CN
> rsDetails.Source = qryDetails
> rsDetails.LockType = adLockReadOnly
> rsDetails.CursorType = adOpenKeyset
> rsDetails.Open
>
> I am unable to upen the recordset. I get the following error:
>
> -2147467259 -->[LNA][ODBC Engine Interface] Error in expression:
> InStr(ACCTID, 'FR').
>
> I have also tried "HAVING CharIndex('FR', ACCTID) <> 0 " and that
> doesn't
> work either. anyone have a solution to my error?
>
> Thanks in advance.



 
Reply With Quote
 
Jim Burke in Novi
Guest
Posts: n/a
 
      31st Jan 2009
What Doug said. As far as I know, you only use HAVING if you need criteria
based on a 'summarized' field, e.g. HAVING SUM(fieldX) > 100, otherwise it
goes in the WHERE clause.

InStr isn't what you'd use here, but just as a note, the first argument in
InStr is the starting position, which you didn't have. I've used InStr in
queries before where I had something like , e.g. IIf(Instr(1,field1,"x"),1,2)
AS MyField, which would create a field called MyField and assign it a value
of 1 if "x" is anywhere in the field, otherwise it would assign a value of 2.

"Ron" wrote:

> I am trying to pull a record set from our financial software's database. I
> only want GL accounts that are associated with a certain location. The GL
> Accounts use the sturcture GLCODE-LOCATION. I am using the OLE DB driver to
> access the data. Here is a snippet of my code
>
> Const CONNECTION As String = "Provider=PervasiveOLEDB;Data
> Source=DB;Location=SERVER;"
> Dim CN As ADODB.connection
> Dim rsQuery As ADODB.Recordset
> Dim rsDetails As ADODB.Recordset
> Dim qryDetails As String
>
>
> 'open a connection to the database
> Set CN= New ADODB.connection
> CN.Open CONNECTION
>
>
> 'Create Query to retrieve transaction details
> qryDetails = "SELECT BATCHNBR, ENTRYNBR, ACCTID, TRANSNBR, FISCALPERD,
> Sum(TRANSAMT) AS TRANSAMOUNT " & _
> "FROM GLPJD " & _
> "WHERE BATCHNBR >= '363361' " & _
> "GROUP BY BATCHNBR, ENTRYNBR, ACCTID, TRANSNBR, FISCALPERD
> " & _
> "HAVING InStr(ACCTID, 'FR') <> 0 " & _
> "ORDER BY BATCHNBR ASC;"
> '"HAVING InStr(ACCTID, 'FR') <> 0 "
>
> Set rsDetails = New ADODB.Recordset
> Set rsDetails.ActiveConnection = CN
> rsDetails.Source = qryDetails
> rsDetails.LockType = adLockReadOnly
> rsDetails.CursorType = adOpenKeyset
> rsDetails.Open
>
> I am unable to upen the recordset. I get the following error:
>
> -2147467259 -->[LNA][ODBC Engine Interface] Error in expression:
> InStr(ACCTID, 'FR').
>
> I have also tried "HAVING CharIndex('FR', ACCTID) <> 0 " and that doesn't
> work either. anyone have a solution to my error?
>
> Thanks in advance.

 
Reply With Quote
 
John W. Vinson
Guest
Posts: n/a
 
      1st Feb 2009
On Sat, 31 Jan 2009 15:44:01 -0800, Jim Burke in Novi
<(E-Mail Removed)> wrote:

>InStr isn't what you'd use here, but just as a note, the first argument in
>InStr is the starting position, which you didn't have.


And that parameter *is optional*. I'm not sure how VBA figures it out but

InStr("abcdeabcde", "e")

correctly returns 5, you don't need the 1.
--

John W. Vinson [MVP]

 
Reply With Quote
 
Jim Burke in Novi
Guest
Posts: n/a
 
      1st Feb 2009
I didn't know that (obviously). Makes sense to default it to 1, since that's
usually what you want.

"John W. Vinson" wrote:

> On Sat, 31 Jan 2009 15:44:01 -0800, Jim Burke in Novi
> <(E-Mail Removed)> wrote:
>
> >InStr isn't what you'd use here, but just as a note, the first argument in
> >InStr is the starting position, which you didn't have.

>
> And that parameter *is optional*. I'm not sure how VBA figures it out but
>
> InStr("abcdeabcde", "e")
>
> correctly returns 5, you don't need the 1.
> --
>
> John W. Vinson [MVP]
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
InStr Help - Multiple Criteria TysonE Microsoft Excel Programming 3 3rd Aug 2009 06:34 PM
Problem with criteria and trouble with instr() =?Utf-8?B?S2FtaXRzdWtlbnU=?= Microsoft Access Queries 6 9th Aug 2007 02:02 AM
Retrieve next record based on current record insideout786@gmail.com Microsoft Access Forms 2 23rd Jan 2007 08:58 PM
Retrieve Record based on criteria in form from table insideout786@gmail.com Microsoft Access Forms 3 23rd Jan 2007 05:31 PM
Lookup if record exists and retrieve another field from record =?Utf-8?B?UmljaDEyMzQ=?= Microsoft Access Form Coding 4 5th Oct 2005 04:00 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:37 AM.