Access VBA using SQL UPDATE - vba

I'm using two Access databases (front end and back end).
My code for query work, but I get it to work with updating the database. What am I doing wrong?
I get a runtime error 3078 for the DoCmd.RunSQL strSql on line 25.
Set cnn = CreateObject("ADODB.Connection")
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & CurrentProject.Path & "\DB_Cryk.accdb"
cnn.Open strConnection
MemberID = txtMemberID.Value
strSql = "UPDATE Cryk " & _
"SET Membership = '" & txtMembership.Value & "', " & _
" Memberstatus = '" & txtMemberstatus.Value & "', " & _
" Membername = '" & txtMembername.Value & "', " & _
" Memberaddress = '" & txtMemberaddress.Value & "', " & _
" Memberzip = '" & txtMemberzip.Value & "', " & _
" Membercity = '" & txtMembercity.Value & "', " & _
" Memberphone = '" & txtMemberphone.Value & "', " & _
" Membermail = '" & txtMembermail.Value & "', " & _
" Memberyear = '" & txtMemberyear.Value & "', " & _
" Dateofbirth = '" & txtDateofbirth.Value & "', " & _
" Memberno = '" & txtMemberno.Value & "', " & _
" Memberfee = '" & txtMemberfee.Value & "', " & _
" Memberpayment = '" & txtMemberpayment.Value & "'" & _
"WHERE MemberID= '" & MemberID & "'"
DoCmd.RunSQL strSql
cnn.Close
Set cnn = Nothing

Error 3078 indicates that the target table does not exist in your database.
Note that, although you open an ADO connection to the database DB_Cryk.accdb, you execute your SQL statement using the DoCmd.RunSQL method, which operates on the current database.
Instead, if you want the SQL to be executed in your DB_Cryk.accdb database, you should use the Execute method of the ADODB Connection object, e.g.:
cnn.Execute strsql
Where query parameterization is concerned, you may wish to refer to this superb answer, specifically, the 'Using ADO' section.

Related

MSACCESS SQL Error 3021 - Updates Locally but not to Server

I have an already existing form that I added one new textbox to that allows the user to input notes. I have 2 queries that run when the submission button is clicked, one to update a local table within MSACCESS and the other is on our server. The new button is called DISPATCH_NOTES, it is also the name of the column in the tables.
BEFORE (Working)
LOCAL:
strSQL = "UPDATE WCL_DATABASE_QUERY " & vbNewLine & _
"SET WCL_DATABASE_QUERY.COMMENTS ='" & Me.COMMENTS.Value & "',WCL_DATABASE_QUERY.DISPATCH_NOTES ='" & Me.DISPATCH_NOTES.Value & "', WCL_DATABASE_QUERY.TRAFFIC_SPECIALIST ='" & GetUsername & "', WCL_DATABASE_QUERY.PLANNED_SHIP_DATE = " & PSD & "WCL_DATABASE_QUERY.LAST_UPDATE =#" & Now() & "#" & vbNewLine & _
"WHERE (((WCL_DATABASE_QUERY.CUSTOMER_PURCHASE_ORDER_ID) in ('" & Join(POList, "', '") & "'))) AND ORDER_STATUS = 'OPEN';"
CurrentDb.Execute strSQL, dbFailOnError
Before (Working) SERVER:
strSQL = "UPDATE TrafficDatabase_Query " & vbNewLine & _
"SET TrafficDatabase_Query.COMMENTS ='" & Me.COMMENTS.Value & "', TrafficDatabase_Query.DISPATCH_NOTES ='" & Me.DISPATCH_NOTES.Value & "', TrafficDatabase_Query.TRAFFIC_SPECIALIST ='" & GetUsername & "', TrafficDatabase_Query.PLANNED_SHIP_DATE = " & PSD & "TrafficDatabase_Query.LAST_UPDATE =#" & Now() & "#" & vbNewLine & _
"WHERE (((LTRIM(RTRIM(TrafficDatabase_Query.CUSTOMER_PURCHASE_ORDER_ID)) & TrafficDatabase_Query.DELIVERY_NUMBER) in (" & PODO & ")));"
CurrentDb.Execute strSQL, dbFailOnError
After (Working) LOCAL:
strSQL = "UPDATE WCL_DATABASE_QUERY " & vbNewLine & _
"SET WCL_DATABASE_QUERY.COMMENTS ='" & Me.COMMENTS.Value & "',WCL_DATABASE_QUERY.DISPATCH_NOTES ='" & Me.DISPATCH_NOTES.Value & "', WCL_DATABASE_QUERY.TRAFFIC_SPECIALIST ='" & GetUsername & "', WCL_DATABASE_QUERY.PLANNED_SHIP_DATE = " & PSD & "WCL_DATABASE_QUERY.LAST_UPDATE =#" & Now() & "#" & vbNewLine & _
"WHERE (((WCL_DATABASE_QUERY.CUSTOMER_PURCHASE_ORDER_ID) in ('" & Join(POList, "', '") & "'))) AND ORDER_STATUS = 'OPEN';"
CurrentDb.Execute strSQL, dbFailOnError
After (Broken) SERVER:
strSQL = "UPDATE TrafficDatabase_Query " & vbNewLine & _
"SET TrafficDatabase_Query.COMMENTS ='" & Me.COMMENTS.Value & "', TrafficDatabase_Query.DISPATCH_NOTES ='" & Me.DISPATCH_NOTES.Value & "', TrafficDatabase_Query.TRAFFIC_SPECIALIST ='" & GetUsername & "', TrafficDatabase_Query.PLANNED_SHIP_DATE = " & PSD & "TrafficDatabase_Query.LAST_UPDATE =#" & Now() & "#" & vbNewLine & _
"WHERE (((LTRIM(RTRIM(TrafficDatabase_Query.CUSTOMER_PURCHASE_ORDER_ID)) & TrafficDatabase_Query.DELIVERY_NUMBER) in (" & PODO & ")));"
CurrentDb.Execute strSQL, dbFailOnError
This is the Debug.Print strSQL before the LOCAL:
UPDATE WCL_DATABASE_QUERY
SET WCL_DATABASE_QUERY.COMMENTS ='Sent to Dispatch',WCL_DATABASE_QUERY.DISPATCH_NOTES ='Please ship these PO's 1st, they have been coded WCLA.', WCL_DATABASE_QUERY.TRAFFIC_SPECIALIST ='JRussi', WCL_DATABASE_QUERY.PLANNED_SHIP_DATE = Null, WCL_DATABASE_QUERY.LAST_UPDATE =#10/19/2021 1:41:57 PM#
WHERE (((WCL_DATABASE_QUERY.CUSTOMER_PURCHASE_ORDER_ID) in ('6596145'))) AND ORDER_STATUS = 'OPEN';

