i got sql syntax error when i debug my application - sql

I want to update my database using formatsqlparam, but when I debug it, it has an error saying:
"Incorrect syntax near ','."
This is my code:
Dim sql2 As String = "update infoHotel set nameHotel = N" & FormatSqlParam(hotel) & _
", knownAs1 = N" & FormatSqlParam(KnownAs(0)) & _
", knownAs2 = N" & FormatSqlParam(KnownAs(1)) & _
", knownAs3 = N" & FormatSqlParam(KnownAs(2)) & _
", knownAs4 = N" & FormatSqlParam(KnownAs(3)) & _
", streetAddress = N" & FormatSqlParam(StreetAddress) & _
", locality = N" & FormatSqlParam(Locality) & _
", postalCode = N" & FormatSqlParam(PostalCode) & _
", country = N" & FormatSqlParam(Country) & _
", addressFull = N" & FormatSqlParam(address) & _
", tel = N" & FormatSqlParam(contact) & ","
Dim objCommand3 As New SqlCommand(sql2, conn)
objCommand3.ExecuteNonQuery()
Maybe I am missing some syntax, but I couldn't find where it is. I hope somebody can help. Thanks in advance. I'm using VB.Net and SQL.

The last line should be like this:
", tel = N" & FormatSqlParam(contact)
Also, you don't have a Where clause in your statement so this will update all rows in your table.

Looks like the trailing comma is your problem:
", tel = N" & FormatSqlParam(contact) & ","

Related

SQL UPDATE function syntax error

I am attempting to write an UPDATE function based on values that will be input into dropdown boxes and text boxes by a user
My code is as follows:
Private Sub cmdUpdate_Prices_Click()
Dim SQL_string As String
SQL_string = _
"UPDATE tblPrice " & _
"SET [P/N] = " & Me.txtPartNumber.Column(0) & ", " & _
"Type = " & Me.txtPriceType.Column(0) & ", " & _
"Region = " & Me.txtRegion.Column(0) & ", " & _
"Price = " & Me.txtPrice.Value & ", " & _
"WHERE ID = " & Me.txtID.Value & ";"
DoCmd.RunSQL SQL_string
End Sub
I keep getting a syntax error in the UPDATE function and I'm not sure where it is.
Any help is appreciated. Thanks!

item cannot be found in the collection corresponding to the requested name or ordinal

My code gives the following error. How can I correct this?
Item cannot be found in the collection corresponding to the requested name or ordinal
ElseIf Me.chkItem.Checked = True Then
Dim CheckNumber As String = ""
Dim CheckRef As String = ""
dsvoucheritem.Clear()
DSVoucher_Expense.Clear()
DSVoucher_Check.Clear()
Try
Me.lstCV.Items.Clear()
strDiscount = Nothing
rec.Open("select billpaymentcheckline.txnnumber, billpaymentcheckline.txndate" _
& ", billpaymentcheckline.payeeentityreffullname" _
& ", billpaymentcheckline.amount, billitemline.itemlineitemreffullname" _
& ", billitemline.memo" _
& ", billpaymentcheckline.appliedtotxndiscountamount" _
& ", billpaymentcheckline.appliedtotxnrefnumber, billpaymentcheckline.bankaccountreffullname" _
& ", billpaymentcheckline.appliedtotxndiscountaccountreffullname" _
& ", billpaymentcheckline.appliedtotxntxndate, billpaymentcheckline.appliedtotxnamount" _
& ", billpaymentcheckline.refnumber, account.AccountNumber from (billitemline inner join" _
& " billpaymentcheckline on billitemline.refnumber=billpaymentcheckline.appliedtotxnrefnumber) left outer join" _
& " account on billitemline.APAccountreflistid=account.listid where" _
& " billpaymentcheckline.bankaccountreflistid='" &Me.lblBankID.Text & "' and" _
& " billpaymentcheckline.refnumber between '" & CInt(Me.txtRefFR.Text)
& "' and '" & CInt(Me.txtRefTO.Text) & "'", con, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly)
It's saying it doesn't recognize one of your column names. Double check all of them. You can also try removing fields until you find the culprit.
memo is a reserved word in Access SQL.
Try billitemline.[memo]

Access 2007 Query Def Syntax Error

