Results 1 to 5 of 5

Thread: [2008] 2 Questions: Access Database & Progress Bar

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    [2008] 2 Questions: Access Database & Progress Bar

    Question 1:
    My Access File is Adding All my Courses that Contain More than 1 of into 1 Database File.

    Example:

    I have ICS4M1-01 4 Times in a File

    It creates the new Database File (ICS4M1-01.mdb) & then suppose to add just 1 to Information but instead it will add 3 others to it, all with the same information.

    How can I fix this?

    The Code for all of it is below.
    vb Code:
    1. Option Strict On
    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 * 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.                     Application.DoEvents()
    23.                     If bImportCancel = False Then
    24.                         For Each DRow As DataRow In DS.Tables(0).Rows
    25.                             If .lstCourses.Items.Contains(DRow("Classes(Import If #1)")) = False Then
    26.                                 frmImport.Show()
    27.                                 Connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Data\Courses\" & DRow("Classes(Import If #1)").ToString() & ".mdb")
    28.                                 CreateCourse(Connection.ConnectionString)
    29.                                 SQL = "INSERT INTO [Information] (CourseCode, RoomNumber) VALUES (?, ?);"
    30.                                 Command = New OleDbCommand(SQL, Connection)
    31.                                 Command.Connection.Open()
    32.                                 Command.Parameters.AddWithValue("CourseCode", DRow("Classes(Import If #1)"))
    33.                                 Command.Parameters.AddWithValue("RoomNumber", DRow("Home Room/Form"))
    34.                                 Command.ExecuteNonQuery()
    35.                                 Connection.Close()
    36.                                 frmImport.pbImport.Maximum = DS.Tables(0).Rows.Count
    37.                                 If frmImport.pbImport.Value > DS.Tables(0).Rows.Count + 5 Then
    38.                                     frmImport.pbImport.Value = frmImport.pbImport.Maximum
    39.                                     File.Delete(FileCopyName)
    40.                                     frmImport.Close()
    41.                                     MessageBox.Show("Import Complete")
    42.                                 Else
    43.                                     frmImport.pbImport.Increment(5)
    44.                                 End If
    45.                             End If
    46.                         Next
    47.                         MsgBox("Done")
    48.                     End If
    49.                 End If
    50.             End If
    51.         End With
    52.     End Sub
    53. End Module

    Question 2:
    My Progress Bar doesn't really work. I want it to increment while reading from the list and each time it reads one line of the file I want it to + 1 to the Value, and then once it reaches the End I want it to say Done, like in MsgBox("Done") but frankly, it doesn't, how would I go about incrementing to the process of reading line by line ?

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [2008] 2 Questions: Access Database & Progress Bar

    1) Add "DISTINCT" to your SQL line, this will chop out repeated data and only pull unique rows:

    SQL = "SELECT DISTINCT * FROM " & .ofdOpen.FileName

    You're pulling all the columns though, so even if one of those columns have different data, you'll get repeat listings. Always pull only the data you need:

    SQL = "SELECT DISTINCT CourseCode, RoomNumber FROM " & .ofdOpen.FileName

    You should use a parameter for your tablename too:
    SQL = "SELECT DISTINCT CourseCode, RoomNumber FROM ? "
    ...
    Command.Parameters.AddWithValue("TableName", .ofdOpen.FileName)

    2) Make a progress bar that has a Min = 0 and a Max = 100.
    Next, get a count of the rows you're going to be adding:
    total = DS.Tables(0).Rows.Count

    Finally, make a counting variable and += 1 each loop.
    Now, it's just simple math we all learned in 5th grade:

    ProgressBar.Value = (count / total) * 100
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: [2008] 2 Questions: Access Database & Progress Bar

    Thanks Jenner You seem to be one with great knowledge also. Always helping me, and giving me tips on how to improve.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: [2008] 2 Questions: Access Database & Progress Bar

    vb Code:
    1. Total = DS.Tables(0).Rows.Count
    2.                                 Count += 1
    3.                                 If frmImport.pbImport.Value >= Total Then
    4.                                     MessageBox.Show("Import Complete")
    5.                                     frmImport.pbImport.Value = frmImport.pbImport.Maximum
    6.                                     File.Delete(FileCopyName)
    7.                                     frmImport.Close()
    8.                                 Else
    9.                                     frmImport.pbImport.Value = (Count / Total) * 100
    10.                                 End If

    For some reason, when it reaches the end, or near it, it just stops. Not showing messagfe box.

  5. #5
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [2008] 2 Questions: Access Database & Progress Bar

    You're using the wrong value in your comparison.

    pbImport.Value will only ever be between 0 and 100. Why? Because you're the math you use standardizes it between 0 and 100; which is perfect for a progress bar.

    What happens if you got 200 rows, so Total = 200?

    pbImport.Value will NEVER be >= Total.

    What you should be checking is: Count >= Total, because Count will be between 1 and Total. As soon as it hits Total, it'll break to your end-routine.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width