Trouble connecting to SQL Server

icariox

New member
I have an excel with macros that fills a range of cells with a recordset obtained from SQL Server database. This excel is working fine since a bunch of months ago.

Now, I want to test XLsPadlock.

Setting this code in VBA compiler:
Code:
public sql_dataBase As String = "myDB"                   
public sql_serverIP As String = "myServerAddress"
public sql_passwordSA As String =  "myPass"
public connStr As string = "Driver={SQL Server};Server=" & sql_serverIP & ";Database=" & sql_dataBase & ";UID=sa;Pwd=" & sql_passwordSA & ";"                                                          
                         
Sub EXECsql(sql_command As String)     
                           
    Dim cnn As Object               
    Set cnn = Application.CreateObject("ADODB.Connection")             
    Set cnn.ConnectionString = connStr                      
    set cnn.Command = sql_command 
    cnn.Open                    
    
    cnn.Execute
    
    cnn.Close
    Set cnn = Nothing
                                
End Sub      

'''''''''''''''''''''''''''''''''''''

Sub GETsql(sql_command As String)
    
    Dim cnn As Object
    Set cnn = Application.CreateObject("ADODB.Connection")
    Set cnn.ConnectionString = connStr       
    cnn.Open  
    
    Dim rSetDades As Object
    Set rSetDades = Application.CreateObject("ADODB.recordset")
    
    set rSetDades.ActiveConnection = cnn  
    Set rSetDades.Command = sql_command  
    rSetDades.Open        
 '   rSetDades.Close
    
    ''''''''''''''''''''''''''''''''''''''''''''''Set rSetDades = Nothing
    cnn.Close
    Set cnn = Nothing
    
                                                                           
End Sub
When trying to fill a cells range with the recordset obtained with:
Code:
Dim res As Object
    res = CallXLSPadlockVBA("GETsql", "SELECT [myField1], [myField2] FROM [myDB].[dbo].[myView]")
    Sheets("mySheet").Range(myStartingCell).CopyFromRecordset res
returns this error:


Trying several options, without success.

¿Am I using “res” object correctly?
 

Attachments

  • padLockErr.png
    padLockErr.png
    13.8 KB · Views: 1
Last edited:
Similar error:

padLockErr2

I’ve changed the compiled sub, to fill cells inside the function:
padLockErr3

Yes, I know “Application.rSetDades” is weird, but :
padLockErr4
 
Ok, solved.

Syntax in VBA Compiler is slightly different than Excel VBA IDE.
Once adapted, functions work well.

Thanks for your help !

Best regards.
 
Back
Top