Results 1 to 7 of 7
  1. #1
    Deggsy is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    May 2015
    Posts
    20

    Import Data via Excel Is Updating All the Records for a Particular Employee

    We have a form on our training database than can update training information via Excel sheets received from the trainers.
    The Update button runs on the following code:
    Code:
    Private Sub Command2_Click()
    Dim strSQL As String
    Dim txtLst As String
    Dim msgAns As Integer
    DoCmd.SetWarnings False
    If IsNull(Me!Table_List) = False Then
    strSQL = "UPDATE [" & Me.Table_List & "] INNER JOIN [Course Completed] ON [" & Me.Table_List & "].[Employee Number] = [Course Completed].[Employee Number] SET [Course Completed].Attended = [" & Me.Table_List & "].[Attended];"
    CurrentDb.QueryDefs("Register_Update_Query").Sql = strSQL
    Else
        MsgBox "Please choose the Register you wish to update", vbOKOnly, "No File Select!"
        Exit Sub
    End If
                DoCmd.OpenQuery "Register_Update_Query", acViewNormal, acEdit
    strSQL = "INSERT INTO [Training Run] ( OT, Course, Course_Date, Start_Time, Finish_Time ) " & _
                "SELECT TOP 1 [" & Me.Table_List & "].[OT], [" & Me.Table_List & "].[Course Name], [" & Me.Table_List & "].[Course Date], [" & Me.Table_List & "].[Start Time], [" & Me.Table_List & "].[Finish Time] " & _
                "FROM [" & Me.Table_List & "];"
    CurrentDb.QueryDefs("Register_Update_Query").Sql = strSQL
                DoCmd.OpenQuery "Register_Update_Query", acViewNormal, acEdit
                
                msgAns = MsgBox("Do You Wish To Remove the Register?" & vbNewLine & vbNewLine & "If you wish to remove later you will need to go through this process again.", vbYesNo, "Remove Rgister")
    If msgAns = vbYes Then
                DoCmd.DeleteObject acTable, Me!Table_List
    Else
                Cancel = True
    End If
    DoCmd.SetWarnings True
    Dim intItemsInList As Integer
    Dim intCounter As Integer
      
    intItemsInList = Me!Table_List.ListCount
      
    For intCounter = 0 To intItemsInList - 1
      Me!Table_List.RemoveItem 0
    Next
    Me.Table_List.SetFocus
    End Sub
    When imported, it creates a table based on the Excel sheet, links to other appropriate and updates the employees' training history in the table Course Completed (Design and Datasheet Views attached - See below), which among its fields is one for Attended, which will be either Y or N.
    It is possible that an employee can attend a training session on one date and not attend a later one, as in the example in the image below.

    What seems to be happening is that when an Excel sheet is received from a trainer, and it contains an employee with a record already in the table, that whatever Attended status is on the Excel sheet just received will override all the statuses in the table for that employee (in the example below, for instance, one employee should have had an Attended Status of Y on 27/04/2015 and N on 25/05/2015. The 27/04/2015 imported fine, but when the 25/05/2015 register was imported, where the employee Attended Status was N, the Attended status for this employee for 27/04/2015 was changed to N).



    I'm assuming at this stage that this is occurring either because of something in the above code during the import code, or because a property needs to be set in the Course Completed table in order to prevent data in previous records from being changed unless the exact dates are used, or something like that. Any advice would be appreciated...
    Click image for larger version. 

Name:	Course Completed Datasheet View.JPG 
Views:	18 
Size:	32.1 KB 
ID:	20921
    Click image for larger version. 

