2

i need some help here. I am trying to extract the data through SQL in excel having the join query but i am getting the debug error while running through below code..

recset.Open strSQL, con, adOpenDynamic, adLockOptimistic

Error - Runtime error -2147467259

Below is the code.....Please help :confused:

Sub Show_data()

    Dim con As ADODB.Connection
    Dim recset As ADODB.Recordset
    Dim ConnectionString As String
    Dim strSQL As String

    Set con = New ADODB.Connection
    Set recset = New ADODB.Recordset

''Check for the connectivity or connected to the xx network
    On Error GoTo errHandler

errHandler:
    If Err.Number = -2147467259 Then
        MsgBox "Please check for the xx connectivity ", vbExclamation + vbOKOnly
        Exit Sub
    End If


    ConnectionString = "Provider=MSDASQL;User ID=myuser;password= mypass;Data Source=mys"

    con.Open ConnectionString

    'Set and Excecute SQL Command'

        strSQL = "SELECT B.USER_NAME AS CREATED_BY, A.CREATION_DATE, C.USER_NAME, A.LAST_UPDATE_DATE, A.PFIZER_ITEMCODE, A.SYSTEM_ITEMCODE AS ORACLE_ITEM_CODE," & _
        "A.ITEM_DESCRIPTION, A.BATCH_NUMBER, A.MFR_CODE, A.MFR_DESC AS MFR_DESCRIPTION, TO_CHAR(A.MFR_DATE,'DD-MON-YYYY')As MFR_DATE, TO_CHAR(A.EXPIRY_DATE,'DD-MON-YYYY')As EXPIRY_DATE," & _
        "TO_CHAR(A.EFFECTIVE_FROM,'DD-MON-YYYY') AS EFFECTIVE_FROM," & _
        "A.EFFECTIVE_TO, A.EXCISE AS EXCISE_AMOUNT, A.EXCISE_RATE, A.P2S, A.P2R, A.MRP, A.STATE_CODE, A.STATE," & _
        "(CASE SUBSTR(A.SYSTEM_ITEMCODE,6,2) WHEN ('PI') THEN 'OIP' WHEN ('PF') THEN 'OPF' ELSE 'OWL' END )AS LEGAL_ENTITY" & _
        "FROM xxdhl_pf_batch_pricing A, fnd_user B, fnd_user c" & _
        "WHERE 1=1" & _
        "AND A.CREATED_BY = B.USER_ID" & _
        "AND A.LAST_UPDATED_BY = C.USER_ID"

        If (ActiveSheet.cmbLE.Text) <> "" Then
        strSQL = strSQL & " LEGAL_ENTITY='" & ActiveSheet.cmbLE.Text & "'"
        End If

        If (ActiveSheet.cmbProduct.Text) <> "" Then
            If (ActiveSheet.cmbLE.Text) <> "" Then
                strSQL = strSQL & " AND [ORACLE_ITEM_CODE]='" & ActiveSheet.cmbProduct.Text & "'"
            Else
                strSQL = strSQL & " [ORACLE_ITEM_CODE]='" & ActiveSheet.cmbProduct.Text & "'"
            End If
        End If

        If (ActiveSheet.txtBatch.Text) <> "" Then
            If (ActiveSheet.cmbLE.Text) <> "" Or (ActiveSheet.cmbProduct.Text) <> "" Then
                strSQL = strSQL & " AND [BATCH_NUMBER]='" & ActiveSheet.txtBatch.Text & "'"
            Else
                strSQL = strSQL & " [BATCH_NUMBER]='" & ActiveSheet.txtBatch.Text & "'"
            End If
        End If



    'Open Recordset

    Set recset.ActiveConnection = con
    recset.Open strSQL, con, adOpenDynamic, adLockOptimistic

    'Copy the data
    If recset.RecordCount > 0 Then
    Sheets("Sheet2").Range("A1").CopyFromRecordset recset
'    ActiveSheet.Range("A1").CopyFromRecordset recset
    Else
    MsgBox "No Data Available", vbExclamation + vbOKOnly
    Exit Sub
    End If

recset.Close
con.Close

End Sub

As per all you requirements i had print the strSQL

    SELECT B.USER_NAME AS CREATED_BY, A.CREATION_DATE, C.USER_NAME, 
    A.LAST_UPDATE_DATE, 
    A.PFIZER_ITEMCODE, A.SYSTEM_ITEMCODE AS ORACLE_ITEM_CODE, 
    A.ITEM_DESCRIPTION, A.BATCH_NUMBER, 
        A.MFR_CODE, A.MFR_DESC AS MFR_DESCRIPTION, 
        TO_CHAR(A.MFR_DATE,'DD-MON-YYYY')As MFR_DATE, 
    TO_CHAR(A.EXPIRY_DATE,'DD-MON-YYYY')As EXPIRY_DATE, 
        TO_CHAR(A.EFFECTIVE_FROM,'DD-MON-YYYY') AS EFFECTIVE_FROM, 
