473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

"Insert Into" statement not working

Good Day All, I have some EEO data in an old dBase4 database that I have converted to an Access table. Since dBase was not a relational database, I didn't create any key fields. I linked all of the tables together on Employees' SSN; therefore, in order to use this data with the current Access database, I need to create a relationship between the old dBase4 EEO data and the Access employee data. The Access employee data was also an old dBase table, but after importing it into Access and adding a key field, EmployeeID, Access automatically assigned a number to each of my 1,200+ records. This part of the equation was simple, and additionally, I am attempting to do a few other things here:

1st - I need to use the SSN in the old dBase data (EEO) and do a FindFirst in tblMasterEmplyee. If I find a match, I want to take the EmployeeID from that record and put it in tblEEO_EmployeeData, along with the data from the associated record in the old dBase table (tblEEO_DataFromDBase).

2. If no match is found for the SSN in tblMasterEmployee, I want to Insert a new record and place the SSN in the table; save the record and then take the new EmployeeID from tblMasterEmployee with the data in tblEEO_DataFromDBase and place it in tblEEO_EmployeeData

Pretty Simple; huh? :o)

Here is the code:

Expand|Select|Wrap|Line Numbers
  1.     Dim blnExist As Boolean
  2.     Dim rstSource As DAO.Recordset
  3.     Dim rstDest As DAO.Recordset
  4.     Dim rstGetID As DAO.Recordset
  5.     Dim lngEmpID As Long
  6.     Dim lngJGCtr As Long
  7.     lngJGCtr = 0
  8.  
  9.     Set rstSource = CurrentDb.OpenRecordset("tblEEO_DataFromDBase")
  10.     Set rstDest = CurrentDb.OpenRecordset("tblEEO_EmployeeData")
  11.     Set rstGetID = CurrentDb.OpenRecordset("tblMasterEmployee")
  12.  
  13.     Do Until rstSource.EOF
  14.         blnExist = DCount("*", "tblMasterEmployee", "SSN='" & rstSource!SSN & "'")
  15.         If Not blnExist Then
  16.             CurrentDb.Execute "insert into tblMasterEmployee (SSN) values ('" & rstGetID!SSN & "')"
  17.             DoCmd.GoToRecord , , acLast
  18.             rstGetID.FindFirst "rstGetID [SSN] = '" & rstGetID!SSN & "'"
  19. '----------------^Get error on this line "Operation is not supported 'for this object" Run-time error 3251 
  20.         End If
  21.  
  22.  
  23.             If rstGetID.NoMatch Then
  24.                 MsgBox "Unable to find match!"
  25.             Else
  26.                 MsgBox "The item is : " & rstGetID![SSN]
  27.             End If  
  28.             rstDest.AddNew
  29.             rstDest!EmployeeID = rstGetID!EmployeeID
  30.             rstDest!Race = rstSource!Race
  31.             rstDest!Gender = rstSource!Gender
  32.             rstDest!PositionID = rstSource!Position
  33.             lngJGCtr = lngJGCtr + 1
  34.             If lngJGCtr < 6 Then
  35.                rstDest!JobGroupID = 5
  36.             Else
  37.                lngJGCtr = 0
  38.                rstDest!JobGroupID = 4
  39.             End If
  40.             rstDest!CategoryID = rstSource!Category
  41.             rstDest!Veteran = rstSource!Vet_Stat
  42.             rstDest!DisVet = rstSource!Dis_Vet
  43.             rstDest!VietNamVet = rstSource!Nam_Stat
  44.             rstDest!DisabilityStatus = rstSource!Handicap
  45.             rstDest!Disability = rstSource!H_Cap
  46.             rstDest.Update
  47.             rstSource.MoveNext
  48.     Loop
  49.  
The program keeps blowing up at the Insert Into command with the following error:

"Operation is not supported for this object" Run-time error 3251

That error is only if SSN not found and I attempt to add the record and then find it for applicable data manipulation. However, all of the records that exist and do process shows an EmployeeID of the number "1". The first person in rstGetID has an employeeID of 1, so for reason, the program is only reading the first record.

Many thanks to whoever comes to my rescue.

David

