?
Solved

How to fetch the data from SQLSERVER in an array in VBA EXCEL instead to display it in worksheet

Posted on 2014-03-26
6
Medium Priority
?
812 Views
Last Modified: 2014-03-27
Hi,



My question is simple for EE used to EXCEL VBA
I want to get the data from SQLSERVER in an array instead to display
it in the worksheet
My code works to select the data and display it in the EXCEL worksheet
it is done via this command

 ThisWorkbook.Sheets("my_sheet").Range("D7").CopyFromRecordset rst

Open in new window



Can Someone tell me how to fetch the rst in an array inside the macro VBA please?

Thanks Dave
 Sub load_users()


    Dim cnn As ADODB.Connection

    Dim connectionString As String   Dim rst1 As ADODB.Recordset
    Dim rst2 As ADODB.Recordset
 
   Dim strSQL As String
     
    Dim User_ID As String
    Dim User_PWD As String
    Dim Initial_Catalog As String 'Nom de la base de données
    Dim Workstation_ID As String
    Dim Data_Source As String
    
    Application.ScreenUpdating = False
    
    On Error GoTo Verif_Connexion
    User_ID = "my login " '"SYGES"   
    User_PWD = "my password" ' "SYGES2014"  
    Initial_Catalog = "DATABASE"  
    Data_Source = "url"  
    Workstation_ID = "url"  '
      
    Set cnn = New ADODB.Connection
    connectionString = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=" & User_ID & ";PWD=" & User_PWD & ";Initial Catalog=" & Initial_Catalog & ";Data Source=" & Data_Source & ";Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=" & Workstation_ID & ";Use Encryption for Data=False;Tag with column collation when possible=False;"
    
    ThisWorkbook.Sheets("my_sheet").Range("A2:AZ65536").Value = ""
    
    strSQL = "select login, password from MYTABLE  "
   
    cnn.Open connectionString

    Set rst = cnn.Execute(strSQL)
    

    ThisWorkbook.Sheets("my_sheet").Activate


    ' HERE IS MY QUESTION
    'This put the result in the worksheet
    'I would like to have it in an array inside the code instead to displa it in the worksheet

    ThisWorkbook.Sheets("my_sheet").Range("D7").CopyFromRecordset rst
    

     
    rst.Close
    cnn.Close
    Set rst = Nothing
    Set cnn = Nothing
     
end sub

Open in new window

0
Comment
Question by:DavidInLove
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 53

Accepted Solution

by:
Rgonzo1971 earned 1200 total points
ID: 39955521
Hi,

pls try

arrRecordArray = rst.GetRows

Open in new window

Regards
0
 

Author Comment

by:DavidInLove
ID: 39955608
Hi,


I've tried to code something but it doesn't work.


' I declare the array as Variant
  Dim arrRecordArray As Variant


    Set rst = cnn.Execute(strSQL)
    
 arrRecordArray = rst.GetRows

MsgBox (arrRecordArray(0))

Open in new window



I just would like to do the same as the following working in PHP


    $res_query  = mssql_query ($req);			
		$row_donTot=mssql_num_rows($res_query);	
  	  while($tab=mssql_fetch_array($res_query)){		
  	   $niveau1[$cpt++]=$tab['niveau1']; 		
  	  }		

Open in new window


So if SO could give me the code to fetch

Thanks Dave
0
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 800 total points
ID: 39955628
The returned array will be two dimensional with fields as 'rows' and records as 'columns' so you have to supply both indices.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:DavidInLove
ID: 39955640
Still doesn't work:

Since I am not good at VBA maybe the way to fetch the data in array I did is wrong:




 Dim arrRecordArray As Variant
    Set rst = cnn.Execute(strSQL)
 arrRecordArray = rst.GetRows

MsgBox (arrRecordArray(1)(1))

Open in new window

0
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 800 total points
ID: 39955646
MsgBox arrRecordArray(1, 1)

Open in new window

is how you specify both indices.
0
 

Author Closing Comment

by:DavidInLove
ID: 39958344
Ok Thanks to you.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question