简体   繁体   中英

Extracting Values from Database into Array - Excel-VBA

I'm connected to an oracle database that basically outputs data into a sheet using an SQL statement I give it:

This is the code that copies it into a spreadsheet:

 Set oRsOracle = New ADODB.Recordset
    With oRsOracle
        .ActiveConnection = oConOracle
        .Open "SELECT CalcGrp_Name FROM Calc_Group"
        Sheets(2).Range("A1").CopyFromRecordset oRsOracle
        .Close
    End With
    Set oRsOracle = Nothing

    oConOracle.Close
    Set oConOracle = Nothing

Basically instead of the line Sheets(2).Range("A1").CopyFromRecordset oRsOracle is there a way to store these values into an Array or any data struct VB has to offer, I basically want to use these values and randomly populate them into another a data-test generation file

You can use arrays or Variants for example:

Dim dat as Variant
dat = Array(record1, record2, record3)

In a loop you can set individual records:

For i = 1 to 10
  dat(i) = 'your record
next

In your code try this:

Dim dat as Variant

With oRsOracle
    .ActiveConnection = oConOracle
    .Open "SELECT CalcGrp_Name FROM Calc_Group"
    dat = oRsOracle.GetRows
    .Close
End With

To print results:

Dim i As Long

For i = LBound(dat) to UBound(dat)
  Debug.Print dat(i)
Next

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