
Originally Posted by
June7
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