Here is a function that I use to fill an Array with values selected from an Access Table.
To use the function you will need:
Dim larayOfSelectedValues() As String = Nothing
Dim lclsFillTypeSecureUserComboBox As clsFillArrayOfSelectedValues
Dim lsqlCommand As String = "SELECT TypeUser " &
"FROM 10_10_TypeSecureUser " &
"Order By TypeUser"
Statments to use the Function:
' ---- Load the Secure User Combo Box with the Secure User Names from Secure User Name Table
lclsFillTypeSecureUserComboBox = New clsFillArrayOfSelectedValues
If lclsFillTypeSecureUserComboBox.FillArrayOfSelectedValues(iclsOLEDBConnectionString:=mclsOLEDBConnectionString,
istrSelectCommand:=lsqlCommand,
iarayOfSelectedValues:=larayOfSelectedValues) = vbFalse Then
Throw New Exception("An error occurred wil populating the Type user Combo Box")
End If
Me.cmboTypeUser.Items.AddRange(larayOfSelectedValues)
And Finally the Function:
Imports System.Data
Imports System.Data.OleDb
Imports Project_Reporting.Project_ReportingDataSetTableAdapters
''' <summary>
''' Selects all the Secure Users from the Secure User table [10_00_SecureUser]
''' and fills the Secure User Combo Box [cmboSecureUser] with the Secure User
''' Names
'''
''' </summary>
Public Class clsFillArrayOfSelectedValues
Private mstrClsTitle As String = "clsFillArrayOfSelectedValues"
'<PageBreak>
''' <summary>
''' Function Selects values from a Table in the Access Data base and fills and array with the
''' selected values [iarayOfSelectedValues] using the provided OLEDb connection string
''' [iclsOLEDBConnectionString] and provided SQL select command [istrSelectCommand].
''' *) Returns True if successful
''' *) Returns False if un-successful
'''
''' The Parameters:
''' *) iclsOLEDBConnectionString - The Database Connection string that is
''' created by the caller as:
''' "Provider=Microsoft.ACE.OLEDB.12.0; " +
''' "Data Source=" + .pstrDataBase +
''' "Project_Reporting.accdb;" +
''' "Jet OLEDB:Database Password=" + mstrPassword + ";" +
''' "Persist Security Info=False;"
''' *) istrSelectCommand - The SQL Select Statement that will select the values
''' to be inserted into the array of selected values
''' [iaraySelectedValue].
''' *) iarayOfSelectedValues - An array of the values selected from
''' the Table that can be inserted into a
''' Combo Box by the caller using:
''' [Me.cmboUseName.Items.AddRange(iarayOfSelectedValues)]
'''
''' </summary>
''' <param name="iclsOLEDBConnectionString"></param>
''' <param name="istrSelectCommand"></param>
''' <param name="iarayOfSelectedValues"></param>
''' <returns></returns>
Public Function FillArrayOfSelectedValues(ByVal iclsOLEDBConnectionString As clsOLEDBConnectionString,
ByVal istrSelectCommand As String,
ByRef iarayOfSelectedValues() As String) As Boolean
' ---- Local Data Base Classes
Dim lcmdOleDbCommand As New OleDbCommand
Dim lrdrOleDbDataReader As OleDbDataReader = Nothing
'<PageBreak>
Try
With lcmdOleDbCommand
.Connection = iclsOLEDBConnectionString.pOleDbConnection
.Connection.Open()
.CommandType = CommandType.Text
.CommandText = istrSelectCommand
End With
ReDim iarayOfSelectedValues(0)
lrdrOleDbDataReader = lcmdOleDbCommand.ExecuteReader
While lrdrOleDbDataReader.Read
Debug.Print("lrdrOleDbDataReader.Item(0) [" & lrdrOleDbDataReader.Item(0) & "]")
iarayOfSelectedValues(UBound(iarayOfSelectedValues)) = lrdrOleDbDataReader.Item(0)
ReDim Preserve iarayOfSelectedValues(UBound(iarayOfSelectedValues) + 1)
End While
lrdrOleDbDataReader.Close()
ReDim Preserve iarayOfSelectedValues(UBound(iarayOfSelectedValues) - 1)
Return vbTrue
Catch ex As Exception
Throw New Exception(mstrClsTitle & ".FillArrayOfSelectedValues " &
"An error occurred while filling the " &
"Type Secure User Combo Box " & vbCrLf &
"Err.Number [" & Err.Number & "] " & vbCrLf &
"Err.Description [" & Err.Description & "]")
Return False
Finally
lcmdOleDbCommand.Connection.Close()
lcmdOleDbCommand = Nothing
lrdrOleDbDataReader = Nothing
End Try
End Function
End Class