Alright, I have this code which - loops through a text file (very big text file) to find the Course ID, then it adds the item to the List Box and while its doing that, it creates the .mdb file & adds some data from the text file to the database, and while all that is going, a Progress Bar ( which I have yet to figure out how to get it to increment with my Adding of Data ).

The Code is very slow and the application gets hung up with Not Responding if I click on it, I want the user to be able to click on the App to cancel the current import but at the moment it just freezes up.

Code:

vb Code:
  1. Option Strict Off
  2. Imports System.Data.OleDb
  3. Imports System.IO
  4. Module modImport
  5.     Public Sub ImportCourse()
  6.         With frmCourses
  7.             If .ofdOpen.ShowDialog = DialogResult.OK Then
  8.                 Dim FileCopyName As String = Application.StartupPath & "\Data\Courses\" & Path.GetFileName(.ofdOpen.FileName.Remove(.ofdOpen.FileName.LastIndexOf("."))) & ".CSV"
  9.                 If File.Exists(FileCopyName) = True Then
  10.                     File.Delete(FileCopyName) 'Temp
  11.                 Else
  12.                     File.Copy(.ofdOpen.FileName, FileCopyName)
  13.                     SQL = "SELECT [Classes(Import If #1)] FROM " & .ofdOpen.FileName
  14.                     Dim Path As String = IO.Path.GetFullPath(.ofdOpen.FileName.Remove(.ofdOpen.FileName.LastIndexOf("\")))
  15.                     Connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Path & ";Extended Properties=""text;HDR=Yes;FMT=Delimited"";")
  16.                     Command = New OleDbCommand(SQL, Connection)
  17.                     Command.Connection.Open()
  18.                     Adapter.SelectCommand = Command
  19.                     DS = New DataSet
  20.                     Adapter.Fill(DS)
  21.                     Command.Connection.Close()
  22.                     frmImport.pbImport.Maximum = DS.Tables(0).Rows.Count
  23.                     frmImport.pbImport.Minimum = 0
  24.                     For Each DRow As DataRow In DS.Tables(0).Rows
  25.                         If .lstCourses.Items.Contains(DRow("Classes(Import If #1)")) = False Then
  26.                             Connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Data\Courses\" & DRow("Classes(Import If #1)") & ".mdb")
  27.                             CreateCourse(Connection.ConnectionString)
  28.                             SQL = "INSERT INTO [Information] (CourseCode) VALUES (?); "
  29.                             Command = New OleDbCommand(SQL, Connection)
  30.                             Command.Connection.Open()
  31.                             Command.Parameters.AddWithValue("CourseCode", DRow("Classes(Import If #1)"))
  32.                             Command.ExecuteNonQuery()
  33.                             Connection.Close()
  34.                             frmImport.Show()
  35.                             If frmImport.pbImport.Value < frmImport.pbImport.Maximum Then
  36.                                 frmImport.pbImport.Value += 5
  37.                             End If
  38.                         End If
  39.                     Next
  40.                     frmImport.Close()
  41.                     File.Delete(FileCopyName)
  42.                 End If
  43.  
  44.             End If
  45.         End With
  46.     End Sub
  47. End Module