SQL UPDATE syntax error issue

I have some SQL where I am getting a syntax error, but cant work out why:
The Code:
CurrentDb.Execute "UPDATE [MS Access;pwd=" & strPassword & ";database=" & DBpath & "\" & DBnameVoice & "].[" & tblhistoric & "] " & _
"SET NudgeSent = '" & SendNudge & "' " & _
"WHERE [ID] = '" & UID & "'"
Why I am confused:
I have the exact same code but to update a different column and it works fine, what am I missing?
Error Received:
Syntax error in UPDATE Statement
Similar code I have the does work:
CurrentDb.Execute "UPDATE [MS Access;pwd=" & strPassword & ";database=" & DBpath & "\" & DBnameVoice & "].[" & tblhistoric & "] " & _
"SET AllocatedTo = '" & Allocateto & "' " & _
"WHERE [ID] = '" & UID & "'"

VB.Net using & _ to carry to next line

Error Error 1 Operator '&' is not defined for types 'String' and 'System.Windows.Forms.TextBox'.
What is wrong with this????!??
SQL = "UPDATE ATG_PP_QTE_HEAD SET " & _
"PART = '" & txtPart.Text & "', " & _
"LOCATION = '" & txtLoc.Text & "', " & _
"DESCRIPTION = '" & txtDescription.Text & "', " & _
"CUSTOMER = '" & txtCustID.Text & "', " & _
"CONTACT_NAME = '" & txtContactName.Text & "', " & _
"CONTACT_PHONE = '" & txtPhone.Text & "', " & _
"CONTACT_EMAIL = '" & txtEmail.Text & "', " & _
"LEAD_TIME = '" & txtLead.Text & "', " & _
"SETUP = " & txtSetup.Text & ", " & _
"WEIGHTPP = " & txtPCWT.Text & ", " & _
"NOTES = '" & txtNotes.Text & "', " & _
"LAST_MODIFIED = '" & DateTime.Now & "', " & _
"LABOR_RATE = " & txtLabor.Text & ", " & _
"OVERHEAD = " & txtOH.Text & ", " & _
"GA = " & txtGA.Text & ", " & _
"SORT_CODE = '" & txtSortCode.Text & "', " & _
"REFERENCE = '" & txtReference.Text & "', " & _
"PL = '" & txtPL.Text & "', " & _
"CUST_DRAW_NO = '" & txtCustDraw.Text & "', " & _
"COMMISSION = " & txtCommission.Text & ", " & _
"PCWT = " & txtPCWT & _
"WHERE QUOTE_ID = " & txtQuoteID.Text
What is wrong with this????!??
Quite a bit, actually. But let's start with the error itself...
On this line:
"PCWT = " & txtPCWT & _
You're trying to concatenate a TextBox to a String. As the error states, you can't do that. Perhaps you meant to use the .Text property:
"PCWT = " & txtPCWT.Text & _
Now, what else is wrong?
First, your code is highly vulnerable to SQL injection attacks. You're going to want to use parameterized queries instead of executing user input as code.
Second, using parameterized queries will make the code a lot easier to read and support, which will make errors like this much easier to find.
Third, on this line there's a significant potential for bugs:
"LAST_MODIFIED = '" & DateTime.Now & "', " & _
Using parameterized queries will remove the culture-dependent string representations from the query and use the actual DateTime data in the query. And you should also get into the habit of using DateTime.UtcNow instead, as having a consistent non-timezone-dependent value is going to make things a lot easier when you have to deal with multiple time zones.

