In VBA it is very simple to define an array, read a recordeset with ADO (a simple accdb table) and then populate the array with the info from the recordset. Say I have a table with 3 columns, A is dates, B is people names and C is the number of steps each person made during a day. Because we deal with numbers and strings, it's simple to just define a variant array:
Dim RS_CON As Object, RS_DATA As Object
Dim connString as String, queryString as string
Dim OutputArr() as variant
Set RS_CON = CreateObject("ADODB.Connection")
Set RS_DATA = CreateObject("ADODB.Recordset")
RS_CON.Open connString
queryString="SELECT * FROM [TableName]"
RS_DATA.Open queryString, RS_CON, adOpenStatic, adLockOptimistic, adCmdText
OutputArr =RS_DATA.GetRows(RS_DATA.RecordCount)
The resulting array has 3 columns and all the rows of table, without even specifying the dimensions of the array. In addition, the array holds various data types. But who do I do the exact same thing in VB.NET?
RS_CON = New OleDbConnection With {
.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" &
sourceFld & dbFileName & "; Persist Security Info=False;")
}
Try
dtSet = New DataSet
dtTables = dtSet.Tables
queryString = "SELECT * FROM [TableName];"
RS_CON.Open() '---> Open Connection To The DataBase File.
dtAdapt = New OleDbDataAdapter(queryString, RS_CON)
dtAdapt.Fill(dtSet)
Dim Y() As Array '---> DOES NOT WORK!!
RS_CON.Close()
Thank you for any hints/clues/guidance. Stefan
You can use the
DataRow.ItemArrayproperty to get your row as an array.Variants don't exist in VB.NET, useObjectinstead.I'd also recommend filling a
DataTableinstead of aDataSet.(NOTE: for MS Access you will need to change to ODBC objects, the code below is for Sql Server)