Related
I look at the code and checking again and again, really don't know what happen to my code and getting error below -
strSql= "BEGIN if (not EXISTS (select empno tablenameA where empno = #empno)) " & _
" begin " & _
" Update tablenameA set ota_verifier = #ota_verifier, ota_approval = #ota_approval, ota_payroll = #ota_payroll, ota_hr = #ota_hr, " & _
" ota_viewall = #ota_viewall, ota_HRMng = #ota_HRMng, ota_MD = #ota_MD, ota_SP=#ota_SP " & _
" WHERE empno = #empno " & _
" end " & _
" else " & _
" begin " & _
" Insert INTO tablenameA (empno, ota_empname, ota_deptcode, ota_verifier, ota_approval, ota_payroll, ota_hr, ota_viewall, ota_AD, ota_HRMng, ota_MD, ota_SP) " & _
" VALUES (#empno, #ota_empname, #ota_deptcode, #ota_verifier, #ota_approval, #ota_payroll, #ota_hr, #ota_viewall, #ota_AD, #ota_HRMng, #ota_MD, #ota_SP) " & _
" end " & _
" END "
Incorrect syntax near '.'. Incorrect syntax near the keyword 'else'.
**You need add FROM **
strSql= "BEGIN if (not EXISTS (select ota_empno FROM [GOGREEN].[dbo].[tbOvertimeAccess] where ota_empno = #ota_empno)) " & _
" begin " & _
" Update tbOvertimeAccess set ota_verifier = #ota_verifier, ota_approval = #ota_approval, ota_payroll = #ota_payroll, ota_hr = #ota_hr, " & _
" ota_viewall = #ota_viewall, ota_HRMng = #ota_HRMng, ota_MD = #ota_MD, ota_SP=#ota_SP " & _
" WHERE ota_empno = #ota_empno " & _
" end " & _
" else " & _
" begin " & _
" Insert INTO tbOvertimeAccess (ota_empno, ota_empname, ota_deptcode, ota_verifier, ota_approval, ota_payroll, ota_hr, ota_viewall, ota_AD, ota_HRMng, ota_MD, ota_SP) " & _
" VALUES (#ota_empno, #ota_empname, #ota_deptcode, #ota_verifier, #ota_approval, #ota_payroll, #ota_hr, #ota_viewall, #ota_AD, #ota_HRMng, #ota_MD, #ota_SP) " & _
" end " & _
" END "
It seems like you are missing a FROM keyword between empno and tablenameA
strSql= "BEGIN if (not EXISTS (select empno tablenameA where empno = #empno)) "
try this :
strSql= "BEGIN if (not EXISTS (select empno FROM tablenameA where empno = #empno)) " & _
" begin " & _
" Update tablenameA set ota_verifier = #ota_verifier, ota_approval = #ota_approval, ota_payroll = #ota_payroll, ota_hr = #ota_hr, " & _
" ota_viewall = #ota_viewall, ota_HRMng = #ota_HRMng, ota_MD = #ota_MD, ota_SP=#ota_SP " & _
" WHERE empno = #empno " & _
" end " & _
" else " & _
" begin " & _
" Insert INTO tablenameA (empno, ota_empname, ota_deptcode, ota_verifier, ota_approval, ota_payroll, ota_hr, ota_viewall, ota_AD, ota_HRMng, ota_MD, ota_SP) " & _
" VALUES (#empno, #ota_empname, #ota_deptcode, #ota_verifier, #ota_approval, #ota_payroll, #ota_hr, #ota_viewall, #ota_AD, #ota_HRMng, #ota_MD, #ota_SP) " & _
" end " & _
" END "
In my Access application, I am trying to concat existing field value with new value and I have code as below written in VBA, I am getting error 3075 "syntax error missing operator"
sUser = UserNameWindows
' MsgBox sUser
currenttime = Format(Now(), "dd/mm/yyyy hh:mm:ss")
SqlQuery = "UPDATE tbl_AllRequests " & _
" SET [Status]= 'Archived' " & _
", [History] = [History] & CHR(13) & CHR(10) & 'Archived on' " & currenttime & " ' by ' & '" + sUser + "' " & _
" WHERE [ID] in (" & selectedIDs & ")"
'" SET [History] = concat([History], 'Archived on " & currenttime & " by '" & sUser & "'&')'&" & _
'data=concat(data, 'a')
'SqlQuery = "UPDATE tbl_AllRequests SET Status = 'Archived' WHERE ID in (17, 11)"
Debug.Print "this is new one " & SqlQuery
DoCmd.RunSQL SqlQuery, True
I am getting error on below line of code.
", [History] = [History] & CHR(13) & CHR(10) & 'Archived on' " & currenttime & " ' by ' & '" + sUser + "' " & _
If I remove the code after 'Archived on' it works.
Thanks
First correct this:
currenttime = Format(Now(), "dd/mm/yyyy hh:nn:ss")
Then, use only single quotes around your fixed text:
", [History] = ([History] + CHR(13) + CHR(10)) & 'Archived on '" & currenttime & "' by '" & sUser & "" & _
Also, it makes no sense first to format Now to a string expression, then convert (with errors) to a date value by wrapping it in octothorpes (#..#), which - when concatenated with the strings - will be casted once more to a string.
Stuggling a bit here. Trying to develop the code to link user input into my database, in the form of a book record. for example the user would be asked to enter their name address etc. But the code I have used does not seem to execute because I continually get the same error.
Line 12: Dim con As New SqlConnection
Line 13: Dim inscmd As New SqlCommand
Line 14: con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("Database.My.MySettings.Database1ConnectionString1").ConnectionString
Line 15: con.Open()
Line 16: inscmd.CommandText = ("insert into booking values('" + txtfirstname.Text + "', " + txtSurname.Text + "', " + txtAddressline1.Text + "', " + txtAddressline2.Text + "', " + txtPostcode.Text + "', " + txtTime.Text + "', " + txtPeople.Text + "', " + txtDropoff1.Text + "', " + txtDropoff2.Text + "', " + txtDropoffpost.Text + "")
It is line 14 that contains the error but I dont know why. This is my code;
Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsubmit.Click
Dim con As New SqlConnection
Dim inscmd As New SqlCommand
con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("Database.My.MySettings.Database1ConnectionString1").ConnectionString
con.Open()
inscmd.CommandText = ("insert into booking values('" + txtfirstname.Text + "', " + txtSurname.Text + "', " + txtAddressline1.Text + "', " + txtAddressline2.Text + "', " + txtPostcode.Text + "', " + txtTime.Text + "', " + txtPeople.Text + "', " + txtDropoff1.Text + "', " + txtDropoff2.Text + "', " + txtDropoffpost.Text + "")
Print(inscmd.CommandText)
inscmd.Connection = con
inscmd.ExecuteNonQuery()
con.Close()
inscmd.Parameters.Clear()
MsgBox("Your booking has been successfully")
con.Close()
End Sub
Hopefully this will help you ( insert your code where need to )
Dim con As New SqlConnection
Dim myConString As String = getSQLString() ' GET YOUR CON String
' my function looks like this when returned
"Server=ServerExactLocationPath;Database=DataBase;User Id=UserName;Password=Password;"
Dim objcommand As SqlCommand = New SqlCommand
'con.ConnectionString = myConString
With objcommand
.Connection = con
Dim cmdText As String = ""
cmdText = "Insert into SitesStatus (SiteNumber,StatusName,Date,ByUser) values ('" & site & "','" & status & "','" & System.DateTime.Today.ToString("MM/dd/yyyy") & "','" & dbUiInitials & "')"
'PUT YOUR INSERT ABOVE
.CommandText = cmdText
End With
con.ConnectionString = myConString
con.Open()
objcommand.ExecuteNonQuery()
con.Close()
Catch ex As Exception
End Try
Return Nothing
insert into booking values('" + txtfirstname.Text + "', " + txtSurname.Text + "', " + txtAddressline1.Text + "', " + txtAddressline2.Text + "', " + txtPostcode.Text + "', " + txtTime.Text + "', " + txtPeople.Text + "', " + txtDropoff1.Text + "', " + txtDropoff2.Text + "', " + txtDropoffpost.Text + "
should be
insert into booking values('" + txtfirstname.Text + "', '" + txtSurname.Text + "', '" + txtAddressline1.Text + "', '" + txtAddressline2.Text + "', '" + txtPostcode.Text + "', " + txtTime.Text + "', '" + txtPeople.Text + "', '" + txtDropoff1.Text + "', '" + txtDropoff2.Text + "', '" + txtDropoffpost.Text + "')"
You should use the connection string wizard in the Project Settings window. Then try the test connection button, Make sure the type of the setting is ConnectionString
You should be able to get the connection string using this syntax if things are set up right.
con.ConnectionString = my.Settings.Database1ConnectionString1
strSQL = "INSERT INTO user_account_details" & _
"(lastname,firstname,middlename,usertype,reg_date_time,status)" & _
" VALUES ( " & _
" '" & txtLName.Text & "', " & _
" '" & txtFName.Text & "' , " & _
" '" & txtMName.Text & "' , " & _
" '" & cboUserType.Text & "' , " & _
" '#" & Now & "#', " & _
" 'Inactive' " & _
")"
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")
Can someone help me how to check if my database is already been update after I make an edit, here are my codes.. my code is just to make an edit and after that update it on the database, what I dont know is how to check if the update happen..
Dim dT As DataTable = MyDB.ExecCommand("SELECT `Field Name` FROM `tblfield` ORDER BY `Field Order`", "wellsfargo").Tables(0)
For i As Integer = 1 To flp.Controls.Count - 1
Application.DoEvents()
Dim xHead As uHead = DirectCast(flp.Controls(0), uHead)
Dim xCont As uControl = DirectCast(flp.Controls(i), uControl)
Dim sSQL As String = ""
Dim dZ As DataTable = MyDB.ExecCommand("SELECT * FROM `" + MyJob + "` WHERE `Record Number`='" + rNum + _
"' AND `Line Number`='" + xCont.lblLine.Text.Trim.Replace("'", "\'") + "'", "wellsfargo").Tables(0)
If dZ.Rows.Count <> 0 Then
sSQL = "UPDATE `" & MyJob & "` SET "
sSQL = sSQL + "`Orig Document Begin ID`='" + xHead.txtOrigBegDoc.Text.Trim + "'"
sSQL = sSQL + ",`Orig Document End ID`='" + xHead.txtOrigEndDoc.Text.Trim + "'"
sSQL = sSQL + ",`Beg Doc`='" + xHead.txtBegDoc.Text.Trim + "'"
sSQL = sSQL + ",`End Doc`='" + xHead.txtEndDoc.Text.Trim + "'"
sSQL = sSQL + ",`Loan Number`='" + xHead.txtLoan.Text.Trim + "'"
sSQL = sSQL + ",`Page Count`='" + xHead.txtPage.Text.Trim + "'"
sSQL = sSQL + ",`Path`='" + xHead.txtPath.Text.Trim + "'"
sSQL = sSQL + ",`File Number`='" + xHead.txtFileNumber.Text.Trim + "'"
sSQL = sSQL + ",`Settlement`='" + xHead.txtDate.Text.Trim + "'"
sSQL = sSQL + ",`Long and Foster`='" + xHead.txtLaF.Text.Trim + "'"
sSQL = sSQL + ",`Comment`='" + xHead.txtComm.Text.Trim + "'"
sSQL = sSQL + ",`Description`='" + xCont.cboDesc.Text.Trim.Replace("'", "\'") + "'"
sSQL = sSQL + ",`Amount`='" + xCont.txtAmount.Text.Trim.Replace("'", "\'") + "'"
sSQL = sSQL + ",`Payee`='" + xCont.txtPayee.Text.Trim.Replace("'", "\'") + "'"
sSQL = sSQL + ",`Borrower`='" + xCont.txtBorrower.Text.Trim.Replace("'", "\'") + "'"
sSQL = sSQL + ",`Seller`='" + xCont.txtSeller.Text.Trim.Replace("'", "\'") + "'"
sSQL = sSQL + ",`Prosperity Borrower`='" + xCont.txtPBorrower.Text.Trim.Replace("'", "\'") + "'"
sSQL = sSQL + ",`Prosperity Seller`='" + xCont.txtPSeller.Text.Trim.Replace("'", "\'") + "'"
sSQL = sSQL & " WHERE `Record Number` = '" & rNum & _
"' AND `Line Number`='" + xCont.lblLine.Text.Trim.Replace("'", "\'") + "'"
MyDB.ExecQuery(sSQL, "wellsfargo")
Else
Dim sColumn As String = ""
For z As Integer = 0 To dT.Rows.Count - 1
If z = 0 Then
sColumn = "`" & dT.Rows(z).Item(0).ToString & "`"
Else
sColumn = sColumn & ",`" & dT.Rows(z).Item(0).ToString & "`"
End If
Next
sSQL = "INSERT INTO `" + MyJob + "` (" + sColumn + ") VALUES (" + _
"'" + rNum + "'," + _
"'" + xHead.txtOrigBegDoc.Text.Trim.Replace("'", "\'") + "'," + _
"'" + xHead.txtOrigEndDoc.Text.Trim.Replace("'", "\'") + "'," + _
"'" + xHead.txtBegDoc.Text.Trim.Replace("'", "\'") + "'," + _
"'" + xHead.txtEndDoc.Text.Trim.Replace("'", "\'") + "'," + _
"'" + xHead.txtLoan.Text.Trim.Replace("'", "\'") + "'," + _
"'" + xHead.txtPage.Text.Trim.Replace("'", "\'") + "'," + _
"'" + xHead.txtPath.Text.Trim.Replace("'", "\'") + "'," + _
"'" + xHead.txtFileNumber.Text.Trim.Replace("'", "\'") + "'," + _
"'" + xHead.txtDate.Text.Trim.Replace("'", "\'") + "'," + _
"'" + xHead.txtLaF.Text.Trim.Replace("'", "\'") + "'," + _
"'" + xHead.txtComm.Text.Trim.Replace("'", "\'") + "'," + _
"'" + xCont.lblLine.Text.Trim.Replace("'", "\'") + "'," + _
"'" + xCont.cboDesc.Text.Trim.Replace("'", "\'") + "'," + _
"'" + xCont.txtAmount.Text.Trim.Replace("'", "\'") + "'," + _
"'" + xCont.txtPayee.Text.Trim.Replace("'", "\'") + "'," + _
"'" + xCont.txtBorrower.Text.Trim.Replace("'", "\'") + "'," + _
"'" + xCont.txtSeller.Text.Trim.Replace("'", "\'") + "'," + _
"'" + xCont.txtPBorrower.Text.Trim.Replace("'", "\'") + "'," + _
"'" + xCont.txtPSeller.Text.Trim.Replace("'", "\'") + "')"
MyDB.ExecQuery(sSQL, "wellsfargo")
End If
Next
MsgBox("Record successfully modified!", MsgBoxStyle.Information, Me.Text)
Usually not too much sense to check - exception will be thrown if something was going wrong.
You can wrap it into stored procedure and check ##rowcount variable.
Or really just make new select to read changing data.