this is really frustrating and I'm hoping you can help me. I keep getting a syntax error no matter what I do on this query. I've read about putting parantheses on multiple joins but I did that and it doesn't work. What am I doing wrong here? Thanks for your time.
Set PTI = CurrentDb.CreateQueryDef("qry_PTImport", "SELECT DISTINCT" & _
" bl.LoanNumber" & _
", qcq.Code" & _
", t.ecType" & _
", eb.QueryNo" & _
", q.QueryName" & _
", q.Description" & _
", q.suggestedAction" & _
", bd.datetransfer" & _
", bd.datebrd " & _
"FROM ((((dbo.eCs_QCqueue qcq " & _
"JOIN dbo.eCs_Queries q ON q.QueryNo = eb.QueryNo) " & _
"JOIN brd_loan bl ON bl.serialID = qcq.serialID) " & _
"JOIN dbo.eCs_Type t ON t.ectypeid = q.ectypeid) " & _
"JOIN brd_deal bd ON bd.Code = bl.Code) " & _
"WHERE qcq.Code = '" & Deal & "'")
just wanted to say user HansUp was right in this instance of what my problem was. VBA doesn't do just JOINs.

Using variables in fieldnames - SQL Update statement

I am new to using Access 2010. I wish to execute the following sql update statement, but I have problems with the syntax. The table is called "Forecasts", and users will edit & update the qty forecasted.
Problem - The table fieldnames are 2014_1, 2014_2, 2014_3 ... to represent the different months, stored in an array. I have done abit of research and I believe the way to dynamically do this is:
Dim sqlString As String
sqlString = "UPDATE Forecasts " & _
" SET Branch_Plant=" & Me.txtBranch_Plant & _
", Item_Number_Short='" & Me.txtItem_Number_Short & "'" & _
", Description='" & Me.txtDescription & "'" & _
", UOM='" & Me.txtUOM & "'" & _
", Estimated_Cost=" & Me.txtEstimated_Cost & _
", Requesting_Business_Unit='" & Me.txtRequesting_Business_Unit & "'" & _
", End_Customer='" & Me.txtEnd_Customer & "'" & _
", Project='" & Me.txtProject & "'" & _
", Forecasts." & "[" & arrMonthToDisplay(0) & "]" = " & Me.txtProjectedJanVolume " & _
" WHERE ID =" & Me.txtID.Tag
MsgBox ("This is the output: " & sqlString)
CurrentDb.Execute sqlString
It was working fine until this line was added
Forecasts." & "[" & arrMonthToDisplay(0) & "]" = " & Me.txtProjectedJanVolume
The msgbx output now shows: "False". Whats wrong with sqlString?
Please help! Thank you very much.
", Forecasts.[" & arrMonthToDisplay(0) & "] = " & Me.txtProjectedJanVolume & _
" WHERE ID =" & Me.txtID.Tag
You compare string to string.
Change
", Forecasts." & "[" & arrMonthToDisplay(0) & "]" = " & Me.txtProjectedJanVolume " &
to
", Forecasts." & "[" & arrMonthToDisplay(0) & "] = " & " Me.txtProjectedJanVolume " &

how to make condition to update table?

i want to make condition to update my table if there's already same data (in the same column) inserted in the table.
im using
If String.ReferenceEquals(hotel, hotel) = False Then
insertDatabase()
Else
updateDatabase()
End If
this is the updateDatabase() code...
Dim sql2 As String = "update infoHotel set nameHotel = N" & FormatSqlParam(hotel) & _
", streetAddress = N" & FormatSqlParam(StreetAddress) & _
", locality = N" & FormatSqlParam(Locality) & _
", postalCode = N" & FormatSqlParam(PostalCode) & _
", country = N" & FormatSqlParam(Country) & _
", addressFull = N" & FormatSqlParam(address) & _
", tel = N" & FormatSqlParam(contact) & _
"where hotel = '" & hotel & "')"
this is the formatSqlParam() code:
Function FormatSqlParam(ByVal strParam As String) As String
Dim newParamFormat As String
If strParam = String.Empty Then
newParamFormat = "'" & "NA" & "'"
Else
newParamFormat = strParam.Trim()
newParamFormat = "'" & newParamFormat.Replace("'", "''") & "'"
End If
Return newParamFormat
End Function
the function manage to go into updateDatabase() but has some error saying
"Incorrect syntax near 'Oriental'."
Oriental is the data inserted in the table. is it suitable to use ReferenceEquals?
im using vb.net and sql database..tq
You should be using parameterized queries instead of concatenating strings like so:
Dim sql2 As String = "Update InfoHotel" _
& "Set nameHotel = #nameHotel" _
& ", knownAs1 = #knownAs1" _
& ", knownAs2 = #knownAs2" _
& ", knownAs3 = #knownAs3" _
& ", knownAs4 = #knownAs4" _
& " Where hotel = #hotel"
If you used parameterized queries, you wouldn't have to worry about trying do quote replacement and potentially introducing a SQL Injection vulnerability.
You have a single quote in your data, as in:
Oriental's
Show us the code for FormatSqlParam() -- the bug is probably in there.
Or else you left out the single quotes around the hotel name:
where hotel = '" & hotel & "')"
I hope that's not it, because it would mean you're using the name as a key, a very bad idea.