5

I'm trying to create an excel program that can get data from sheet1 to sheet2 within the same file using VBA. But when I declared the ADODB, it does not appear in the drop down list. And when I try to run sub I get the 'user defined type not defined' error. Can anyone please share with me any fixes?

The code is as below:

Sub testsql()

'declare variable
Dim objMyConn As ADODB.Connection
Dim objMyCmd As ADODB.Command
Dim objMyRecordSet As ADODB.Recordset

Set objMyConn = New ADODB.Connection
Set objMyCmd = New ADODB.Command
Set objMyRecordSet = New ADODB.Recordset

'open connection
objMyConn.connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & wbWorkBook & ";Extended Properties=Excel 8.0;"
objMyConn.Open

'set and execute command
Set objMyCmd.activeconnection = objMyConn
objMyCmd.CommandText = "select top 10000 [Die No], Description from DieMaintenanceEntry"
objMyCmd.CommandType = adcmdtext


'open recordset
Set objMyRecordSet.Source = objMyCmd
objMyRecordSet.Open

'copy data to excel
ActiveWorkbook.Sheets("Display-Die Maintenance Summary").ActiveSheet.Range("A5").CopyFromRecordset (objMyRecordSet)

End Sub
10
  • did you reference the Activex library? Commented May 30, 2017 at 5:40
  • 1
    Tools-> References -> Microsoft ActiveX Data object 2.0 library check the box click ok Commented May 30, 2017 at 5:44
  • 1
    If you're just copying data from one sheet to another, inside the same Excel session, and you're not applying sorting or filtering, then all of this ADO business is probably overkill. Commented May 30, 2017 at 6:08
  • 1
    @SivaprasathV You should use the 6.x version (Windows Vista and later). 2.8 is for use with Windows XP and 2.0-2.7 are unsupported. Commented May 30, 2017 at 6:11
  • 1
    Thanks @ThunderFrame, I was usually going with 2.8 library without knowing the reason and thought that was the latest library. Since the code didnt have any version reference i thought he can go with the lowest version and thought that the later versions had backward compatability. Commented May 30, 2017 at 6:14

1 Answer 1

5

You can solve this in two ways:

  1. Early Binding (as hinted by your code)
    What you need to do is reference the correct Microsoft ActiveX Data Object. For my version, it is 6.1.

    References Library

  2. Using Late Binding (No need to reference library)

    Dim objMyConn As Object '/* Declare object type variable */
    Dim objMyCmd As Object
    Dim objMyRecordset As Object
    
    '/* Set using create object */
    Set objMyConn = CreateObject("ADODB.Connection")
    Set objMyCmd = CreateObject("ADODB.Command")
    Set objMyRecordset = CreateObject("ADODB.Recordset")
    

As for which to use, I can only give suggestion. During development, use Early Binding for you to take advantage of Intellisense. At deployment, change to Late Binding to overcome version compatibility issues.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the input. But now after changing the code I get a new error.
"objMyConn.Open" what needs to be changed?
@hjh93 That is already a new error and is out of scope on my answer :). Just kidding (but I'm serious), anyways, you need to check your connection string. Try replacing wbWorkbook with ThisWorkbook.FullName
@L42, is the msjet4.0 dll available in windows 10 by default?
@SivaprasathV Is that related to this post or you're asking a different question. Btw, it should probably work (no way to check, I'm on Win7) but you should use ACE OLEDB instead since it superceded JET.
|

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.