简体   繁体   中英

Having Excel function VBA connection to SQL issue - Compile Error

Hi I am trying to run a function in my excel table where a query is run through the function and data is generated but I have managed to fix the query which works when I use in SQL but when I run the function from Excel I get the following error:

Compile Error: User-defined type not defined.

error which I get in EXCEL VB:

在此处输入图片说明

The function I am running is:

Function Download_Standard_BOM(Query As String)
    Dim cnn As New ADODB.Connection
    Dim rst As New ADODB.Recordset
    Dim ConnectionString As String
    Dim StrQuery As String

    ConnectionString = "Driver={SQL Server};Server=STORESYSTEM 
;Database=STORE;Trusted_Connection=Yes;"

    cnn.Open ConnectionString
    StrQuery = Query 'Sheets("Tests Scenario").Range("J2").Offset(i, 0).Value
    cnn.CommandTimeout = 100
    rst.Open StrQuery, cnn, adOpenStatic, adLockOptimistic
    Download_Standard_BOM = rst("NAME")
End Function

and the SQL query I am running is:

SELECT TOP 1 NAME
FROM [SERVER].[dbo].[STORE]
WHERE SecurityE = 'SGX' AND
      Trans = ' 0' AND
      Type = '9' AND
      Status = '0' AND
      SubId = 'Reversal'

Make sure you include the reference to

Microsoft ActiveX Data Objects 2.1 Library

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM