Why Cant I Populate an Array from ADO.Recordset?

%3CLINGO-SUB%20id%3D%22lingo-sub-1102505%22%20slang%3D%22en-US%22%3EWhy%20Cant%20I%20Populate%20an%20Array%20from%20ADO.Recordset%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1102505%22%20slang%3D%22en-US%22%3E%3CP%3EPerhaps%20this%20is%20obvious%2C%20however%20I%20seldom%20use%20arrays.%26nbsp%3B%20In%20fact%20I%20am%20struggling%20to%20recall%20last%20time%20I%20used%20this%20as%20my%20code%20usually%20doesn't%20need%20it.%26nbsp%3B%20However%20in%20this%20instance%20I%20think%20it%20is%20the%20best%20choice...Perhaps%20I%20am%20wrong%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20an%20SQL%20Server%20backend.%26nbsp%3B%20I%20am%20running%20a%20number%20of%20SP%20on%20this%20and%20using%20these%20with%20effectively%20access%20acting%20as%20a%20friendly%20form.%26nbsp%3B%20This%20is%20working%20well%20and%20has%20the%20entire%20app%20snappy%20and%20responding%20well.%26nbsp%3B%20Separate%20topic%20really.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20my%20tables%20I%20have%20a%20a%20table%20set%20up%20for%20Jobs.%26nbsp%3B%20We%20are%20a%20kind%20of%20maintenance%20contractor%20with%20construction%20and%20consulting%20all%20mixed%20in.%26nbsp%3B%20Bit%20unusual%20compared%20to%20larger%20companies.%26nbsp%3B%20Consequently%20jobs%20can%20be%20opened%20at%20different%20levels%20including%20Client%2C%20Site%2C%20Equipment%20and%20so%20on.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAs%20the%20jobs%20can%20be%20opened%20from%20different%20levels%20I%20am%20using%20a%20link%20table%20to%20hold%20the%20jobID%20and%20the%20relevant%20ID%20from%20the%20Client%20or%20Site%20or%20Equipment%20down%20to%20level%205.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20then%20was%20working%20on%20some%20air%20code%20and%20have%20just%20started%20to%20work%20on%20the%20long%20hand%20version%20only%20in%20Access%20at%20the%20moment%2C%20of%20the%20code.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWHat%20I%20am%20doing%20is%20feeding%20a%20SQL%20Statement%20with%20the%20relevant%20Table%20number%20and%20RecordID%20to%20return%20the%20SQL%20correctly.%26nbsp%3B%20An%20example%20being%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ESELECT%20*%20FROM%20tblJobLinkTable%20WHERE%20LinkTable%20%3D%204%20and%20LinkID%20%3D%2031%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20fine%20and%20correct%20for%20what%20I%20am%20asking%20for.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20then%20have%20a%20module%20level%20array%20set%20to%20hold%20the%20JobID%20returned%26nbsp%3B%20being%20mlngJob%20and%20I%20have%20scoped%20this%20to%20an%20array%20of%2030%2C000%20as%20I%20am%20thinking%20its%20highly%20unlikely%20any%20client%20including%20all%20their%20sites%20and%20items%20of%20equipment%20would%20ever%20have%2030%2C000%20jobs%20open%20at%20any%20point%20of%20time.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20then%20added%20the%20code%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EPrivate%20Sub%20popArr(strSQL%20As%20String)%0ADim%20rst%20As%20New%20ADODB.Recordset%0ADim%20lngJob%20As%20Long%0A%20%20%20%20With%20rst%20%20%20%20%0A%20%20%20%20%20%20%20%20.Open%20strSQL%2C%20CurrentProject.Connection%2C%20adOpenKeyset%2C%20adLockPessimistic%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Do%20Until%20.EOF%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20'Use%20get%20rows%20to%20pop%20array%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mlngJob%20%3D%20.Fields(%22job%22)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.MoveNext%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Loop%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20.Close%0A%20%20%20%20%20%20%20%20End%20With%0A%20%20%20%20%20%20%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20effectively%20I%20am%20asking%20the%20ADO%20to%20populate%20the%20array%20with%20the%20returned%20jobID.%26nbsp%3B%20As%20The%20next%20step%20I%20need%20to%20build%20a%20SQL%20statement%20by%20walking%20the%20array%20and%20going%20something%20like%20mlngJob%20OR%20mlngJob%20OR%20mlngJob%20etc%20on%20the%20final%20query.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHowever%20I%20am%20getting%20loverly%20messages%20telling%20me%20I%20cant%20pop%20an%20array%20from%20a%20Recordset.%26nbsp%3B%20First%20why%20on%20earth%20not%3F%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20looked%20at%20the%20world%20wide%20confuser%20and%20found%20comments%20on%20using%20GetRows%20and%20I%20looked%20this%20up%20and%20tried%20it...but%20no.%26nbsp%3B%20It%20thinks%20Im%20ugly%20and%20my%20mum%20dresses%20me%20funny.%20%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20can%20think%20of%20about%206%20other%20ways%20to%20do%20this%2C%20however%20I%20would%20really%20like%20to%20use%20this%20concept.%26nbsp%3B%20It%20would%20suit%20the%20direction%20I%20am%20generally%20going%20and%20I%20think%20would%20be%20the%20best%20approach.%26nbsp%3B%20All%20I%20want%20to%20do%20is%20hold%20these%20job%20numbers%20returned%20in%20an%20Array%20so%20I%20can%20go%20back%20and%20get%20the%20data%20in%20a%20few%20more%20lines%20of%20code.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20there%20another%20approach%20that%20will%20work%20for%20this%3F%20%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1102505%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3E2016%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAccess%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAccess%20Web%20App%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1107121%22%20slang%3D%22en-US%22%3ERe%3A%20Why%20Cant%20I%20Populate%20an%20Array%20from%20ADO.Recordset%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1107121%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F417778%22%20target%3D%22_blank%22%3E%40dynamictiger%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20your%20case%20as%20been%20posted%20you%20are%20looping%20through%20the%20entire%20recordset%20so%20you%20can%20assign%20one%20value%20a%20time%20to%20the%20array.%20Then%20you%20should%20use%20an%20array%20index%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDim%20i%20as%20integer%3C%2FP%3E%3CP%3E...%3C%2FP%3E%3CP%3EDo%20while%20not%20rst.eof%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3Bi%20%3D%20i%20%2B%201%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3BmlngJob%5Bi%5D%20%3D%20.fields(%22job%22)%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3Brst.MoveNext%3C%2FP%3E%3CP%3ELoop%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnother%20solution%20-%20and%20I%20assume%20this%20is%20the%20solution%20you%20are%20looking%20for%20-%26nbsp%3B%20is%20assigning%20the%20recordset%20rows%20directly%20to%20an%20array%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E...%3C%2FP%3E%3CP%3EDim%20rst%20As%20Recordset%3CBR%20%2F%3EDim%20mstrArray%20As%20Variant%3CBR%20%2F%3E%3CBR%20%2F%3ESet%20rst%20%3D%20CurrentDb.OpenRecordset(%22yourSQLstatement%22)%3CBR%20%2F%3E%3CBR%20%2F%3EmstrArray%20%3D%20rst.GetRows()%3CBR%20%2F%3E%3CBR%20%2F%3ESet%20rst%20%3D%20Nothing%3C%2FP%3E%3CP%3E...%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Perhaps this is obvious, however I seldom use arrays.  In fact I am struggling to recall last time I used this as my code usually doesn't need it.  However in this instance I think it is the best choice...Perhaps I am wrong?

 

I have an SQL Server backend.  I am running a number of SP on this and using these with effectively access acting as a friendly form.  This is working well and has the entire app snappy and responding well.  Separate topic really.

 

In my tables I have a a table set up for Jobs.  We are a kind of maintenance contractor with construction and consulting all mixed in.  Bit unusual compared to larger companies.  Consequently jobs can be opened at different levels including Client, Site, Equipment and so on.

 

As the jobs can be opened from different levels I am using a link table to hold the jobID and the relevant ID from the Client or Site or Equipment down to level 5.

 

I then was working on some air code and have just started to work on the long hand version only in Access at the moment, of the code.

 

WHat I am doing is feeding a SQL Statement with the relevant Table number and RecordID to return the SQL correctly.  An example being:

 

SELECT * FROM tblJobLinkTable WHERE LinkTable = 4 and LinkID = 31

 

This is fine and correct for what I am asking for.

 

I then have a module level array set to hold the JobID returned  being mlngJob and I have scoped this to an array of 30,000 as I am thinking its highly unlikely any client including all their sites and items of equipment would ever have 30,000 jobs open at any point of time.

 

I have then added the code

 

Private Sub popArr(strSQL As String)
Dim rst As New ADODB.Recordset
Dim lngJob As Long
    With rst    
        .Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockPessimistic        
                        
                Do Until .EOF                
                    'Use get rows to pop array                    
                    mlngJob = .Fields("job")                
                    
                    .MoveNext                    
                Loop                
              .Close
        End With
      
End Sub

 

So effectively I am asking the ADO to populate the array with the returned jobID.  As The next step I need to build a SQL statement by walking the array and going something like mlngJob OR mlngJob OR mlngJob etc on the final query.

 

However I am getting loverly messages telling me I cant pop an array from a Recordset.  First why on earth not? 

 

I looked at the world wide confuser and found comments on using GetRows and I looked this up and tried it...but no.  It thinks Im ugly and my mum dresses me funny.  

 

I can think of about 6 other ways to do this, however I would really like to use this concept.  It would suit the direction I am generally going and I think would be the best approach.  All I want to do is hold these job numbers returned in an Array so I can go back and get the data in a few more lines of code.

 

Is there another approach that will work for this?  

 

Thanks

1 Reply

@dynamictiger 

 

In your case as been posted you are looping through the entire recordset so you can assign one value a time to the array. Then you should use an array index:

 

Dim i as integer

...

Do while not rst.eof

   i = i + 1

   mlngJob[i] = .fields("job")

   rst.MoveNext

Loop

 

Another solution - and I assume this is the solution you are looking for -  is assigning the recordset rows directly to an array

 

...

Dim rst As Recordset
Dim mstrArray As Variant

Set rst = CurrentDb.OpenRecordset("yourSQLstatement")

mstrArray = rst.GetRows()

Set rst = Nothing

...