I have a SQL command that I'm running in VB.NET... I've written this type of command countless times, however, I'm getting an error when trying to process the command below:
Dim VIconn As New SqlConnection("Data Source=MBSRVERP01;Initial Catalog=MBUk;Integrated Security=True")
Dim CMDoperation As SqlCommand = New SqlCommand()
CMDoperation.Connection = VIconn
CMDoperation.CommandText = ("INSERT INTO PRJCREATION.dbo.OPERATION_CO (WORKORDER_TYPE, WORKORDER_BASE_ID, WORKORDER_LOT_ID, WORKORDER_SPLIT_ID, WORKORDER_SUB_ID, SEQUENCE_NO, RESOURCE_ID, SETUP_HRS, " _
& "RUN, RUN_TYPE, LOAD_SIZE_QTY, RUN_HRS, MOVE_HRS, TRANSIT_DAYS, SERVICE_ID, SCRAP_YIELD_PCT, SCRAP_YIELD_TYPE, FIXED_SCRAP_UNITS, MINIMUM_MOVE_QTY, CALC_START_QTY, " _
& "CALC_END_QTY, COMPLETED_QTY, DEVIATED_QTY, ACT_SETUP_HRS, ACT_RUN_HRS, STATUS, SETUP_COMPLETED, SERVICE_BEGIN_DATE, CLOSE_DATE, OPERATION_TYPE, DRAWING_ID, DRAWING_REV_NO, " _
& "OVERRIDE_QTYS, BEGIN_TRACEABILITY, CAPACITY_USAGE_MAX, CAPACITY_USAGE_MIN, TEST_ID, SPC_QTY, SCHED_START_DATE, SCHED_FINISH_DATE, COULD_FINISH_DATE, ISDETERMINANT, " _
& "SETUP_COST_PER_HR, RUN_COST_PER_HR, RUN_COST_PER_UNIT, BUR_PER_HR_SETUP, BUR_PER_HR_RUN, BUR_PER_UNIT_RUN, SERVICE_BASE_CHG, BUR_PERCENT_SETUP, BUR_PERCENT_RUN, " _
& "BUR_PER_OPERATION, EST_ATL_LAB_COST, EST_ATL_BUR_COST, EST_ATL_SER_COST, REM_ATL_LAB_COST, REM_ATL_BUR_COST, REM_ATL_SER_COST, ACT_ATL_LAB_COST, ACT_ATL_BUR_COST, " _
& "ACT_ATL_SER_COST, EST_TTL_MAT_COST, EST_TTL_LAB_COST, EST_TTL_BUR_COST, EST_TTL_SER_COST, REM_TTL_MAT_COST, REM_TTL_LAB_COST, REM_TTL_BUR_COST, REM_TTL_SER_COST, " _
& "ACT_TTL_MAT_COST, ACT_TTL_LAB_COST, ACT_TTL_BUR_COST, ACT_TTL_SER_COST, SPLIT_ADJUSTMENT, MILESTONE_ID, SCHEDULE_TYPE, MIN_SEGMENT_SIZE, PROTECT_COST, DRAWING_FILE, " _
& "DISPATCHED_QTY, SERVICE_MIN_CHG, VENDOR_ID, VENDOR_SERVICE_ID, SERVICE_PART_ID, LAST_DISP_DATE, LAST_RECV_DATE, WAREHOUSE_ID, ALLOCATED_QTY, FULFILLED_QTY, " _
& "LEAST_MIN_MOVE_QTY, MAX_GAP_PREV_OP, APPLY_CALENDAR, MAX_DOWNTIME, ACCUM_DOWNTIME, RUN_QTY_PER_CYCLE, USER_1, USER_2, USER_3, USER_4, USER_5, USER_6, USER_7, USER_8, " _
& "USER_9, USER_10, UDF_LAYOUT_ID, NUM_MEM_TO_SCHED, SERVICE_BUFFER, MILESTONE_SUB_ID, POST_MILESTONE, PROJ_MILESTONE_OP, WBS_CODE, WBS_START_DATE, WBS_END_DATE, " _
& "WBS_DURATION, MILESTONE_SEQ_NO, PRD_INSP_PLAN_ID, SETUP_INSPECT_REQ, RUN_INSPECT_REQ, STATUS_EFF_DATE, PRED_SUB_ID, PRED_SEQ_NO, SITE_ID, SCHED_CAPACITY_USAGE)" _
& "SELECT WORKORDER_TYPE, WORKORDER_BASE_ID + 'F', WORKORDER_LOT_ID, WORKORDER_SPLIT_ID, WORKORDER_SUB_ID, SEQUENCE_NO, RESOURCE_ID, SETUP_HRS, " _
& "RUN, RUN_TYPE, LOAD_SIZE_QTY, RUN_HRS, MOVE_HRS, TRANSIT_DAYS, SERVICE_ID, SCRAP_YIELD_PCT, SCRAP_YIELD_TYPE, FIXED_SCRAP_UNITS, MINIMUM_MOVE_QTY, CALC_START_QTY, " _
& "CALC_END_QTY, COMPLETED_QTY, DEVIATED_QTY, ACT_SETUP_HRS, ACT_RUN_HRS, 'R', SETUP_COMPLETED, SERVICE_BEGIN_DATE, NULL, OPERATION_TYPE, DRAWING_ID, DRAWING_REV_NO, " _
& "OVERRIDE_QTYS, BEGIN_TRACEABILITY, CAPACITY_USAGE_MAX, CAPACITY_USAGE_MIN, TEST_ID, SPC_QTY, NULL, NULL, NULL, ISDETERMINANT, " _
& "SETUP_COST_PER_HR, RUN_COST_PER_HR, RUN_COST_PER_UNIT, BUR_PER_HR_SETUP, BUR_PER_HR_RUN, BUR_PER_UNIT_RUN, SERVICE_BASE_CHG, BUR_PERCENT_SETUP, BUR_PERCENT_RUN, " _
& "BUR_PER_OPERATION, EST_ATL_LAB_COST, EST_ATL_BUR_COST, EST_ATL_SER_COST, REM_ATL_LAB_COST, REM_ATL_BUR_COST, REM_ATL_SER_COST, ACT_ATL_LAB_COST, ACT_ATL_BUR_COST, " _
& "ACT_ATL_SER_COST, EST_TTL_MAT_COST, EST_TTL_LAB_COST, EST_TTL_BUR_COST, EST_TTL_SER_COST, REM_TTL_MAT_COST, REM_TTL_LAB_COST, REM_TTL_BUR_COST, REM_TTL_SER_COST, " _
& "ACT_TTL_MAT_COST, ACT_TTL_LAB_COST, ACT_TTL_BUR_COST, ACT_TTL_SER_COST, SPLIT_ADJUSTMENT, MILESTONE_ID, SCHEDULE_TYPE, MIN_SEGMENT_SIZE, PROTECT_COST, DRAWING_FILE, " _
& "DISPATCHED_QTY, SERVICE_MIN_CHG, VENDOR_ID, VENDOR_SERVICE_ID, SERVICE_PART_ID, LAST_DISP_DATE, LAST_RECV_DATE, WAREHOUSE_ID, ALLOCATED_QTY, FULFILLED_QTY, " _
& "LEAST_MIN_MOVE_QTY, MAX_GAP_PREV_OP, APPLY_CALENDAR, MAX_DOWNTIME, ACCUM_DOWNTIME, RUN_QTY_PER_CYCLE, USER_1, USER_2, USER_3, USER_4, USER_5, USER_6, USER_7, USER_8, " _
& "USER_9, USER_10, UDF_LAYOUT_ID, NUM_MEM_TO_SCHED, SERVICE_BUFFER, MILESTONE_SUB_ID, POST_MILESTONE, PROJ_MILESTONE_OP, WBS_CODE, WBS_START_DATE, WBS_END_DATE, " _
& "WBS_DURATION, MILESTONE_SEQ_NO, PRD_INSP_PLAN_ID, SETUP_INSPECT_REQ, RUN_INSPECT_REQ, STATUS_EFF_DATE, PRED_SUB_ID, PRED_SEQ_NO, SITE_ID, SCHED_CAPACITY_USAGE" _
& "FROM MBUK.dbo.OPERATION db2 " _
& "WHERE (db2.WORKORDER_BASE_ID = '" & rw.Cells(8).Value & "')")
CMDoperation.ExecuteNonQuery()
The error I get is {"Incorrect syntax near '.'."}
The SQL command is simply inserting data from a table in database A into a table in database B... Nothing complex.
Can anyone spot where I'm going wrong here?
Thanks
Here my 2 cents:
pay attention to VB concatenation, the end of the line is strictly attached to the following one, so in your code
& "WBS_DURATION, MILESTONE_SEQ_NO, PRD_INSP_PLAN_ID, SETUP_INSPECT_REQ, RUN_INSPECT_REQ, STATUS_EFF_DATE, PRED_SUB_ID, PRED_SEQ_NO, SITE_ID, SCHED_CAPACITY_USAGE" _
& "FROM MBUK.dbo.OPERATION db2 " _
vb ends concatenating words in this way: SCHED_CAPACITY_USAGEFROM
so add an extra space at the end of each line you concatenate.
can anyone help me where am I doing wrong.. im building an update statement with multiple columns to update MSQL with VBA and values from Excel. heres my simple code, Im parsing variable in function to execute SQL command (I have a table name "People" and I have define all the column) below:
Function GetUpdateTextSQL(PIC As String,
Customer As String,
DOB As Date,
Rank As String,
Organization As String,
Status As String,
Gender As String,
Religion As String,
Hobby As String,
CreatedBy1 As String,
CreatedOn1 As Date,
ChangedBy1 As String,
ChangedOn1 As Date,
PeopleID As Integer) As String
SQLStr = _
"UPDATE People" & _
"SET PIC = " & _
"'" & PIC & "', Customer = '" & Customer & "'," & _
"DOB = '" & Format(DOB, "yyyy-mm-dd") & "', Rank = '" & Rank & "'," & _
"Organization = '" & Organization & "',Status = '" & Status & "', Gender = '" & Gender & "'," & _
"Religion = '" & Religion & "', Hobby = '" & Hobby & "'," & _
"CreatedBy = '" & CreatedBy1 & "', CreatedOn = '" & CreatedOn1 & "'," & _
"ChangedBy = '" & ChangedBy1 & "', ChangedOn = '" & ChangedOn1 & "'" & _
"WHERE PeopleID = & PeopleID &;"
GetUpdateTextSQL = SQLStr
//And here Im inserting and executing command below to get values from excel:
For Each r In Range("A45", Range("A45").End(xlDown))
CmdForSave.CommandText = _
GetUpdateTextSQL( _
r.Offset(0, 1).Value, r.Offset(0, 2).Value, _
r.Offset(0, 3).Value, _
r.Offset(0, 4).Value, r.Offset(0, 5).Value, _
r.Offset(0, 6).Value, r.Offset(0, 7).Value, _
r.Offset(0, 8).Value, r.Offset(0, 9).Value, _
r.Offset(0, 10).Value, r.Offset(0, 11).Value, _
r.Offset(0, 12).Value, r.Offset(0, 13).Value, _
r.Offset(0, 0).Value)
CmdForSave.Execute
Next r
The error I get is 'Incorrect Syntax near 'PIC'
What is wrong with my code? Iam using MS SQL Express (SQL 2012)
Thanks All,
I have figured it out why the problem is with space on the update SQL Statement.
The right statement using VBA (space before SET, space before WHERE):
SQLStr = _
"UPDATE People" & _
" SET PIC = '" & PIC & "', Customer = '" & Customer & "'" & _
" WHERE PeopleID = '" & PeopleID & "';"
I keep getting a syntax error when I run a debug on the following code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles add.Click
Dim cmd As New OleDb.OleDbCommand
If Not cnn.State = ConnectionState.Open Then
'Open Connection if not yet Open
cnn.Open() End If
cmd.Connection = cnn
If Me.sn.Tag & "" = "" Then
cmd.CommandText = "INSERT INTO First_Year(Student_No,Lastname,Firstname,Year_Level,Enroll_Date,SEX,SY,CIVIL_STATUS,Religion,Birthdate,TEL_NO,Father,Occupation_F,Mother,Occupation_m,School Last Attended,Address School,Middle_Name)" +
"VALUES ('" & Me.sn.Text & "','" & Me.fn.Text & "','" & Me.ln.Text & "' ,'" & Me.Year.Text & "','" & Me.ed.Value & "','" & Me.s.Text & "','" & Me.sy.Text & "','" & Me.cs.Text & "','" & Me.re.Text & "'," & Me.cn.Text & ",'" & Me.bd.Value & "','" & Me.fa.Text & "','" & Me.fo.Text & "','" & Me.ma.Text & "','" & Me.mo.Text & "','" & Me.lad.Text & "','" & Me.ad.Text & "','" & Me.mi.Text & "')"
cmd.ExecuteNonQuery()
Can some please point out to me whats wrong with it?
You have some fields name that contains spaces. To use these fields names you need to enclose them in square brackets
cmd.CommandText = "INSERT INTO First_Year " & _
"(Student_No,Lastname,Firstname,Year_Level,Enroll_Date,SEX, " & _
"SY,CIVIL_STATUS,Religion,Birthdate,TEL_NO,Father,Occupation_F,Mother, " &
"Occupation_m,[School Last Attended],[Address School],Middle_Name) " &
"...... "
Said that, remember that string concatenations like yours lead to Sql Injection and problem in parsing strings that contains quotes (O'Brien) or decimal numbers or date
Search about Sql Injection and Parameterized queries
A parameterized approach to your query would be
cmd.CommandText = "INSERT INTO First_Year " & _
"(Student_No,Lastname,Firstname,Year_Level,Enroll_Date,SEX, " & _
"SY,CIVIL_STATUS,Religion,Birthdate,TEL_NO,Father,Occupation_F,Mother, " &
"Occupation_m,[School Last Attended],[Address School],Middle_Name) " &
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
cmd.Parameters.AddWithValue("#p1", Me.sn.Text)
cmd.Parameters.AddWithValue("#p2", Me.fn.Text)
... and so on for the remainder 16 parameters placeholders
... respecting their position and converting to the appropriate datatype
you need to remove the space here (in your query) :
......School Last Attended,Address School.......
or write it like this :
..........[School Last Attended],[Address School]..........
This question already has an answer here:
Access VBA - Identifying text
(1 answer)
Closed 8 years ago.
I have a form and subform. Im trying to allow updates in the subform with the use of buttons. However my code is giving me.
KEY_ID is text, the other two are number types.
"Syntax error in query expression "5", ROOM=5. DRAWER=55 (this is the new value i tried to change it to) WHERE KEY_ID=5'.
This is an image of my form: http://jumpshare.com/b/17A7Pr
This is what im trying:
Private Sub cmdAdd_Click()
If Me.keyID.Tag & "" = "" Then
CurrentDb.Execute "INSERT INTO KEYS(KEY_ID, ROOM, DRAWER)" & _
" VALUES('" & Me.keyID & "'," & Me.roomID & "," & Me.drawerID & ")"
subKey.Form.Requery
Else
CurrentDb.Execute "UPDATE KEYS " & _
" SET KEY_ID=" & Chr(39) & keyID & Chr(39) & _
", ROOM=" & Me.roomID & _
", DRAWER=" & Me.drawerID & _
" WHERE KEY_ID=" & Me.keyID.Tag
Debug.Print KEY_ID
End If
Solution:
CurrentDb.Execute "UPDATE KEYS " & _
" SET KEY_ID=" & Me.keyID & _
", ROOM=" & Me.roomID & _
", DRAWER=" & Me.drawerID & _
" WHERE KEY_ID=" & Chr(39) & keyID.Tag & Chr(39)
Solution:
CurrentDb.Execute "UPDATE KEYS " & _
" SET KEY_ID=" & Me.keyID & _
", ROOM=" & Me.roomID & _
", DRAWER=" & Me.drawerID & _
" WHERE KEY_ID=" & Chr(39) & keyID.Tag & Chr(39)
Try the code without Chr(39).. This should work.
Private Sub cmdAdd_Click()
If Me.keyID.Tag & "" = "" Then
CurrentDb.Execute "INSERT INTO KEYS(KEY_ID, ROOM, DRAWER)" & _
" VALUES('" & Me.keyID & "'," & Me.roomID & "," & Me.drawerID & ")"
subKey.Form.Requery
Else
CurrentDb.Execute "UPDATE KEYS " & _
" SET KEY_ID=" & keyID & _
", ROOM=" & Me.roomID & _
", DRAWER=" & Me.drawerID & _
" WHERE KEY_ID=" & Me.keyID.Tag
Debug.Print KEY_ID
End If