Running on Windows XP and Access 2002 in 2000 mode.
Mar 29 '07 #1
5 4565
MMcCarthy
14,534 Expert Mod 8TB
I've made a couple of changes and also I have a question?

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim blnExist As Boolean
  3.     Dim rstSource As DAO.Recordset
  4.     Dim rstDest As DAO.Recordset
  5.     Dim rstGetID As DAO.Recordset
  6.     Dim lngEmpID As Long
  7.     Dim lngJGCtr As Long
  8.     lngJGCtr = 0
  9.  
  10.     Set rstSource = CurrentDb.OpenRecordset("tblEEO_DataFromDBase")
  11.     Set rstDest = CurrentDb.OpenRecordset("tblEEO_EmployeeData")
  12.     Set rstGetID = CurrentDb.OpenRecordset("tblMasterEmployee")
  13.  
  14.     rstSource.MoveFirst
  15.     Do Until rstSource.EOF
  16.         blnExist = DCount("*", "tblMasterEmployee", "SSN='" & rstSource!SSN & "'")
  17.         If Not blnExist Then
  18.        DoCmd.RunSQL "insert into tblMasterEmployee (SSN) values ('" & rstGetID!SSN & "')"
  19. ' what are you trying to do here? 
  20. ' If you are trying to find the record you've just inserted than you
  21. ' can't as you would have to close and then reopen the recordset
  22.             DoCmd.GoToRecord , , acLast 
  23.             rstGetID.FindFirst "rstGetID [SSN] = '" & rstGetID!SSN & "'"
  24.  
  25.         End If
  26. >
  27. >
  28. >
  29.  
Mar 30 '07 #2
I've made a couple of changes and also I have a question?

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim blnExist As Boolean
  3.     Dim rstSource As DAO.Recordset
  4.     Dim rstDest As DAO.Recordset
  5.     Dim rstGetID As DAO.Recordset
  6.     Dim lngEmpID As Long
  7.     Dim lngJGCtr As Long
  8.     lngJGCtr = 0
  9.  
  10.     Set rstSource = CurrentDb.OpenRecordset("tblEEO_DataFromDBase")
  11.     Set rstDest = CurrentDb.OpenRecordset("tblEEO_EmployeeData")
  12.     Set rstGetID = CurrentDb.OpenRecordset("tblMasterEmployee")
  13.  
  14.     rstSource.MoveFirst
  15.     Do Until rstSource.EOF
  16.         blnExist = DCount("*", "tblMasterEmployee", "SSN='" & rstSource!SSN & "'")
  17.         If Not blnExist Then
  18.        DoCmd.RunSQL "insert into tblMasterEmployee (SSN) values ('" & rstGetID!SSN & "')"
  19. ' what are you trying to do here? 
  20. ' If you are trying to find the record you've just inserted than you
  21. ' can't as you would have to close and then reopen the recordset
  22.             DoCmd.GoToRecord , , acLast 
  23.             rstGetID.FindFirst "rstGetID [SSN] = '" & rstGetID!SSN & "'"
  24.  
  25.         End If
  26. >
  27. >
  28. >
  29.  
Expand|Select|Wrap|Line Numbers
  1. ' what are you trying to do here? 
  2. ' If you are trying to find the record you've just inserted than you
  3. ' can't as you would have to close and then reopen the recordset[/b]
  4.             DoCmd.GoToRecord , , acLast 
  5.             rstGetID.FindFirst "rstGetID [SSN] = '" & rstGetID!SSN & "'"
  6.  
  7.         End If
Thanks for the insight and assistance. I mistakenly thought that you had to save the record before you could manipulate the data. After looking at your code, I only included the DoCmd.RunSQL command, and although it ran through the EOF of the recordset, all 1,200+ records still has an EmployeeID of 1. So for some reason my logic is forcing the program to only read the first record of the rstGetID recordset.

Any more suggestions?

David
Mar 30 '07 #3
MMcCarthy
14,534 Expert Mod 8TB
Try this ...

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim blnExist As Boolean
  3.     Dim rstSource As DAO.Recordset
  4.     Dim rstDest As DAO.Recordset
  5.     Dim rstGetID As DAO.Recordset
  6.     Dim lngEmpID As Long
  7.     Dim lngJGCtr As Long
  8.     lngJGCtr = 0
  9.  
  10.     Set rstSource = CurrentDb.OpenRecordset("tblEEO_DataFromDBase")
  11.     Set rstDest = CurrentDb.OpenRecordset("tblEEO_EmployeeData")
  12.     Set rstGetID = CurrentDb.OpenRecordset("tblMasterEmployee")
  13.  
  14.     rstSource.MoveFirst
  15.  
  16.     Do Until rstSource.EOF
  17.         blnExist = DCount("*", "tblMasterEmployee", "SSN='" & rstSource!SSN & "'")
  18.         If Not blnExist Then
  19.       rstGetID.FindFirst "[SSN] = '" & rstSource!SSN & "'"
  20.        DoCmd.RunSQL "insert into tblMasterEmployee (SSN) values ('" & rstGetID!SSN & "')"
  21.         End If
  22.       If rstGetID.NoMatch Then
  23.                 MsgBox "Unable to find match!"
  24.             Else
  25.                 MsgBox "The item is : " & rstGetID![SSN]
  26.             End If  
  27.             rstDest.AddNew
  28.             rstDest!EmployeeID = rstGetID!EmployeeID
  29.             rstDest!Race = rstSource!Race
  30.             rstDest!Gender = rstSource!Gender
  31.             rstDest!PositionID = rstSource!Position
  32.             lngJGCtr = lngJGCtr + 1
  33.             If lngJGCtr < 6 Then
  34.                rstDest!JobGroupID = 5
  35.             Else
  36.                lngJGCtr = 0
  37.                rstDest!JobGroupID = 4
  38.             End If
  39.             rstDest!CategoryID = rstSource!Category
  40.             rstDest!Veteran = rstSource!Vet_Stat
  41.             rstDest!DisVet = rstSource!Dis_Vet
  42.             rstDest!VietNamVet = rstSource!Nam_Stat
  43.             rstDest!DisabilityStatus = rstSource!Handicap
  44.             rstDest!Disability = rstSource!H_Cap
  45.             rstDest.Update
  46.             rstSource.MoveNext
  47.     Loop
  48.  
