I agree with ridders52, an UPDATE query would be faster, but I don't know what you are trying to do now..... Would need to have your dB to try and understand......
About your code, there are several syntax errors and a couple of other errors. Here is what I see:
Code:
Option Compare Database '<--EVERY module should have this line at the top
Option Explicit '<--EVERY module should have this line at the top
Sub test() '<< the name I used for this routine
Dim strSQL As String
Dim db As DAO.Database
Dim sMfrPartNumber As String
Dim sPartDescription As String
Dim rstSource As DAO.Recordset
Dim rstTarget As DAO.Recordset
Set db = CurrentDb
On Error GoTo EH ' 1
strSQL = "DELETE * from tblPartMain"
CurrentDb.Execute strSQL, dbFailOnError
strSQL = "SELECT MfrPartNumber, PartDescription"
strSQL = strSQL & " FROM (SELECT Material AS MfrPartNumber, [Material Description] AS PartDescription"
strSQL = strSQL & " FROM Master_SAP_MATERIAL"
strSQL = strSQL & " UNION SELECT Component AS MfrPartNumber,"
strSQL = strSQL & " [Component Description] AS PartDescription"
strSQL = strSQL & " FROM Master_SAP_MATERIAL) AS C;"
Set rsParts = db.OpenRecordset(strSQL) ' 2
strTable = "tblPartMain" ' 3
Set rstSource = db.OpenRecordset(strSQL)
Set rstTarget = db.OpenRecordset("tblPartMain", dbOpenDynaset)
Do While Not rstSource.EOF ' 4
sMfrPartNumber = rstSource!MfrPartNumber
sPartDescription = rstSource!PartDescription
rstTarget.AddNew
rstTarget![MfrPartNumber] = sMfrPartNumber
rstTarget![PartDescription] = sPartDescription
rstTarget.Update
rstSource.MoveNext
Loop
rstTarget.Close ' 5
db.Close
End Sub
1) Compile error - the Label "EH" is not defined. Missing the error handling code
2) Compile error - Variable not defined. You are trying to open a recordset named "rsParts", but "rsParts" was never declared (no Dim statement). Should delete this line.
3) Compile error - Variable not defined. You are trying to set the variable "strTable" to a value, but "strTable" was never declared (no Dim statement)/ Also, "strTable" is never used in the code, so why is it there? Should delete this line.
4) Good programming practice is to test for records BEFORE trying to use the record set. If there are no records in the record set, there will be an error.
Something like
Code:
.
.
.
If Not rstSource.BOF and Not rstSource.EOF Then
rstSource.MoveLast
rstSource.MoveFirst
Do While Not rstSource.EOF
.
.
.
Loop
End if
.
.
5) You DON'T close the dB. The rule is "If you create it, destroy it, if you open it, close it". You opened 2 record sets, but only closed one. You also created 2 record sets, but didn't destroy either. And you didn't destroy the "db" object.
The code would look like
Code:
rstTarget.Close
rstSource..Close
Set rstTarget = Nothing
Set rstSource = Nothing
Set db = Nothing
End Sub