Passing an Empty/Null Date variable in VBA to an SQL UPDATE statement

I have an excel userform with various textboxes, some are fields to enter dates. The user can then save their entries.
At this point, I connect to an access backend via an ADO connection. The values entered by a user are passes to an SQL string, e.g.
strSQL = "UPDATE tblDECONVERSION_DATA SET tblDECONVERSION_DATA.Status = '" & NewBusiness_WorkQueue.Decon_CaseStatus & "', " & _
"tblDECONVERSION_DATA.DMS = '" & NewBusiness_WorkQueue.Decon_DMS & "', " & _
"tblDECONVERSION_DATA.DateRecieved = #" & Format(NewBusiness_WorkQueue.Decon_DateRecieved, "mm/dd/yyyy") & "#, " & _
"tblDECONVERSION_DATA.WireDate = #" & Format(NewBusiness_WorkQueue.Decon_WireDate, "mm/dd/yyyy") & "#, " & _
"tblDECONVERSION_DATA.LastEditXID = '" & CurrUser & "', tblDECONVERSION_DATA.LastEditDate = #" & Now & "# " & _
"WHERE (((tblDECONVERSION_DATA.CaseID)=" & ID & "));"
adoRecSet.Open Source:=strSQL, ActiveConnection:=dbconnect, CursorType:=adOpenDynamic, LockType:=adLockOptimistic
However, some of the date fields can be left blank, meaning for example the NewBusiness_WorkQueue.Decon_DateRecieved variable being empty. This causes a syntax error. How can I pass a Null or Empty date variable in the SQL statement that both VBA and the access database will accept?
strSQL = "UPDATE tblDECONVERSION_DATA SET tblDECONVERSION_DATA.Status = '" & _
NewBusiness_WorkQueue.Decon_CaseStatus & "', " & _
"tblDECONVERSION_DATA.DMS = '" & NewBusiness_WorkQueue.Decon_DMS & "', " & _
"tblDECONVERSION_DATA.DateRecieved = " & _
DateOrNull(NewBusiness_WorkQueue.Decon_DateRecieved) & ", " & _
"tblDECONVERSION_DATA.WireDate = " & _
DateOrNull(NewBusiness_WorkQueue.Decon_WireDate) & ", " & _
"tblDECONVERSION_DATA.LastEditXID = '" & CurrUser & _
"', tblDECONVERSION_DATA.LastEditDate = #" & Now & "# " & _
"WHERE tblDECONVERSION_DATA.CaseID=" & ID & ";"
An example function:
Function DateOrNull(v) As String
Dim rv as String
If IsDate(v) Then
rv = " #" & Format(v, "mm/dd/yyyy") & "# "
Else
rv = " null "
End If
DateOrNull = rv
End Function

Vb6 Sql update query

what is wrong in this code i get syntax error from this query
ssql = "UPDATE karakter SET lvl='" & Form1.Label7.Caption & "','" & hp = Form1.Label18.Caption & "','" & mp = Form1.Label17.Caption & "','" & para = Form1.Label19.Caption & "','" & yuzde = Form1.Label16.Caption & "' WHERE id='" & Form1.Label5.Caption & "'"
You have wrong positions for you quotes.
ssql = "UPDATE karakter SET lvl='" & Form1.Label7.Caption & "', " & _
"hp ='" & Form1.Label18.Caption & "', " & _
"mp ='" & Form1.Label17.Caption & "', " & _
"para ='" & Form1.Label19.Caption & "', " & _
"yuzde ='" & Form1.Label16.Caption & "' " & _
"WHERE id='" & Form1.Label5.Caption & "'"
I suggest to use the parameters instead of concatenation.