2011-11-27 5 views
0

Microsoft Access 데이터베이스에 연결하고 파일을 읽고 일부 수학을 수행 한 다음 몇 가지 기본 통계를 다시 제공하는 Windows 양식 프로젝트에서 작업하고 있습니다. 지금 나는 VB에서 가르치고 있으며, 아래의 코드가 더 효율적일 수 있음을 알고있다. 그러나 지금 당장은 그것을 기능적으로 만들려고 노력하고 있습니다.'채우기'를 호출하기 전에 select 명령 속성이 초기화되지 않았습니다.

프로그램은 sql을 통해 필요한 데이터를 필터링하고 몇 가지 sql 문이 있습니다. 폼을로드 할 때 각각을 호출 할 수 있도록 사용자가 버튼을 클릭하여 업데이트 할 때도 호출 할 수 있도록 각 SQL 문에 대한 코드와 서브 루틴을 구분했습니다. 프로그램은 폼로드에 잘 작동하지만, 업데이트 버튼을 클릭하면 서브 루틴 Count()의 'odaCalls.Fill'에 다음과 같은 오류가 발생합니다. "select 명령 속성이 'Fill'을 호출하기 전에 초기화되지 않았습니다.

어떤 도움이 크게 감사. 나는 구글에 검색 및 제안이 찾았지만 수정을 발견하지 않은 시도한다.

Option Explicit On 

