Can't run query in Access via vba with a long definition - ms-access
I got a very long query definition that works when it run it in the Access GUI. I don't want to save it as a Query object that an end user can accidently run so I want to run it via VBA. When I tried to run it using DoCmd.RunSQL it throws an error:
"A RunSQL action requires an argument consisting of an SQL statement."
When I try it with mydatabase.Execute I get:
"The Microsoft Access engine cannot find the input table or query ''. Make sure it exists and that its name is spelled correctly."
I did a Debug.Print and copied the output to an Query in the Access GUI, after cleaning up the line brakes the Immediate window added the query ran fine. The query string is 5,293 characters long. Any help would be appreciated.
Dim dBase As Database
Set dBase = CurrentDb()
Dim stringSQL_3 As String
stringSQL_3 = _
"INSERT INTO Measurement_Period_History ( EMPLOYEE_NUMBER, PERIOD_START, PERIOD_END, DATE_REVIEWED, AVERAGE_HOURS, PERIOD_YEAR, PERIOD_TYPE_ID, FIRST_PAY_WEEK, LAST_PAY_WEEK ) " & _
"SELECT FinalData.EMPLOYEE_NUMBER " & _
",FinalData.START_PERIOD " & _
",FinalData.END_PERIOD " & _
",Now() AS DATE_REVIEWED " & _
",IIF(FinalData.FLSA = 2, 40.00, SUM(FinalData.HOURS) / 52) AS AVERAGE_HOURS " & _
",Cint(FinalData.PERIOD_YEAR) AS PERIOD_YEAR " & _
",FinalData.PERIOD_TYPE_ID " & _
",FinalData.FIRST_PAY_WEEK " & _
",FinalData.LAST_PAY_WEEK "
stringSQL_3 = stringSQL_3 & _
"FROM ( " & _
"SELECT MeasurementData.EMPLOYEE_NUMBER " & _
",MeasurementData.FLSA " & _
",MeasurementData.STARTING_HIRE_DATE " & _
",MeasurementData.VARIABLE_EMPLOYEE " & _
",MeasurementData.START_PERIOD " & _
",MeasurementData.END_PERIOD " & _
",MeasurementData.PERIOD_TYPE_ID " & _
",MeasurementData.PRIOR_PERIOD_START " & _
",MeasurementData.PERIOD_YEAR " & _
",MeasurementData.FIRST_PAY_WEEK " & _
",MeasurementData.LAST_PAY_WEEK " & _
",IIf(IsNull(Payroll_History.HOURS), 0, Payroll_History.HOURS) AS HOURS "
stringSQL_3 = stringSQL_3 & _
"FROM Payroll_History " & _
"RIGHT JOIN ( " & _
"SELECT BaseData.EMPLOYEE_NUMBER " & _
",BaseData.FLSA " & _
",BaseData.STARTING_HIRE_DATE " & _
",BaseData.VARIABLE_EMPLOYEE " & _
",BaseData.START_PERIOD " & _
",BaseData.END_PERIOD " & _
",BaseData.PERIOD_TYPE_ID " & _
",BaseData.PRIOR_PERIOD_START " & _
",BaseData.PERIOD_YEAR " & _
",DateAdd('d', 3, DateAdd('ww', - 52, DateAdd('d', - 2, DateAdd('ww', DateDiff('ww', 0, DateAdd('ww', IIf(DatePart('w', " & _
"BaseData.END_PERIOD) >= 5, 0, - 1), BaseData.END_PERIOD)), 0)))) AS FIRST_PAY_WEEK " & _
",DateAdd('d', - 2, DateAdd('ww', DateDiff('ww', 0, DateAdd('ww', IIf(DatePart('w', BaseData.END_PERIOD) >= 5, 0, - 1), " & _
"BaseData.END_PERIOD)), 0)) AS LAST_PAY_WEEK "
stringSQL_3 = stringSQL_3 & _
"FROM ( " & _
"SELECT ElgEmployees.EMPLOYEE_NUMBER " & _
",ElgEmployees.FLSA " & _
",ElgEmployees.STARTING_HIRE_DATE " & _
",ElgEmployees.VARIABLE_EMPLOYEE " & _
",IIF(ElgEmployees.VARIABLE_EMPLOYEE = No, DateSerial(" & DatePickerYear & " - 1, 4, 1), " & _
"IIF(PeriodInfo.PRIOR_PERIOD_START IS NOT NULL, DateSerial(" & DatePickerYear & " - 1, DatePart('m', " & _
"PeriodInfo.PRIOR_PERIOD_START), 1), DateSerial(" & DatePickerYear & " - 1, DatePart('m', ElgEmployees.STARTING_HIRE_DATE), 1))) AS START_PERIOD " & _
",IIF(ElgEmployees.VARIABLE_EMPLOYEE = No, DateAdd('d', - 1, DateSerial(" & DatePickerYear & ", 4, 1)), DateAdd('d', - 1, " & _
"IIF(PeriodInfo.PRIOR_PERIOD_START IS NOT NULL, DateSerial(" & DatePickerYear & ", DatePart('m', PeriodInfo.PRIOR_PERIOD_START), 1), " & _
"DateSerial(" & DatePickerYear & ", DatePart('m', ElgEmployees.STARTING_HIRE_DATE), 1)))) AS END_PERIOD " & _
"," & DatePickerYear & " AS PERIOD_YEAR " & _
",PeriodInfo.PERIOD_TYPE_ID " & _
",PeriodInfo.PRIOR_PERIOD_START "
stringSQL_3 = stringSQL_3 & _
"FROM ( " & _
"SELECT Employee.EMPLOYEE_NUMBER " & _
",Employee.FLSA " & _
",Employee.VARIABLE_EMPLOYEE " & _
",IIf(Day(Employee.CURRENT_HIRE_DATE) = 1, Employee.CURRENT_HIRE_DATE, DateSerial(DatePart('yyyy', DateAdd('m', 1, " & _
"Employee.CURRENT_HIRE_DATE)), DatePart('m', DateAdd('m', 1, Employee.CURRENT_HIRE_DATE)), 1)) AS STARTING_HIRE_DATE "
stringSQL_3 = stringSQL_3 & _
"FROM Employee " & _
"WHERE ( " & _
"((Employee.EMPLOYEE_NUMBER)" & EmployeeNumberText & ") " & _
"AND ((IIf(Day(Employee.CURRENT_HIRE_DATE) = 1, Employee.CURRENT_HIRE_DATE, DateSerial(DatePart('yyyy', DateAdd('m', 1, " & _
"Employee.CURRENT_HIRE_DATE)), DatePart('m', DateAdd('m', 1, Employee.CURRENT_HIRE_DATE)), 1))) < DateAdd('y', - 1, DATE ())) " & _
"AND ((Employee.CURRENT_TERMINATION_DATE) IS NULL) " & _
") " & _
") AS ElgEmployees " & _
"INNER JOIN ( " & _
"SELECT Employee.EMPLOYEE_NUMBER " & _
",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, NULL, Measurement_Period_History.PERIOD_YEAR) AS PRIOR_PERIOD_YEAR " & _
",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, IIF(Employee.VARIABLE_EMPLOYEE = No, 2, 0), " & _
"IIF(Employee.VARIABLE_EMPLOYEE = No, 2, IIF(Measurement_Period_History.PERIOD_END < Employee.CURRENT_HIRE_DATE, 0, " & _
"IIF(Measurement_Period_History.AVERAGE_HOURS >= 30, 1, 0)))) AS PERIOD_TYPE_ID " & _
",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, NULL, IIF(Measurement_Period_History.PERIOD_END < " & _
"Employee.CURRENT_HIRE_DATE, NULL, Measurement_Period_History.PERIOD_END)) AS PRIOR_PERIOD_END " & _
",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, NULL, IIF(Measurement_Period_History.PERIOD_END < " & _
"Employee.CURRENT_HIRE_DATE, NULL, Measurement_Period_History.PERIOD_START)) AS PRIOR_PERIOD_START "
stringSQL_3 = stringSQL_3 & _
"FROM Measurement_Period_History " & _
"RIGHT JOIN Employee ON Measurement_Period_History.EMPLOYEE_NUMBER = Employee.EMPLOYEE_NUMBER " & _
"WHERE ((Employee.CURRENT_TERMINATION_DATE) Is Null) AND (Employee.EMPLOYEE_NUMBER" & EmployeeNumberText & ") " & _
"GROUP BY Employee.EMPLOYEE_NUMBER " & _
",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, NULL, Measurement_Period_History.PERIOD_YEAR) " & _
",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, IIF(Employee.VARIABLE_EMPLOYEE = No, 2, 0), " & _
"IIF(Employee.VARIABLE_EMPLOYEE = No, 2, IIF(Measurement_Period_History.PERIOD_END < Employee.CURRENT_HIRE_DATE, 0, " & _
"IIF(Measurement_Period_History.AVERAGE_HOURS >= 30, 1, 0)))) " & _
",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, NULL, IIF(Measurement_Period_History.PERIOD_END < " & _
"Employee.CURRENT_HIRE_DATE, NULL, Measurement_Period_History.PERIOD_END)) " & _
",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, NULL, IIF(Measurement_Period_History.PERIOD_END < " & _
"Employee.CURRENT_HIRE_DATE, NULL, Measurement_Period_History.PERIOD_START)) " & _
") AS PeriodInfo ON ElgEmployees.EMPLOYEE_NUMBER = PeriodInfo.EMPLOYEE_NUMBER " & _
") AS BaseData " & _
") AS MeasurementData ON Payroll_History.EMPLOYEE_NUMBER = MeasurementData.EMPLOYEE_NUMBER " & _
"WHERE ((Payroll_History.PAY_DATE) Is Null) " & _
"OR ( " & _
"(Payroll_History.PAY_DATE) >= MeasurementData.FIRST_PAY_WEEK " & _
"AND (Payroll_History.PAY_DATE) <= MeasurementData.LAST_PAY_WEEK " & _
") " & _
") AS FinalData "
stringSQL_3 = stringSQL_3 & _
"WHERE LAST_PAY_WEEK < Now() " & _
"GROUP BY EMPLOYEE_NUMBER " & _
",FLSA " & _
",STARTING_HIRE_DATE " & _
",VARIABLE_EMPLOYEE " & _
",START_PERIOD " & _
",END_PERIOD " & _
",PERIOD_TYPE_ID " & _
",PRIOR_PERIOD_START " & _
",PERIOD_YEAR " & _
",FIRST_PAY_WEEK " & _
",LAST_PAY_WEEK "
Debug.Print stringSQL_3
Debug.Print Len(stringSQL_3)
dBase.Execute string_3
Isn't your SQL in stringSQL_3? What's in the other string (string_3)? That might the issue, you are trying to execute the wrong string?
Related
Number format of a query in access vba
I created a query in access using VBA, and I want to change the number format of the last two columns to * #.##0,00;* (#.##0,00);* -00 I didn't find how to do it. Here's what I did: Sub orcMensal(periodo As String) Dim rs As QueryDef Dim SQL1 As String Dim ano As String Dim tabelaCA As String Dim tabelaRealizado As String tabelaCA = "[ORCAMENTO_CA_20" & Right(periodo, 2) & "]" tabelaRealizado = "[" & periodo & "_MA]" SQL1 = "SELECT " & tabelaCA & ".coger AS [Codigo Coger], " _ & tabelaCA & ".[Descricao], " _ & tabelaCA & ".[" & periodo & "] AS [Orcamento], " _ & tabelaRealizado & ".[REALIZADO] AS [Realizado], " _ & "(" & tabelaRealizado & ".[REALIZADO] - " & tabelaCA & ".[" & periodo & "]) AS [Diferenca] " _ & "FROM " & tabelaCA _ & " INNER JOIN " & tabelaRealizado & " ON" _ & " (" & tabelaCA & ".[coger] = " & tabelaRealizado & ".[COD_ES] AND " _ & tabelaCA & ".[Titular] = " & tabelaRealizado & ".[TITULAR]);" Debug.Print SQL1 Set rs = CurrentDb.CreateQueryDef(periodo, SQL1) DoCmd.OpenQuery rs.Name, , acReadOnly End Sub
I used the Format function in the SQL query, like #Wayne G. Dunn commented. The format that I wanted was 1) 1000000,5023 >>>> 1.000.000,50; 2) -5200000,7833 >>>> (5.200.000,78); 3) 0 >>>> - So I used '#,##0.00; (#,##0.00); -' in the format function, here's how the query looks: SQL1 = "SELECT " & tabelaCA & ".coger AS [Codigo Coger], " _ & tabelaCA & ".[Descricao], " _ & "Format(" & tabelaCA & ".[" & periodo & "], '#,##0.00; (#,##0.00); -') AS [Orcamento], FORMAT(" _ & tabelaRealizado & ".[REALIZADO], '#,##0.00; (#,##0.00); -') AS [Realizado], " _ & "Format((" & tabelaRealizado & ".[REALIZADO] - " & tabelaCA & ".[" & periodo & "]), '#,##0.00; (#,##0.00); -') AS [Diferenca] " _ & "FROM " & tabelaCA _ & " LEFT JOIN " & tabelaRealizado & " ON" _ & " (" & tabelaCA & ".[coger] = " & tabelaRealizado & ".[COD_ES] AND " _ & tabelaCA & ".[Titular] = " & tabelaRealizado & ".[TITULAR])" _ & "WHERE " & tabelaCA & ".coger <> '';"
Parameterizing query says "Mixing named and unnamed parameters is not allowed."
I want to parameterized my query so instead of this code (This is a working code, I didn't put all the codes here because we have nothing to do with it.) MySQL_Query = "SET #row_number = 0; " _ & "SELECT hardware_add " _ & "FROM (" _ & "SELECT " _ & "#row_number:=#row_number + 1 AS num, " _ & "hardware_add AS hardware_add " _ & "FROM teller_info " _ & "WHERE status = 'Disconnected'" _ & ") AS sub_query " _ & "WHERE num = " & counter & ";" Console.WriteLine(MySQL_Query) Dim MySQL_CMD As New MySqlCommand(MySQL_Query, MysqlConn) MySQL_CMD.Connection.Open() I changed it to this. MySQL_Query = "SET #row_number = 0; " _ & "SELECT hardware_add " _ & "FROM (" _ & "SELECT " _ & "#row_number:=#row_number + 1 AS num, " _ & "hardware_add AS hardware_add " _ & "FROM teller_info " _ & "WHERE status = 'Disconnected'" _ & ") AS sub_query " _ & "WHERE num = ?;" Console.WriteLine(MySQL_Query) Dim MySQL_CMD As New MySqlCommand(MySQL_Query, MysqlConn) MySQL_CMD.Connection.Open() MySQL_CMD.Parameters.Add(New MySqlParameter("counter", counter)) The error says MySql.Data.MySqlClient.MySqlException (0x80004005): Mixing named and unnamed parameters is not allowed. My question is how can I properly parameterize that query?
You have to give a name to that param, doing so: MySQL_Query = "SET #row_number = 0; " _ & "SELECT hardware_add " _ & "FROM (" _ & "SELECT " _ & "#row_number:=#row_number + 1 AS num, " _ & "hardware_add AS hardware_add " _ & "FROM teller_info " _ & "WHERE status = 'Disconnected'" _ & ") AS sub_query " _ & "WHERE num = #counter;" Console.WriteLine(MySQL_Query) Dim MySQL_CMD As New MySqlCommand(MySQL_Query, MysqlConn) MySQL_CMD.Connection.Open() MySQL_CMD.Parameters.Add(New MySqlParameter("#counter", counter))
How to insert correct value from DropDownList into Database Table
I have a form where an admin can add a company to a database table called 'Company' In this form there is a Drop Down list to select whether a company is a Scholarship Company, there are two options 'Yes' and 'No'. I have created a table to pull these options from called 'YesNo'. When I select Yes in the drop down list and hit submit it updates the table so the cell for ScholarshipCompany is '1' and not 'Yes'. This works fine in the edit form I have which just updates the company though I am having trouble with the add company form which inserts a new entry into the table. Please see code below; Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Session("Name") = Nothing Then Response.Redirect("Login.aspx") End If Session("Name") = Session("Name") Session("Username") = Session("Username") Try If Not IsPostBack Then Dim locationList As OleDbDataReader = Database.DoSQLReturnDataReader("SELECT [{Location}].ID, [{Location}].Location FROM [{Location}] ORDER BY [{Location}].ID") ddlLocation.DataSource = locationList ddlLocation.DataTextField = "Location" ddlLocation.DataValueField = "ID" ddlLocation.DataBind() locationList.close() Dim statusList As OleDbDataReader = Database.DoSQLReturnDataReader("SELECT * FROM EmployerStatus ORDER BY ID ASC") ddlStatus.DataSource = statusList ddlStatus.DataTextField = "Status" ddlStatus.DataValueField = "ID" ddlStatus.DataBind() statusList.close() Dim yesnoList As OleDbDataReader = Database.DoSQLReturnDataReader("SELECT * FROM YesNo ORDER BY ID DESC") ddlYesno.DataSource = yesnoList ddlYesno.DataTextField = "Option" ddlYesno.DataValueField = "Option" ddlYesno.DataBind() yesnoList.close() End If Catch ex As Exception Response.Redirect("index.aspx?Yr=" & Replace(Request.QueryString("Yr"), "'", "''")) End Try End Sub Protected Sub BtnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSubmit.Click Dim strSql As String = "INSERT INTO Company(" & _ "CompanyName, " & _ "URL, " & _ "Location, " & _ "EmployerStatus, " & _ "Address, " & _ "ScholarshipCompany, " & _ "ScholarshipNotes, " & _ "Town, " & _ "County, " & _ "Blurb, " & _ "InterviewTips, " & _ "HRContactForename, " & _ "HRContactSurname, " & _ "HREmail, " & _ "Telephone, " & _ "[Password], " & _ "Fax, " & _ "HRContactForename2, " & _ "HRContactSurname2, " & _ "HREmail2, " & _ "Telephone2, " & _ "HRContactForename3, " & _ "HRContactSurname3, " & _ "HREmail3, " & _ "Telephone3, " & _ "Postcode) " & _ "VALUES " & _ "( " & _ "'" & TxtBoxCompanyName.Text.Replace("'", "''") & "', " & _ "'" & TxtBoxURL.Text.Replace("'", "''") & "', " & _ "'" & Replace((ddlLocation.SelectedIndex + 1), "'", "''") & "', " & _ "'" & Replace((ddlStatus.SelectedIndex + 1), "'", "''") & "', " & _ "'" & TxtAreaAddress.InnerText.Replace("'", "''") & "', " & _ "'" & Replace((ddlYesno.SelectedIndex), "'", "''") & "', " & _ "'" & TxtBoxSchol.Text.Replace("'", "''") & "', " & _ "'" & TxtBoxTown.Text.Replace("'", "''") & "', " & _ "'" & TxtBoxCounty.Text.Replace("'", "''") & "', " & _ "'" & "" & "', " & _ "'" & "" & "', " & _ "'" & "" & "', " & _ "'" & "" & "', " & _ "'" & "" & "', " & _ "'" & "" & "', " & _ "'" & "" & "', " & _ "'" & "" & "', " & _ "'" & "" & "', " & _ "'" & "" & "', " & _ "'" & "" & "', " & _ "'" & "" & "', " & _ "'" & "" & "', " & _ "'" & "" & "', " & _ "'" & "" & "', " & _ "'" & "" & "', " & _ "'" & TxtBoxPostcode.Text.Replace("'", "''") & "'" & _ ")" Dim updateCompanyDetails As OleDbDataReader = Database.DoSQLReturnDataReader(strSql) updateCompanyDetails.Close() System.Threading.Thread.Sleep("1000")
Instead of Replace((ddlLocation.SelectedIndex + 1), "'", "''") & "', " & _ "'" & Replace((ddlStatus.SelectedIndex + 1), "'", "''") & "', " & _ try Replace((ddlLocation.SelectedValue + 1), "'", "''") & "', " & _ "'" & Replace((ddlStatus.SelectedValue + 1), "'", "''") & "', " & _ or SelectedText
I'm trying to UPDATE my table in Access VBA
If Me.dcBox & "" = "" Then CurrentDb.Execute "UPDATE worklogData " & _ " SET [Submitter] = '" & Me.subBox & "'" & _ ", [Section] = '" & Me.secBox & "'" & _ ", [Received By] = '" & Me.rbBox & "'" & _ ", [Date Received] = """ & _ ", [Serial Number] = '" & Me.snBox & "'" & _ ", [Problem] = '" & Me.probBox & "'" & _ ", [Computer Name] = '" & Me.cnBox & "'" & _ ", [MAC] = '" & Me.macBox & "'" & _ ", [Solution] = '" & Me.solBox & "'" & _ ", [Completed By] = '" & Me.cbBox & "'" & _ ", [Date Completed] = """ & _ " WHERE [Order] = " & Me.orderBox.Tag & "" Else CurrentDb.Execute "UPDATE worklogData " & _ " SET [Submitter] = '" & Me.subBox & "'" & _ ", [Section] = '" & Me.secBox & "'" & _ ", [Received By] = '" & Me.rbBox & "'" & _ ", [Date Received] = #" & Me.drBox & "#" & _ ", [Serial Number] = '" & Me.snBox & "'" & _ ", [Problem] = '" & Me.probBox & "'" & _ ", [Computer Name] = '" & Me.cnBox & "'" & _ ", [MAC] = '" & Me.macBox & "'" & _ ", [Solution] = '" & Me.solBox & "'" & _ ", [Completed By] = '" & Me.cbBox & "'" & _ ", [Date Completed] = #" & Me.dcBox & "#" & _ " WHERE [Order] = " & Me.orderBox.Tag & "" End If I'm trying to UPDATE my table in Access VBA, but I found it causes Runtime Error: 3075 'Syntax error in date in query expression '#' when I try to UPDATE [Date Completed] field from what was blank previously. And I've set Me.probBox as some text value which was previously blank, but [Problem] field doesn't be updated. What is surprising is there is no error coming out for this one.
Try this CurrentDb.Execute "UPDATE worklogData " & _ " SET [Submitter] = '" & Me.subBox & "'" & _ ", [Section] = '" & Me.secBox & "'" & _ ", [Received By] = '" & Me.rbBox & "'" & _ ", [Date Received] = """"" & _ ", [Serial Number] = '" & Me.snBox & "'" & _ ", [Problem] = '" & Me.probBox & "'" & _ ", [Computer Name] = '" & Me.cnBox & "'" & _ ", [MAC] = '" & Me.macBox & "'" & _ ", [Solution] = '" & Me.solBox & "'" & _ ", [Completed By] = '" & Me.cbBox & "'" & _ ", [Date Completed] = """"" & _ " WHERE [Order] = " & Me.orderBox.Tag & ""
End of statement error
I am trying to assemble an insert statement with multiple line breaks. The insertion includes both numerical as well as textual data. I keep getting an error, and I cannot find the where I am syntactically wrong. Can anyone see me error? CurrentDb.Execute "INSERT INTO tblCustParts (CustPartNum,CustomerID,Alloy,Temper,Finish,FormType,Gauge,GaugeAimPlus,GaugeAimMinus," & _ "GaugeGuarPlus,GaugeGuarMinus,Width,WidthAimPlus,WidthAimMinus,WidthGuarPlus,WidthGuarMinus,Length,LengthAimPlus,LengthAimMinus," & _ "LengthGuarPlus,LengthGuarMinus,Diameter,DiameterAimPlus,DiameterAimMinus,DiameterGuarPlus,DiameterGuarMinus,CircleShear,IDMin," & _ "ODMin,ODMax,LabelAs,ProduceAs,ShopPaperNotes,EyeOrientation,CoreType,PackingNotes,AQ,PaperInterleave,HeatTreatedSkids,HasRecipeFlag," & _ "QRRExists,TensionLevel,MSKCoreSpec,ChemCertsReq,PhysCertsReq,PhysAndChemCertsReq,AAStandard,ASTM_B209_10)" & _ "VALUES('" & Part & "', " & Me.Customer & ", '" & Alloy & "', '" & Temper & "', '" & Finish & "', '" & FormType & "', " & Me.Gauge & ", "" & _ " & Me.Gauge & ", " & Me.GaugeAimPlus & ", " & Me.GaugeAimMinus & ", " & Me.GaugeGuarPlus & ", " & Me.GaugeGuarMinus & ", "" & _ " & Me.WidthEntry & ", " & Me.WidthAimPlus & ", " & Me.WidthAimMinus & ", " & Me.WidthGuarPlus & ", " & Me.WidthGuarMinus & ", "" & _ " & Me.LengthEntry & ", " & Me.LengthAimPlus & ", " & Me.LengthAimMinus & ", " & Me.LengthGuarPlus & ", " & Me.LengthGuarMinus & ", "" & _ " & Me.Diameter & ", " & Me.DiameterAimPlus & ", " & Me.DiameterAimMinus & ", " & Me.DiameterGuarPlus & ", "" & _ " & Me.DiameterGuarMinus & ", " & Me.CS & ", " & Me.IDMin & ", " & Me.ODMin & ", " & Me.ODMax & ", '" & Me.LabelAs & "', '"" & _ " & Me.ProduceAs & "', '" & Me.ShopPaperNotes & "', '" & Me.EyeOrientation & "', '" & Me.CoreType & "', '" & Me.PackingNotes & "', "" & _ " & Me.AQ & ", " & Me.PaperInterleave & ", " & Me.HeatTreatedSkids & ", " & Me.HasRecipeFlag & ", " & Me.QRRExists & ", "" & _ " & Me.TensionLevel & ", " & Me.MSKCoreSpec & ", " & Me.ChemCertsReq & ", " & Me.PhysCertsReq & ", " & Me.PhysAndChemCertsReq & ", "" & _ " & Me.AAStandard & ", " & Me.ASTM_B209_10 & ")"
I am not sure what the extra quotes and ampersands are for, but they are causing the problem. They occur at the start and end of each Value line. It is best to put your string in a separate variable, and then execute that, it is easier to spot problems. You might also like to consider parameters, they will make your life easier in this case. ssql = "INSERT INTO tblCustParts (CustPartNum,CustomerID,Alloy,Temper,Finish,FormType,Gauge,GaugeAimPlus,GaugeAimMinus," & _ "GaugeGuarPlus,GaugeGuarMinus,Width,WidthAimPlus,WidthAimMinus,WidthGuarPlus,WidthGuarMinus,Length,LengthAimPlus,LengthAimMinus," & _ "LengthGuarPlus,LengthGuarMinus,Diameter,DiameterAimPlus,DiameterAimMinus,DiameterGuarPlus,DiameterGuarMinus,CircleShear,IDMin," & _ "ODMin,ODMax,LabelAs,ProduceAs,ShopPaperNotes,EyeOrientation,CoreType,PackingNotes,AQ,PaperInterleave,HeatTreatedSkids,HasRecipeFlag," & _ "QRRExists,TensionLevel,MSKCoreSpec,ChemCertsReq,PhysCertsReq,PhysAndChemCertsReq,AAStandard,ASTM_B209_10)" & _ "VALUES('" & Part & "', " & Me.Customer & ", '" & Alloy & "', '" & Temper & "', '" & Finish & "', '" & FormType & "', " & Me.Gauge & ", " & _ Me.Gauge & ", " & Me.GaugeAimPlus & ", " & Me.GaugeAimMinus & ", " & Me.GaugeGuarPlus & ", " & Me.GaugeGuarMinus & ", " & _ Me.WidthEntry & ", " & Me.WidthAimPlus & ", " & Me.WidthAimMinus & ", " & Me.WidthGuarPlus & ", " & Me.WidthGuarMinus & ", " & _ Me.LengthEntry & ", " & Me.LengthAimPlus & ", " & Me.LengthAimMinus & ", " & Me.LengthGuarPlus & ", " & Me.LengthGuarMinus & ", " & _ Me.Diameter & ", " & Me.DiameterAimPlus & ", " & Me.DiameterAimMinus & ", " & Me.DiameterGuarPlus & ", " & _ Me.DiameterGuarMinus & ", " & Me.CS & ", " & Me.IDMin & ", " & Me.ODMin & ", " & Me.ODMax & ", '" & Me.LabelAs & "', '" & _ Me.ProduceAs & "', '" & Me.ShopPaperNotes & "', '" & Me.EyeOrientation & "', '" & Me.CoreType & "', '" & Me.PackingNotes & "', " & _ Me.AQ & ", " & Me.PaperInterleave & ", " & Me.HeatTreatedSkids & ", " & Me.HasRecipeFlag & ", " & Me.QRRExists & ", " & _ Me.TensionLevel & ", " & Me.MSKCoreSpec & ", " & Me.ChemCertsReq & ", " & Me.PhysCertsReq & ", " & Me.PhysAndChemCertsReq & ", " & _ Me.AAStandard & ", " & Me.ASTM_B209_10 & ")" Dim db As database Set db = CurrentDB db.Execute ssql dbFailOnError Last record inserted ID Set rs = db.OpenRecordset("select ##identity as id") LastID = rs("id") Parameter example: ssql = "INSERT INTO Table1 (Atext,Anumber) Values (#AText,#Anumber)" Dim qdf As QueryDef Set qdf = CurrentDb.CreateQueryDef("", ssql) qdf.Parameters("#atext") = "abc" qdf.Parameters("#Anumber") = 1 qdf.Execute dbFailOnError Last record inserted ID qdf.SQL = "select ##identity as id" Set rs = qdf.OpenRecordset LastID = rs("id")