0

I have 11 Access database files each having 20 items which needs to be stored in local variables as RejRsnStn(i)(Reason) where i stands for station number 1 to 11 & Reason stands for Reason 1 to 20.

I am reading this data in following way:

Public Sub ReadReasonCodes()
    'Station 1
    For Reason = 1 To 20
        DatafileStn = "E:\DATANetwork\DATAStation_1.accdb;Jet OLEDB:Database Password=xxxxxxxx"
        Dim ConnstringStn As String = provider & DatafileStn
        connstringwrkstn = ConnstringStn
        myConnection.ConnectionString = ConnstringStn
        myConnection.Open()

        str = "SELECT * FROM Table_Config WHERE StationNo = 1"
        cmd = New OleDbCommand(str, myConnection)
        dr = cmd.ExecuteReader

        While dr.Read()
            RejRsnStn1(Reason) = If(IsDBNull(dr("RejectionReason" & Reason)), "NA", dr("RejectionReason" & Reason))
        End While
        myConnection.Close()
    Next

    'Station 2
    For Reason = 1 To 20
        DatafileStn = "E:\DATANetwork\DATAStation_2.accdb;Jet OLEDB:Database Password=xxxxxxxx"
        Dim ConnstringStn As String = provider & DatafileStn
        connstringwrkstn = ConnstringStn
        myConnection.ConnectionString = ConnstringStn
        myConnection.Open()

        str = "SELECT * FROM Table_Config WHERE StationNo = 2"
        cmd = New OleDbCommand(str, myConnection)
        dr = cmd.ExecuteReader

        While dr.Read()
            RejRsnStn2(Reason) = If(IsDBNull(dr("RejectionReason" & Reason)), "NA", dr("RejectionReason" & Reason))
        End While
        myConnection.Close()
    Next

End Sub

Can you please guide me with a concise method.

Thanks in advance. Prashant.

3
  • 1
    Move all the connection stuff outside the loops. Commented Aug 31, 2017 at 9:02
  • it boggles me that you use a loop for the reason code, but utterly fail to use them for the station id. Commented Aug 31, 2017 at 9:05
  • I'm voting to close this question as off-topic because no, you can not haz teh codez. Commented Aug 31, 2017 at 9:06

1 Answer 1

1

I would do something like this:

First I would import System.Data.OleDb and then construct a class to contain all the codes:

Imports System.Data.OleDb
Public Class rCode
    Dim _ReasonCode As String = ""
    Public Property ReasonCode() As String
        Get
            Return _ReasonCode
        End Get
        Set(value As String)
        _ReasonCode = value
        End Set
    End Property
End Class

Public Class RCodes
    Public Property Station_1_Reasoncodes As New List(Of rCode)
    Public Property Station_2_Reasoncodes As New List(Of rCode)
    Public Property Station_3_Reasoncodes As New List(Of rCode)
    Public Property Station_4_Reasoncodes As New List(Of rCode)
    Public Property Station_5_Reasoncodes As New List(Of rCode)
    Public Property Station_6_Reasoncodes As New List(Of rCode)
    Public Property Station_7_Reasoncodes As New List(Of rCode)
    Public Property Station_8_Reasoncodes As New List(Of rCode)
    Public Property Station_9_Reasoncodes As New List(Of rCode)
    Public Property Station_10_Reasoncodes As New List(Of rCode)
    Public Property Station_11_Reasoncodes As New List(Of rCode)
End Class

Then I would initiate the class I just created:

public class form1
    Public ReasonCodes As RCodes = New RCodes

Then I would make a subroutine to get the twenty codes from a database:

public class form1
    Public ReasonCodes As RCodes = New RCodes

    Public Sub GetReasonCodes(station As String)
    Dim Provider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
    Dim myConnection As New OleDbConnection
    Dim DatafileStn As String = "E:\DATANetwork\DATAStation_" & station & ".accdb;Jet OLEDB:Database Password=xxxxxxxx"
    Dim ConnstringStn As String = Provider & DatafileStn
    myConnection.ConnectionString = ConnstringStn
    myConnection.Open()
    Dim cmd As New OleDbCommand("SELECT * FROM Table_Config WHERE StationNo = " & station, myConnection)
    Dim dr As OleDbDataReader = cmd.ExecuteReader
    Dim s As rCode = New rCode
    Dim s2 As New List(Of rCode)
    While dr.Read()
        For Reason = 1 To 20
            If IsDBNull(dr("RejectionReason" & CStr(Reason))) Then s.ReasonCode = "NA" Else s.ReasonCode = CStr(dr("RejectionReason" & CStr(Reason)))
            s2.Add(s)
        Next
    End While
    If Not dr.IsClosed Then dr.Close()
    myConnection.Close()
    dr = Nothing
    myConnection = Nothing
    Select Case station
        Case "1"
            ReasonCodes.Station_1_Reasoncodes = s2
        Case "2"
            ReasonCodes.Station_2_Reasoncodes = s2
        Case "3"
            ReasonCodes.Station_3_Reasoncodes = s2
        Case "4"
            ReasonCodes.Station_4_Reasoncodes = s2
        Case "5"
            ReasonCodes.Station_5_Reasoncodes = s2
        Case "6"
            ReasonCodes.Station_6_Reasoncodes = s2
        Case "7"
            ReasonCodes.Station_7_Reasoncodes = s2
        Case "8"
            ReasonCodes.Station_8_Reasoncodes = s2
        Case "9"
            ReasonCodes.Station_9_Reasoncodes = s2
        Case "10"
            ReasonCodes.Station_10_Reasoncodes = s2
        Case "11"
            ReasonCodes.Station_11_Reasoncodes = s2
    End Select
End Sub

Then, I would make the initiator:

    Public Sub ReadReasonCodes()
        For i As Integer = 1 To 11
            GetReasonCodes(CStr(i))
        Next
    End Sub
End Class

you can access your data from the class "ReasonCodes"

EXA:

Dim Stn6_code15 = ReasonCodes.Station_6_Reasoncodes(14).ReasonCode
'Note: since reasoncodes are stored in an Array, they are 0 based. 
'So ReasonCodes.Station_6_Reasoncodes(14).ReasonCode = 
'station 6, reason code 15.

Of course, I cannot test this, as I have no access DB files, but it should work, and if not it may require minor tweaks to obtain your goal.

Hope this helps you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.