Public Class Form1 

    'Count() Variables 
    Dim strSQL = "SELECT * FROM tblcallLog" 

    Dim strPath As String = "Provider=Microsoft.ACE.OLEDB.12.0 ;" _ 
     & "Data Source=C:\callLogRev2_be.accdb" 
    Dim odaCalls As New OleDb.OleDbDataAdapter(strSQL, strPath) 
    Dim datCallCount As New DataTable 
    Dim intCount As Integer = 0 

    'LiveCalls() variables 
    Dim strSQLLive As String = "SELECT * FROM tblcallLog WHERE callLive=True" 
    Dim odaCallsLive As New OleDb.OleDbDataAdapter(strSQLLive, strPath) 
    Dim datCallLive As New DataTable 
    Dim intCallLiveCount As Integer = 0 
    Dim decCallLivePct As Decimal 

    'TransferCalls() variables 
    Dim strSQLTransfered As String = _ 
     "SELECT * FROM tblcallLog WHERE callName LIKE '% transfer %' OR callName LIKE 'transfer%'" 
    Dim odaCallsTransfered As New OleDb.OleDbDataAdapter(strSQLTransfered, strPath) 
    Dim datCallTransfered As New DataTable 
    Dim intCallTransfered As Integer = 0 
    Dim decCallTranfered As Decimal 

    'SingleStaffCall() Variables 
    Dim strSQLSingleStaff As String = _ 
     "SELECT * FROM tblcallLog WHERE callName LIKE '% single %' OR callName LIKE 'single%'" 
    Dim odaCallSingleStaff As New OleDb.OleDbDataAdapter(strSQLSingleStaff, strPath) 
    Dim datCallSingleStaff As New DataTable 
    Dim intCallSingleStaff As Integer = 0 
    Dim decCallSingleStaff As Decimal 

    'SingleStaffCallsLive() Variables 
    Dim strSQLSingleStaffLive As String = _ 
     "SELECT * FROM tblcallLog WHERE callName LIKE '% single %' OR callName LIKE 'single%' AND callLive=True" 
    Dim odaCallSingleStaffLive As New OleDb.OleDbDataAdapter(strSQLSingleStaffLive, strPath) 
    Dim datCallSingleStaffLive As New DataTable 
    Dim intCallSingleStaffLive As Integer = 0 
    Dim decCallSingleStaffLive As Decimal 

    'CallToday() Variables 
    Dim strSQLToday As String = _ 
     "SELECT * FROM tblcallLog WHERE startDate = date()" 
    Dim odaCallToday As New OleDb.OleDbDataAdapter(strSQLToday, strPath) 
    Dim datCallToday As New DataTable 
    Dim intCallToday As New Integer 

    'CallTodayLive() Variables 
    Dim strSQLTodayLiveCalls As String = _ 
     "SELECT * FROM tblcallLog WHERE callLive=TRUE AND startDate = date()" 
    Dim odaCallTodayLive As New OleDb.OleDbDataAdapter(strSQLTodayLiveCalls, strPath) 
    Dim datCallTodayLive As New DataTable 
    Dim intCallTodayLive As New Integer 
    Dim decCallTodayLive As New Decimal 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    Count() 
    LiveCalls() 
    TransferCalls() 
    SingleStaffCalls() 
    SingleStaffCallsLive() 
    CallToday() 
    CallTodayLive() 
    End Sub 

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click 

    'Checks the database for updates when user pushes the update button on the static data tab. 
    Count() 
    LiveCalls() 
    TransferCalls() 
    SingleStaffCalls() 
    SingleStaffCallsLive() 
    CallToday() 
    CallTodayLive() 
    End Sub 

    Private Sub Count() 
    'database connect and call count 
    odaCalls.Fill(datCallCount) 
    odaCalls.Dispose() 

    intCount = datCallCount.Rows.Count 

    lblTotalCallsData.Text = intCount.ToString 
    lblTotalCallsData.Visible = True 
    End Sub 

    Private Sub LiveCalls() 
    'determine number of live calls 
    odaCallsLive.Fill(datCallLive) 
    odaCallsLive.Dispose() 

    intCallLiveCount = datCallLive.Rows.Count 
    lblcallsLiveData.Text = intCallLiveCount.ToString 
    lblcallsLiveData.Visible = True 

    decCallLivePct = intCallLiveCount/intCount 
    lblPctCallsLiveData.Text = decCallLivePct.ToString("P") 
    lblPctCallsLiveData.Visible = True 
    End Sub 

    Private Sub TransferCalls() 
    'determine the number of transfer calls 

    odaCallsTransfered.Fill(datCallTransfered) 
    odaCallsTransfered.Dispose() 

    intCallTransfered = datCallTransfered.Rows.Count 
    lblTotalTransferCallsData.Text = intCallTransfered.ToString 
    lblTotalTransferCallsData.Visible = True 

    decCallTranfered = intCallTransfered/intCount 
    lblPctTransferCallsData.Text = decCallTranfered.ToString("P") 
    lblPctTransferCallsData.Visible = True 
    End Sub 

    Private Sub SingleStaffCalls() 
    'determine the number of single staff calls and percentage of total 

    odaCallSingleStaff.Fill(datCallSingleStaff) 
    odaCallSingleStaff.Dispose() 

    intCallSingleStaff = datCallSingleStaff.Rows.Count 
    lblTotalSingleStaffCallData.Text = intCallSingleStaff.ToString 
    lblTotalSingleStaffCallData.Visible = True 
    decCallSingleStaff = intCallSingleStaff/intCount 
    lblPctSingleStaffCallsData.Text = decCallSingleStaff.ToString("P") 
    End Sub 

    Private Sub SingleStaffCallsLive() 
    'determine the percentage of single staff calls taken live 

    odaCallSingleStaffLive.Fill(datCallSingleStaffLive) 
    odaCallSingleStaffLive.Dispose() 

    intCallSingleStaffLive = datCallSingleStaffLive.Rows.Count 
    decCallSingleStaffLive = intCallSingleStaffLive/intCount 

    lblPctSingleStaffCallsLiveData.Visible = True 
    lblPctSingleStaffCallsLiveData.Text = decCallSingleStaffLive.ToString("P") 
    End Sub 

    Private Sub CallToday() 
    'determine values for todays date 
    odaCallToday.Fill(datCallToday) 
    odaCallToday.Dispose() 

    intCallToday = datCallToday.Rows.Count 
    lblTotalCallsTodayData.Text = intCallToday 
    lblTotalCallsTodayData.Visible = True 
    End Sub 

    Private Sub CallTodayLive() 
    'determine the number of live calls today 
    odaCallTodayLive.Fill(datCallTodayLive) 
    odaCallTodayLive.Dispose() 

    intCallTodayLive = datCallTodayLive.Rows.Count 
    lblCallsTodayLiveData.Text = intCallTodayLive.ToString 
    lblCallsTodayLiveData.Visible = True 

    'Statement to error correct so the database doesn't force the program to divide by zero 
    Try 
     decCallTodayLive = intCallTodayLive/intCallToday 
    Catch Exception As DivideByZeroException 
     lblPctCallsTodayLiveData.Text = "0.00%" 
     lblPctCallsTodayLiveData.Visible = True 
    Catch Exception As OverflowException 
     decCallTodayLive = 0 
    End Try 

    lblPctCallsTodayLiveData.Text = decCallTodayLive.ToString("P") 
    lblPctCallsTodayLiveData.Visible = True 
    End Sub 
End Class 
+2

오류 때문에 게시 할 코드가 너무 많습니다. 관련 부품으로 트리밍 할 수 있습니까? – LarsTech

답변

1

문제는 당신이 그들을 작성 후 즉시 dataadapters을 배치하는 것입니다.

이것은 양식로드시 작동하지만 이후에는 작동하지 않는 이유입니다. y 폼 레벨에서 스펙으로 작성하는 대신 메소드에서 사용하는 데이터 어댑터.

+1

+1 Dispose를 호출하는 대신'Using' 문을 사용하는 것도 좋은 생각입니다. 이것은 OP가 제안하는 대로만 수행 할 수 있습니다. –

+0

모두에게 제안 해 주셔서 감사합니다. – Brad

+0

당신은 오신 것을 환영합니다. 답변이 해결되었거나 문제를 해결하는 데 도움이된다면, 질문 옆에있는 확인란을 클릭하여 해당 답변이 귀하에게 도움이되었다는 것을 알리십시오. –

관련 문제