Mar 30 '07 #4
The "If Not blnExist " condition was never true, so the program never hit the FindFirst statment. I commented out the If-Then statement, but still used the fiindfirst:

Expand|Select|Wrap|Line Numbers
  1. Do Until rstSource.EOF
  2.         blnExist = DCount("*", "tblMasterEmployee", "SSN='" & rstSource!SSN & "'")
  3. '        If Not blnExist Then
  4. '            rstGetID.FindFirst "[SSN] = '" & rstSource!SSN & "'"
  5. '            DoCmd.RunSQL "insert into tblMasterEmployee (SSN) values ('" & rstGetID!SSN & "')"
  6. '        End If
  7.         rstGetID.FindFirst "[SSN] = '" & rstSource!SSN & "'"
  8.         If rstGetID.NoMatch Then
  9. '..............Rest of code
I received the following error:

Run-time error 3251 Operation is not supported for this type of object

However, when I ran your code with the If-Then statement, I noticed that the program is never finding a match on SSN in rstGetID, so the recordset pointer is always sitting at the first record, which is why when the program runs through EOF, all of the EmployeeIDs in rstDest is 1. For some reason, Access appears not to like the the FindFirst statement with the rstGetID recordset.
Mar 30 '07 #5
I figured it out. I needed to add dbOpenDynaset to Current.db statements:

Expand|Select|Wrap|Line Numbers
  1. Set rstSource = CurrentDb.OpenRecordset("tblEEO_DataFromDBase", dbOpenDynaset)
  2. Set rstDest = CurrentDb.OpenRecordset("tblEEO_EmployeeData", dbOpenDynaset)
  3. Set rstGetID = CurrentDb.OpenRecordset("tblMasterEmployee", dbOpenDynaset)
Thanks for pointing me in the right direction. Have a blessed day as you have blessed mine.

David
Mar 30 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: newbie_mw | last post by:
Hi, I need urgent help with a novice problem. I would appreciate any advice, suggestions... Thanks a lot in advance! Here it is: I created a sign-up sheet (reg.html) where people fill in their...
1
by: newbie_mw | last post by:
Seems my post was buried in more cries for help :-) I will try again. It's probably a very novice question so please take a look! Thanks!...
5
by: Chad Richardson | last post by:
Is there a way in SQL Server 2000 to extract data from a table, such that the result is a text file in the format of "Insert Into..." statements, i.e. if the table has 5 rows, the result would be 5...
3
by: anurag | last post by:
Hi, Is "MERGE INTO..." statement allowed to be executed in a dynamic SQL statement, inside an SQL Stored Procedure? I am using DB2 UDB 8.1.2 on Windows. If it is not allowed (as I suspect),...
20
by: Mark Harrison | last post by:
So I have some data that I want to put into a table. If the row already exists (as defined by the primary key), I would like to update the row. Otherwise, I would like to insert the row. I've...
4
by: Takeadoe | last post by:
Dear NG - I've got a bunch of SQL statements (Insert Into "Access Table Name") generated from another software program. They look like this: Pasting these lines into the SQL view as they are...
4
by: Bart op de grote markt | last post by:
Hello I used to work in a Sybase database environment. When I had to insert/ update records in the database, I always used "insert on existing update", in this way, you didn't have to check...
6
by: ewpatton | last post by:
Good day, I've been trying to work with SQL and an Access database in order to handle custom user profiles. I haven't had any trouble reading from my database, but inserting new entries into...
2
by: paulquinlan100 | last post by:
Hi I'm using automation in excel to insert a lot of data into an Access DB. Its all working fine, apart from the fact that the data entry people occasionally put "n/a" in the cells instead of 0,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.