|
-
Sep 1st, 2011, 08:48 AM
#1
Thread Starter
New Member
Slow moving program with long load time
Code:
Option Explicit On
Imports Microsoft.Office.Interop
Imports System.Data.OleDb
Imports ADOR
Imports System.IO
Public Class ediForm
'Declare our Excel Application object and Workbook object
Private moApp As Excel.Application
Private moWB As Excel.Workbook
Private strFileName As String
Dim ediDate As String
Friend partNum1 As String = "76801TA5 A010M2"
Friend partNum2 As String = "80100TK4 A010M1"
Friend partNum3 As String = "79710STK A010M1"
Dim timeStamp As String
Dim timeCount As Integer = 0
Dim counter As Integer = 0
Dim currentIndex As Integer = 0
Dim maxVal As String
Dim prevVal As String
Dim totalIndex As Integer = 0
Private Sub ediButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ediButton.Click
Try
Try
Dim OpenDlg As New OpenFileDialog
Using OpenDlg
With OpenDlg
.CheckFileExists = True
.CheckPathExists = True
.Filter = "ExcelWorkbooks Only (*.xls)|*.xls"
.FilterIndex = 1
.Multiselect = False
.ShowHelp = False
.ShowReadOnly = False
.Title = "Select an Excel Workbook file"
'If a file is selected then lets open it
If .ShowDialog = Windows.Forms.DialogResult.OK Then
strFileName = OpenDlg.FileName
'Call the .Open function and assign the return to the workbook variable for use
'in other functions/areas in our app
moWB = moApp.Workbooks.Open(strFileName)
'Show the opened workbook/application
moApp.Visible = False
End If
End With
End Using
fileTextBox.Text = strFileName
dateComboBox1.Enabled = True
Catch ex As Exception
MessageBox.Show("Error loading data: " & ex.Message.ToString, "Data Download Error", MessageBoxButtons.OK, MessageBoxIcon.Hand)
End Try
Try
Dim cnRange As String = "provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source='" & strFileName & "';" & _
"Extended Properties=""Excel 8.0; IMEX=1; HDR=No;"""
'moApp.Visible = True
Using MyConnection As New System.Data.OleDb.OleDbConnection(cnRange)
MyConnection.Open()
Dim da As New OleDbDataAdapter( _
"SELECT DISTINCT * FROM [Sheet1$O5:O]", MyConnection)
Dim dt As New DataTable
da.Fill(dt)
dateComboBox1.DisplayMember = dt.Columns(0).ColumnName
dateComboBox1.DataSource = dt
dataButton1.Enabled = True
dataButton2.Enabled = True
dataButton3.Enabled = True
End Using
Catch ex As Exception
End Try
Catch ex As Exception
MessageBox.Show("Program Failure: " & ex.Message.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Error handling on creating an Excel application as the user may not have it installed
Try
moApp = DirectCast(CreateObject("Excel.Application"), Excel.Application)
Catch ex As Exception
MessageBox.Show(ex.Message.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub closeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles closeButton.Click, ToolStripMenuItem2.Click
'Close and dispose our objects
If TypeName(moWB) <> "Nothing" Then
'Can change to autosave or not
moWB.Close() 'SaveChanges:=True
End If
moWB = Nothing
If TypeName(moApp) <> "Nothing" Then
moApp.Quit()
End If
moApp = Nothing
Me.Close()
End Sub
Private Sub dataButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dataButton1.Click
Try
Dim cnRange As String = "provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source='" & strFileName & "';" & _
"Extended Properties=""Excel 8.0; IMEX=1; HDR=No;"""
'moApp.Visible = True
Using MyConnection As New System.Data.OleDb.OleDbConnection(cnRange)
MyConnection.Open()
Dim cmd As OleDbCommand = New OleDbCommand( _
"SELECT * FROM [Sheet1$J5:P]", MyConnection)
Dim dr As System.Data.IDataReader = cmd.ExecuteReader
Dim dt As New DataTable
dt.Load(dr)
BindingSource1.DataSource = dt
BindingSource1.Filter = String.Format("F6 = '{0}' AND F1 = '{1}'", dateComboBox1.Text, partNum1)
If timeCount = 0 Then
Try
timeComboBox.DisplayMember = dt.Columns(6).ColumnName
timeComboBox.DataSource = BindingSource1
totalIndex = timeComboBox.Items.Count()
For counter As Integer = 0 To (totalIndex - 1)
timeComboBox.SelectedIndex = counter
currentIndex = timeComboBox.SelectedIndex()
prevVal = timeComboBox.Text
If maxVal < prevVal Then
maxVal = prevVal
timeStamp = maxVal
End If
Next
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
timeCount = timeCount + 1
End If
BindingSource2.DataSource = BindingSource1
BindingSource2.Filter = String.Format("F6 = '{0}' AND F1 = '{1}' AND F7 = '{2}'", dateComboBox1.Text, partNum1, timeStamp)
DataGridView1.DataSource = BindingSource2
DataGridView1.Columns("F5").Visible = False
DataGridView1.Columns("F6").Visible = False
DataGridView1.Columns("F7").Visible = False
Chart1.Series("a").XValueMember = "F3"
Chart1.Series("a").YValueMembers = "F4"
Chart1.DataSource = BindingSource2
Chart1.DataBind()
printOdyChartButton.Enabled = True
ToolStripMenuItem3.Enabled = True
MyConnection.Close()
End Using
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
AboutBox1.Show()
End Sub
Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
Form2.Show()
End Sub
Private Sub printOdyChartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles printOdyChartButton.Click, PrintChartOnlyToolStripMenuItem.Click
Chart1.Printing.Print(Printing.PrintAction.PrintToPrinter)
End Sub
Private Sub dataButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dataButton2.Click
Try
Dim cnRange As String = "provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source='" & strFileName & "';" & _
"Extended Properties=""Excel 8.0; IMEX=1; HDR=No;"""
'moApp.Visible = True
Using MyConnection As New System.Data.OleDb.OleDbConnection(cnRange)
MyConnection.Open()
Dim cmd As OleDbCommand = New OleDbCommand( _
"SELECT * FROM [Sheet1$J5:P]", MyConnection)
Dim dr As System.Data.IDataReader = cmd.ExecuteReader
Dim dt As New DataTable
dt.Load(dr)
BindingSource3.DataSource = dt
BindingSource3.Filter = String.Format("F6 = '{0}' AND F1 = '{1}'", dateComboBox1.Text, partNum2)
If timeCount = 0 Then
Try
timeComboBox.DisplayMember = dt.Columns(6).ColumnName
timeComboBox.DataSource = BindingSource1
totalIndex = timeComboBox.Items.Count()
For counter As Integer = 0 To (totalIndex - 1)
timeComboBox.SelectedIndex = counter
currentIndex = timeComboBox.SelectedIndex()
prevVal = timeComboBox.Text
If maxVal < prevVal Then
maxVal = prevVal
timeStamp = maxVal
End If
Next
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
timeCount = timeCount + 1
End If
BindingSource4.DataSource = BindingSource3
BindingSource4.Filter = String.Format("F6 = '{0}' AND F1 = '{1}' AND F7 = '{2}'", dateComboBox1.Text, partNum2, timeStamp)
DataGridView2.DataSource = BindingSource4
DataGridView2.Columns("F5").Visible = False
DataGridView2.Columns("F6").Visible = False
DataGridView2.Columns("F7").Visible = False
Chart2.Series("a").XValueMember = "F3"
Chart2.Series("a").YValueMembers = "F4"
Chart2.DataSource = BindingSource4
Chart2.DataBind()
printRidChartButton.Enabled = True
ToolStripMenuItem3.Enabled = True
MyConnection.Close()
End Using
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub dataButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dataButton3.Click
Try
Dim cnRange As String = "provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source='" & strFileName & "';" & _
"Extended Properties=""Excel 8.0; IMEX=1; HDR=No;"""
'moApp.Visible = True
Using MyConnection As New System.Data.OleDb.OleDbConnection(cnRange)
MyConnection.Open()
Dim cmd As OleDbCommand = New OleDbCommand( _
"SELECT * FROM [Sheet1$J5:P]", MyConnection)
Dim dr As System.Data.IDataReader = cmd.ExecuteReader
Dim dt As New DataTable
dt.Load(dr)
BindingSource5.DataSource = dt
BindingSource5.Filter = String.Format("F6 = '{0}' AND F1 = '{1}'", dateComboBox1.Text, partNum1)
If timeCount = 0 Then
Try
timeComboBox.DisplayMember = dt.Columns(6).ColumnName
timeComboBox.DataSource = BindingSource1
totalIndex = timeComboBox.Items.Count()
For counter As Integer = 0 To (totalIndex - 1)
timeComboBox.SelectedIndex = counter
currentIndex = timeComboBox.SelectedIndex()
prevVal = timeComboBox.Text
If maxVal < prevVal Then
maxVal = prevVal
timeStamp = maxVal
End If
Next
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
timeCount = timeCount + 1
End If
BindingSource6.DataSource = BindingSource5
BindingSource6.Filter = String.Format("F6 = '{0}' AND F1 = '{1}' AND F7 = '{2}'", dateComboBox1.Text, partNum1, timeStamp)
DataGridView3.DataSource = BindingSource6
DataGridView3.Columns("F5").Visible = False
DataGridView3.Columns("F6").Visible = False
DataGridView3.Columns("F7").Visible = False
Chart3.Series("a").XValueMember = "F3"
Chart3.Series("a").YValueMembers = "F4"
Chart3.DataSource = BindingSource6
Chart3.DataBind()
printPilChartButton.Enabled = True
ToolStripMenuItem3.Enabled = True
MyConnection.Close()
End Using
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub printRidChartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles printRidChartButton.Click
Chart2.Printing.Print(Printing.PrintAction.PrintToPrinter)
End Sub
Private Sub printPilChartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles printPilChartButton.Click
Chart3.Printing.Print(Printing.PrintAction.PrintToPrinter)
End Sub
Private Sub allVehButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles allVehButton.Click
dataButton1_Click(sender, e)
dataButton2_Click(sender, e)
dataButton3_Click(sender, e)
End Sub
End Class
Hi Everyone,
The code above works, but the program runs slow. The program has the user pick an excel file, pulls information from the file, and outputs it to a datagridview and a chart. I think the way the info is pulled in slows the program down. Is there a faster way to accomplish what I am currently doing?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|