MS Access SQL to copy part of a record displayed on a form, insert into same table as new record, and display the new record on a form

StackOverflow https://stackoverflow.com/questions/23527881

  •  17-07-2023
  •  | 
  •  

Question

I have inherited a Microsoft Access 2010 database that is used to keep position/employee information in a single table. The table has an auto-number field for each record. The database has a form – ScreenB – that uses a query to pull and display in ScreenB a single record based on search criteria (Personnel Number, Last Name, Social Security Number, etc.) entered by the user. The record contains approximately 25 fields that show the information about the position as well as information about the person in the position. Of course, every so often a person will leave a position and someone else will be hired into the vacated position. My users would like a button on ScreenB that will, when clicked:

  1. Copy the record information displayed in the form for just the 12 or so fields of position information being displayed on ScreenB.
  2. Paste the information as a new record in the table.
  3. Display the new record with the pasted information in the form.

Since the query pulls a single record, my thought was to create a new form – ScreenC – identical in all respects to ScreenB to display the new record. So, the button will copy the position information from the record displayed on ScreenB, paste it as a new record into the table, open ScreenC, jump to the end of the records in the table and display the new (last) record (which contains only the position information) on ScreenC, and close ScreenB, leaving the user with the position information only and a form that can be filled out with the employee information. This will preserve the information of the previous employee while in that position, which is required.

I’m not a trained programmer, so the code below is probably rife with errors, but it was the best I could come up with:

Dim strSQL as String
strSQL = "INSERT INTO [tblNewCurrent Emp] (numCostCenter, numPosition, txtTitles, txtClassCode. numGrade, txtPositionType, txtUnit1, txtFieldStaff, txtFedMatch, txtPositionLicReq, txtPositionOrigin, datPosAcquired, datPosLastVacated, datPosTransferred, txtOrigTitle, txtOrigClassCode, numOrigGrade, datPosReclassified) "
strSQL = strSQL & "SELECT numCostCenter, numPosition, txtTitles, txtClassCode. numGrade, txtPositionType, txtUnit1, txtFieldStaff, txtFedMatch, txtPositionLicReq, txtPositionOrigin, datPosAcquired, datPosLastVacated, datPosTransferred, txtOrigTitle, txtOrigClassCode, numOrigGrade, datPosReclassified from [tblNewCurrent Emp] "
strSQL = strSQL & "WHERE (numSystemNumber = " & Me.numSystemNumber & ")"
DoCmd.RunSQL (strSQL)
DoCmd.OpenForm frmScreenC
DoCmd.GoToRecord , , acLast
DoCmd.Close frmScreenB

The table is named “tblNewCurrent Emp”, which is why I placed it in brackets. numSystemNumber is the name of the auto-number field. All forms, queries and tables are in a single database/file. The field names in the code above are the actual names of the fields.

When I attempt to run the code, I get an error message that states, “Syntax error in INSERT INTO statement.” No other information, and no information in the Access Help system. I tried removing the brackets from the table name, and get the same error message. I would appreciate any input, thoughts, or suggestions. What is the error with the INSERT INTO statement? Is there another way to accomplish this task? Unfortunately, I can’t make structural changes to the database and since it is the backend for a number of other databases, changing the table name is not an option, either. Users access the database through the forms only. No datasheet or other views. Thanks.

EDIT: Thank you billmac1 and Remou. The "...,txtClassCode. numGrade,..." was the issue on the SQL. Once I corrected that, I used your suggestion to run the code in the Immediate Windows with the print.debug as well. Ran fine, so all that was left was to clean up the VBA code for opening and closing the forms. For anyone else attempting the same, I also set ScreenC to open to the last record in the Load property. It works perfectly now. Below is the finished code.

Dim strSQL As String
strSQL = "INSERT INTO [tblNewCurrent Emp] (numCostCenter, numPosition, txtTitles, txtClassCode, numGrade, txtPositionType, txtUnit1, txtFieldStaff, txtFedMatch, txtPositionLicReq, txtPositionOrigin, datPosAcquired, datPosLastVacated, datPosTransferred, txtOrigTitle, txtOrigClassCode, numOrigGrade, datPosReclassified) "
strSQL = strSQL & "SELECT numCostCenter, numPosition, txtTitles, txtClassCode, numGrade, txtPositionType, txtUnit1, txtFieldStaff, txtFedMatch, txtPositionLicReq, txtPositionOrigin, datPosAcquired, datPosLastVacated, datPosTransferred, txtOrigTitle, txtOrigClassCode, numOrigGrade, datPosReclassified FROM [tblNewCurrent Emp] "
strSQL = strSQL & "WHERE (numSystemNumber = " & Me.numSystemNumber & ")"
DoCmd.RunSQL (strSQL)
DoCmd.OpenForm "frmScreen C"
DoCmd.Close acForm, "frmScreen B"
Was it helpful?

Solution

Too new to this site to leave this one as a comment, but part of your SQL string caught my attention "...,txtClassCode. numGrade,...". There's a period and a space in both your field string and value string.

As Remou suggested, "...try a debug.print strSQL and paste the result into the query design window to check for problems." You might also find it beneficial to just take the SELECT portion of your statement and get it working, then try the INSERT portion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top