A.EFFECTIVE_TO, A.EXCISE AS EXCISE_AMOUNT, 
        A.EXCISE_RATE, A.P2S, 
A.P2R, A.MRP, A.STATE_CODE, A.STATE, 
        (CASE SUBSTR(A.SYSTEM_ITEMCODE,6,2) WHEN ('PI') THEN 'OIP' WHEN ('PF') THEN 'OPF' ELSE 'OWL' END )AS LEGAL_ENTITY 
            FROM xxdhl_pf_batch_pricing A, fnd_user B, fnd_user c 
WHERE 1=1 AND A.CREATED_BY = B.USER_ID AND A.LAST_UPDATED_BY = C.USER_ID  AND [A.LEGAL_ENTITY]='J0012' AND [A.SYSTEM_ITEMCODE]='1407_PI-J0012'
2
  • Can you print strSQL just before its run and attach to the question? Commented May 19, 2015 at 16:05
  • Should [A.LEGAL_ENTITY] be instead A.LEGAL_ENTITY or [A].[LEGAL_ENTITY] and so on. Commented May 19, 2015 at 23:32

4 Answers 4

1

Where you have code like

"WHERE 1=1" & _
"AND A.CREATED_BY = B.USER_ID" & _

It will be rendered

"WHERE 1=1AND A.CREATED_BY = B.USER_ID"

i.e. No space between the 1=1 and AND

Try putting a space before closing quotes on each line:

"WHERE 1=1 " & _
"AND A.CREATED_BY = B.USER_ID " & _
Sign up to request clarification or add additional context in comments.

15 Comments

which line of code creates the error? Can you print the strSQL variable? does the error still occur if using something like "SELECT * FROM Table"?
Does the error occur at this line: recset.Open strSQL, con, adOpenDynamic, adLockOptimistic? To print the value of strSQL use the line Debug.Print strSQL just before this line and the value of the variable gets written to the immediate window in the IDE.
Is there an 'AND' missing from just before 'LEGAL_ENTITY' in this sql AND A.LAST_UPDATED_BY = C.USER_ID LEGAL_ENTITY='OIP' AND
I think you need to comment out all of the WHERE code and then gradually uncomment bits and run the code to find the bit(s) that are causing the issues. It also looks like you are using the fnd_user table twice in the FROM clause - is that intentional?
As mentioned before, I think you need to comment out all of the WHERE code, run the code and then gradually uncomment bits and run the code to find the bit(s) that are causing the issues.
|
1

There is a limit to the number of characters that can be added to a string variable using the "& _" extender.

Chip's suggestion to Debug.Print the variable will tell you whether or not this query is viable before you try to execute it.

When I create my SQL for a query, I use a process like this to avoid truncation:

strSQL = "SELECT "
strSQL = strSQL & "Field1, Field2, Field3 "
strSQL = strSQL & "FROM MyTable "

Adding a space at the end of each line ensures that the issue Andy notes above doesn't happen.

Comments

1

Similiar to Andy Joiner suggestion, you're actually missing a space at the end of several places. For example

"(CASE SUBSTR(A.SYSTEM_ITEMCODE,6,2) WHEN ('PI') THEN 'OIP' WHEN ('PF') THEN 'OPF' ELSE 'OWL' END )AS LEGAL_ENTITY" & _
"FROM xxdhl_pf_batch_pricing A, fnd_user B, fnd_user c" & _

will look like:

"(CASE SUBSTR(A.SYSTEM_ITEMCODE,6,2) WHEN ('PI') THEN 'OIP' WHEN ('PF') THEN OPF' ELSE 'OWL' END )AS LEGAL_ENTITYFROM xxdhl_pf_batch_pricing A, fnd_user B, fnd_user c" & _

Thus eliminating your FROM clause.

5 Comments

Yes i tried with space but still the same error i m facing
As suggested previously we need to see the full query as it is being passed to SQL
Hi i have put the query from the print of debug sql ...pls see and let me know if any...please
Is LEGAL_ENTITY a field in your xxdhl_pf_batch_pricing table?
Then you can't use a.[LEGAL_ENTITY], you should use :(CASE SUBSTR(A.SYSTEM_ITEMCODE,6,2) WHEN ('PI') THEN 'OIP' WHEN ('PF') THEN 'OPF' ELSE 'OWL' END ) = ''J0012' instead. Which looks like it will never evaluate to true....
0

Finally i had solved the error.....actually in a coding where i have given parameter in which the quotes was given the problem...i.e.

Previously i was running the query with below code,

strSQL & " AND [ORACLE_ITEM_CODE]='"

Just beacause there was the quote

"["

...ws getting that error.

But after removing the same its working fine....

I would thanks to all of you who shared their knowledge to solve the error...it also helps me to gain my knowledge...

Thanks all

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.