Name:	Course Completed Design View.JPG 
Views:	18 
Size:	52.2 KB 
ID:	20920

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,603
    Yes, need more criteria in the UPDATE action to prevent other records being updated. Perhaps comparing date values will accomplish. If you need more help, post the Register_Update_Query statement for analysis.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Deggsy is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    May 2015
    Posts
    20
    Quote Originally Posted by June7 View Post
    Yes, need more criteria in the UPDATE action to prevent other records being updated. Perhaps comparing date values will accomplish. If you need more help, post the Register_Update_Query statement for analysis.
    Thanks - here's the SQL for the Register_Update_Query, with the name of whatever imported spreadsheet taking the name of the table:
    Code:
    INSERT INTO [Training Run] ( OT, Course, Course_Date, Start_Time, Finish_Time )
    SELECT TOP 1 [Imported Spreadsheet Table].[OT], [Imported Spreadsheet Table].[Course Name], [Imported Spreadsheet Table].[Course Date], [Imported Spreadsheet Table].[Start Time], [Imported Spreadsheet Table].[Finish Time]
    FROM [Imported Spreadsheet Table];

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,603
    That is an INSERT action. It should not impact existing records, only add new records.

    An UPDATE action will modify existing records.

    I see now the code is using QueryDefs to modify query object. Why? Don't even need objects to execute these SQL actions. I use CurrentDb.Execute method.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Deggsy is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    May 2015
    Posts
    20
    Quote Originally Posted by June7 View Post
    That is an INSERT action. It should not impact existing records, only add new records.

    An UPDATE action will modify existing records.

    I see now the code is using QueryDefs to modify query object. Why? Don't even need objects to execute these SQL actions. I use CurrentDb.Execute method.
    Thanks for responding - Based on my limited knowledge, it appears that it was set up like this in case a returned register was incomplete (for instance leaving the times off it) but that existing records would still be updated (e.g. whether or not the employee attended the particular course). It creates a temporary table for the Excel register that is chosen and imported.

    I'm not that great on SQL - is there anything I can add to the existing SQL to keep all the Attended Statuses of a particular employee from being changed? (I'll add all the code below):
    Code:
    Private Sub Command2_Click()
    Dim strSQL As String
    Dim txtLst As String
    Dim msgAns As Integer
    DoCmd.SetWarnings False
    If IsNull(Me!Table_List) = False Then
    strSQL = "UPDATE [" & Me.Table_List & "] INNER JOIN [Course Completed] ON [" & Me.Table_List & "].[Employee Number] = [Course Completed].[Employee Number] SET [Course Completed].Attended = [" & Me.Table_List & "].[Attended];"
    CurrentDb.QueryDefs("Register_Update_Query").Sql = strSQL
    Else
        MsgBox "Please choose the Register you wish to update", vbOKOnly, "No File Select!"
        Exit Sub
    End If
                DoCmd.OpenQuery "Register_Update_Query", acViewNormal, acEdit
    strSQL = "INSERT INTO [Training Run] ( OT, Course, Course_Date, Start_Time, Finish_Time ) " & _
                "SELECT TOP 1 [" & Me.Table_List & "].[OT], [" & Me.Table_List & "].[Course Name], [" & Me.Table_List & "].[Course Date], [" & Me.Table_List & "].[Start Time], [" & Me.Table_List & "].[Finish Time] " & _
                "FROM [" & Me.Table_List & "];"
    CurrentDb.QueryDefs("Register_Update_Query").Sql = strSQL
                DoCmd.OpenQuery "Register_Update_Query", acViewNormal, acEdit
                
                msgAns = MsgBox("Do You Wish To Remove the Register?" & vbNewLine & vbNewLine & "If you wish to remove later you will need to go through this process again.", vbYesNo, "Remove Rgister")
    If msgAns = vbYes Then
                DoCmd.DeleteObject acTable, Me!Table_List
    Else
                Cancel = True
    End If
    DoCmd.SetWarnings True
    Dim intItemsInList As Integer
    Dim intCounter As Integer
      
    intItemsInList = Me!Table_List.ListCount
      
    For intCounter = 0 To intItemsInList - 1
      Me!Table_List.RemoveItem 0
    Next
    Me.Table_List.SetFocus
    End Sub
    
    Private Sub Form_Load()
    Dim obj As AccessObject
    Dim dbs As Object
    Dim ctrlListBox As ListBox
    Set dbs = Application.CurrentData
    Set ctrlListBox = Me.Table_List
    Dim intItemsInList As Integer
    Dim intCounter As Integer
      
    intItemsInList = Me!Table_List.ListCount
      
    For intCounter = 0 To intItemsInList - 1
      Me!Table_List.RemoveItem 0
    Next
        With ctrlListBox
          For Each obj In dbs.AllTables
            If Left$(obj.Name, 8) = "Register" Then
                .AddItem Item:=obj.Name
            End If
          Next obj
        End With
    End Sub
    
    Private Sub Form_Timer()
    Set dbs = Application.CurrentData
    Set ctrlListBox = Me.Table_List
    Dim intItemsInList As Integer
    Dim intCounter As Integer
      
    intItemsInList = Me!Table_List.ListCount
      
    For intCounter = 0 To intItemsInList - 1
      Me!Table_List.RemoveItem 0
    Next
    
        With ctrlListBox
          For Each obj In dbs.AllTables
            If Left$(obj.Name, 8) = "Register" Then
                .AddItem Item:=obj.Name
            End If
          Next obj
        End With
    End Sub
    
    Private Sub Table_List_GotFocus()
    Dim obj As AccessObject
    Dim dbs As Object
    Dim ctrlListBox As ListBox
    Set dbs = Application.CurrentData
    Set ctrlListBox = Me.Table_List
    Dim intItemsInList As Integer
    Dim intCounter As Integer
      
    intItemsInList = Me!Table_List.ListCount
      
    For intCounter = 0 To intItemsInList - 1
      Me!Table_List.RemoveItem 0
    Next
    
        With ctrlListBox
          For Each obj In dbs.AllTables
            If Left$(obj.Name, 8) = "Register" Then
                .AddItem Item:=obj.Name
            End If
          Next obj
        End With
    End Sub
    Last edited by June7; 06-08-2015 at 09:23 AM.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,603
    Not sure what the code looping through listbox items is accomplishing. I think it is just building the listbox list of items. Why it is in the timer event is beyond me. I do see the same code repeated in 3 events. Really should have the repetitive code in one procedure that can be called by the 3 events.

    Code:
    Private Sub Form_Load()
    Call ListBoxEdit
    End Sub
    
    Private Sub Form_Timer()
    Call ListBoxEdit
    End Sub
    
    Private Sub Table_List_GotFocus()
    Call ListBoxEdit
    End Sub
    
    Sub ListBoxEdit()
    Dim obj As AccessObject
    Dim dbs As Object
    Dim ctrlListBox As ListBox
    Set dbs = Application.CurrentData
    Set ctrlListBox = Me.Table_List
    Dim intItemsInList As Integer
    Dim intCounter As Integer
      
    intItemsInList = Me!Table_List.ListCount
      
    For intCounter = 0 To intItemsInList - 1
      Me!Table_List.RemoveItem 0
    Next
    With ctrlListBox
      For Each obj In dbs.AllTables
        If Left$(obj.Name, 8) = "Register" Then
            .AddItem Item:=obj.Name
        End If
      Next obj
    End With
    End Sub
    Still not seeing code or query that updates existing data.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Deggsy is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    May 2015
    Posts
    20
    Quote Originally Posted by June7 View Post
    Not sure what the code looping through listbox items is accomplishing. I think it is just building the listbox list of items. Why it is in the timer event is beyond me. I do see the same code repeated in 3 events. Really should have the repetitive code in one procedure that can be called by the 3 events.

    Code:
    Private Sub Form_Load()
    Call ListBoxEdit
    End Sub
    
    Private Sub Form_Timer()
    Call ListBoxEdit
    End Sub
    
    Private Sub Table_List_GotFocus()
    Call ListBoxEdit
    End Sub
    
    Sub ListBoxEdit()
    Dim obj As AccessObject
    Dim dbs As Object
    Dim ctrlListBox As ListBox
    Set dbs = Application.CurrentData
    Set ctrlListBox = Me.Table_List
    Dim intItemsInList As Integer
    Dim intCounter As Integer
      
    intItemsInList = Me!Table_List.ListCount
      
    For intCounter = 0 To intItemsInList - 1
      Me!Table_List.RemoveItem 0
    Next
    With ctrlListBox
      For Each obj In dbs.AllTables
        If Left$(obj.Name, 8) = "Register" Then
            .AddItem Item:=obj.Name
        End If
      Next obj
    End With
    End Sub
    Still not seeing code or query that updates existing data.
    Yeah, it's a mess, isn't it? This is what you get when you let one person build a database the way they want it. It's fine - so long as they don't leave :-)

    Thanks for looking at it anyway, it's greatly appreciated :-) :-) :-)

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 01-05-2015, 02:21 PM
  2. Import access query data into excel file, using excel vba
    By exceljockey in forum Import/Export Data
    Replies: 3
    Last Post: 11-15-2013, 02:36 PM
  3. import excel sheet causes autonumber beyond records
    By BCJourney in forum Import/Export Data
    Replies: 11
    Last Post: 09-25-2013, 06:02 AM
  4. Replies: 5
    Last Post: 03-01-2012, 01:11 PM
  5. Replies: 1
    Last Post: 02-10-2009, 09:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums