简体   繁体   中英

How can I resolve this error message “Compile Error: User-defined type not defined” in Microsoft VBA

I am new at Microsoft-VBA, and i trying to make Excel run a Stored Procedure in MSSQL Server and add the records to a table created for that purpose. But every time i try to run this to test my code, i get this error message. I have reviewed and implemented some solutions suggested on this forum, but i have not been able to make it work. For example, i have gone to activate the 'Microsoft ActiveX Data Objects Recordset 2.8 Library in Tools > References, but i still get the same error. Will be glad if anybody can review my code and tell me what i am doing wrong.

This image contains the code and error message


Sub My_Test_Report()

    Dim objConn As New ADODB.Connection
    Dim objCmd As New ADODB.Command
    Dim objParm1 As New ADODB.Parameter
    Dim objRs As New ADODB.Recordset

    objCmd.CommandText = "[Report Queries].[dbo].[My_Test_Report]"
    objCmd.CommandType = adCmdStoredProc
    Set objConn = GetNewConnection
    objCmd.ActiveConnection = objConn
    objCmd.Parameters.Refresh
    objCmd(1) = True
    Set objRs = objCmd.Execute

End Sub

I agree with ashleedawg3, you have 'Paremeter' vs 'Parameter'. When I try to compile, i get the same error with a highlight on that specific line:

在此处输入图片说明

The clue here is the compiler sees a type it DOESNT know about, so you get the "User Defined Type, not defined"

Try adjusting the spelling and see if that fixes it....

I think you're missing a second library. Go ToolsReferences and add both:

☑ Microsoft ActiveX Data Objects 6.1 Library

☑ Microsoft ActiveX Data Objects Recordset 6.0 Library

(Uncheck any lower versions of the same libraries.)

Note that when given the choice of different versions of the same library, it's generally best to go for the highest available version number.

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