Hi folks,
I've written a module to import data from one table into another. I get a daily MTD table in excel that I import through a saved import procedure, then my module grabs the first date from the imported file, deletes all records >= that date, then imports the new data, adding in some other fields from a JOIN query (things like department info and manager name etc - cos these change monthly, I need to store them in the central table in 'real time'... I know it's not 'normal' but the best solution I could come up with).
Anyway, from a developer point of view, I print how many records were deleted in the immediate window, then how many were inserted in the second run.
We should be seeing that the number for inserted records is higher than what has been deleted... but it's not.
On checking, the INSERT INTO command is working correctly, but the number being displayed is wrong. I.e. It successfully inserted 1267 records this morning, but the RecordsAffected tells me there were only 1187... but the first figure (records deleted) is correct.
I've scoured these forums and other sites and can't figure out what's wrong.
The only thing I can think of that might affect it is that before I insert the records, I update any employee IDs with a preceding 0 if they are only 5 numbers - I need this to link to a seperate table.
Anyway, here's some of the code...
I'm sure I'm missing something (I'm still quite new at this) but I cannot seem to find the answer anywhere.
Thanks in advance
I've written a module to import data from one table into another. I get a daily MTD table in excel that I import through a saved import procedure, then my module grabs the first date from the imported file, deletes all records >= that date, then imports the new data, adding in some other fields from a JOIN query (things like department info and manager name etc - cos these change monthly, I need to store them in the central table in 'real time'... I know it's not 'normal' but the best solution I could come up with).
Anyway, from a developer point of view, I print how many records were deleted in the immediate window, then how many were inserted in the second run.
We should be seeing that the number for inserted records is higher than what has been deleted... but it's not.
On checking, the INSERT INTO command is working correctly, but the number being displayed is wrong. I.e. It successfully inserted 1267 records this morning, but the RecordsAffected tells me there were only 1187... but the first figure (records deleted) is correct.
I've scoured these forums and other sites and can't figure out what's wrong.
The only thing I can think of that might affect it is that before I insert the records, I update any employee IDs with a preceding 0 if they are only 5 numbers - I need this to link to a seperate table.
Anyway, here's some of the code...
Code:
Private Sub btnimportnew_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim dtefirst As String
Dim strRSSQL As String
Dim strdelSQL As String
Dim strupdt As String
Dim empid As String
Dim intrecordsupd As Integer
DoCmd.SetWarnings (False)
'find first date in new data
strRSSQL = "SELECT * FROM NEWDATA ORDER BY [NEWDATA]![LAST CONTACT DATE];"
Set db = CurrentDb()
Set rs = db.OpenRecordset(strRSSQL, dbOpenDynaset, dbReadOnly)
rs.MoveFirst
dtefirst = sqldate(rs![Last Contact Date])
rs.Close
Set rs = Nothing
'Debug.Print dtefirst
'delete records from DATA where dates are included in new data
strdelSQL = "DELETE * FROM DATA WHERE [DATA]![LAST CONTACT DATE]>=" & dtefirst & ";"
db.Execute strdelSQL
intrecordsupd = db.RecordsAffected
[I]This one works fine...[/I]
Debug.Print "There were " & intrecordsupd & " records deleted from the DATA table"
'add preceding zero to len=5 ids
Set rs = db.OpenRecordset("SELECT AGENTLNIATA FROM NEWDATA ORDER BY AGENTLNIATA;", dbOpenDynaset, dbSeeChanges)
rs.MoveFirst
Do Until rs.EOF = True
If Len(rs!AGENTLNIATA) = 5 Then
empid = rs!AGENTLNIATA
empid = "0" & empid
rs.Edit
rs!AGENTLNIATA = empid
rs.Update
End If
rs.MoveNext
Loop
Set rs = Nothing
'move new data into DATA
strupdt = "INSERT INTO DATA SELECT * FROM qry_NEW_DATA_CLEAN;"
db.Execute strupdt
intrecordsupd = db.RecordsAffected
[I]This one gives the wrong number...[/I]
[B]Debug.Print "There were " & intrecordsupd & " records inserted into the DATA table"
[/B]
'update tables
DoCmd.RunSQL ("DELETE * FROM NEWDATA;")
Set db = Nothing
DoCmd.SetWarnings (True)
MsgBox "The new data has been imported", , "Done!"
End Sub
I'm sure I'm missing something (I'm still quite new at this) but I cannot seem to find the answer anywhere.
Thanks in advance