updating table via SQL with variables - ms-access

I'm trying to update a table, via docmd.RunSql, and can't get it to update.
idlook = DLookup("[ID]", "119_review", "[todays_date] = #" & Format("" & Me.Combo87 & " " & Me.Combo89 & " 20" & Me.Combo91 & "", Medium) & "#")
MySQL = "UPDATE 119_review SET [Earned_Income]=" & Val(EarnedIncome) & " AND [Earned_income_withcal]=" & Val(EarnedIncomeCal) & " WHERE [ID]= " & idlook & ";"
Debug.Print MySQL
DoCmd.RunSQL MySQL
I've tried it both with and without brackets on the fields, the immediate window reads:
UPDATE 119_review SET Earned_Income=62 AND Earned_income_withcal=58.4 WHERE ID= 23;
UPDATE 119_review SET [Earned_Income]=62 AND [Earned_income_withcal]=58.4 WHERE [ID]= 23;
any idea where I'm going wrong?

You've made a simple syntax error.
Different columns in an update statement should be separated by ,, not by AND.
Weirdly enough, doing this wrong doesn't throw a syntax error, but just doesn't update anything.
Change the row setting your SQL string to the following:
MySQL = "UPDATE 119_review SET [Earned_Income]=" & Val(EarnedIncome) & " , [Earned_income_withcal]=" & Val(EarnedIncomeCal) & " WHERE [ID]= " & idlook & ";"

Related

Error on INSERT INTO statement

I have the following and getting the error in INSERT INTO statement. I've done a debug.print and pasted back into SSMS and it works just fine so I'm really stumped. The syntax looks fine to me but I know sometimes going from straight SQL to VBA SQL can be tricky. I had a feeling it was the EXISTS section and I took that out and made the appropriate edits and still got the error msg. Any suggetions?
sqlstr = "INSERT INTO [database.[dbo].[table]" & _
"(" & _
"User_name" & _
",Client_Id" & _
",Client_Name" & _
",UserAccess" & _
",UserId" & _
")" & _
"SELECT " & _
"User_name = '" & UserName & "'" & _
",Client_Id = " & Me.ClientList.ItemData(ClientID) & "" & _
",Client_Name = '" & Me.ClientList.Column(1, ClientID) & "'" & _
",UserAccess = 0" & _
",UserId = " & UserId & "" & _
" WHERE NOT EXISTS (SELECT 1 FROM [database].[dbo].[table] where UserID = " & UserId & " and Client_Id = " & Me.ClientList.ItemData(ClientID) & ")"
Access SQL != T-SQL.
You either need to run this SQL string as a Pass-Through query, then it's the same as running it in SSMS.
Or translate it into Access SQL.
At the very most you must change the table names into the names of the linked tables in Access (which certainly aren't [database].[dbo].[table])
If you need more help with option 2, please post the formatted result of Debug.Print sqlstr

MS Access Tag property - how its identifying update and insert operation?

I trying to create Add,update,Delete operation on MS Access form.I found this code on internet where Insert and update is happening on the same button. I am not getting what is exactly happening in below line and how it's identifying it is for update or insert.
Not getting following line : = Me.txtid.Tag & "" = ""
Please find below code which works perfect as per requirement.
'When we click on button Add there are two options
'1. for insert
'2. for update
If Me.txtid.Tag & "" = "" Then
' this is for insert new
' add data in table
CurrentDb.Execute "insert into student(stdid,stdname,gender,phone,address)" & _
" values(" & Me.txtid & ",' " & Me.txtname & " ',' " & Me.cmbgender & " ','" & _
Me.txtphone & "', '" & Me.txtaddress & "')"
'refresh data in list on form
subform_student.Form.Requery
Else
CurrentDb.Execute "UPDATE student " & _
" set stdid = " & Me.txtid & _
", stdname = '" & Me.txtname & "' " & _
", gender = '" & Me.cmbgender & " ' " & _
", phone = ' " & Me.txtphone & " ' " & _
", address = ' " & Me.txtphone & " ' " & _
" WHERE stdid = " & Me.txtid.Tag
End If
The .Tag property is a general-purpose string property of every form and control object in VBA/VB6. It is provided as a place for developers to "put stuff" to support the operation of their applications.
The original code from which you copied your sample must have written a value to Me.txtid.Tag when the record was loaded (e.g., perhaps in the form's Current event) to indicate whether the record is an existing record or a new record (empty="new", non-empty="existing"). The line
If Me.txtid.Tag & "" = "" Then
simply checks to see if the .Tag property is empty, and then performs the INSERT or UPDATE accordingly.
BTW, re:
below code which works perfect as per requirement
No, it doesn't. Try adding a record where [stdname] is Tam O'Shanter and see for yourself. You should ditch the dynamic SQL and use one of
a bound form (as Gustav suggests),
a parameterized query, or
a recordset update.
Forget/remove all this code and bind the form to table Student to make this all happen automatically.
If a bound form is not familiar to you, browse for a tutorial for "Beginning with Microsoft Access" or the like.

Using DCount function with a query linked to a control

The following code is giving me a syntax error message. I would like to use the DCount function on the current event of a form to count records in a table where the value on the control 'TeamID' is equal to that in field 'teamID' in the table 'tblCompetency06'
Me.etcRecordNumber.Caption = "Record " & Me.CurrentRecord & " of " & DCount("ID", "tblCompetency06") WHERE [tblcompetency06].[teamID]= '" & me.Teamid & "'"
There could be two reasons, one you are using the wrong syntax for DCount. Second you are using/comparing a Number data type and you are comparing with a String. Try the following.
Me.etcRecordNumber.Caption = "Record " & Me.CurrentRecord & " of " & _
DCount("*", "tblCompetency06", "[teamID]= " & me.Teamid)

Error in Update Statement in Access

I have to update 3 values in a table named tbl_InvoicewiseDetails for which I am using UPDATE statement. However I am getting an error no.3144 SYNTAX Error in UPDATE Statement.
I have the following code:
CurrentDb.Execute "UPDATE tbl_InvoicewiseDetails" _
& "Set [Form Status] = " & "Received" & "" _
& "Set [Form No] = " & Me.FormSerialNo & "" _
& "Set [Form Amount] =" & Forms![ReceivedbyPBC]![tbl_tempFormDetails].Form![InvoiceValue] & "" _
& "WHERE [Year&Invoice] =" & Forms![ReceivedbyPBC]![tbl_tempFormDetails].Form![Year&Invoice] & ";"
Pls help
This is the SQL query copied from SQL View of Query builder :-
UPDATE tbl_InvoiceWiseDetails
SET tbl_InvoiceWiseDetails.[Form Status] = "Received",
tbl_InvoiceWiseDetails.[Form No] = [Forms]![ReceivedbyPBC]![FormSerialNo],
tbl_InvoiceWiseDetails.[Form Amount] = [Forms]![tbl_tempFormDetails]![InvoiceValue]
WHERE
(((tbl_InvoiceWiseDetails.[Year&Invoice])=[Forms]![tbl_tempFormDetails]![Year&Invoice]));
Hope this is helpful
Should be only one SET I think. Replace all following SET by ,.
Also CurrentDb.Execute will NOT work if you have references to forms. You MUST use DoCmd.RunSql then. (EDIT: I realize the form references are evaluated before, so problem is not there).
I suggest you assign the statement to a variable and Debug.Print the variable.
Also
& "Set [Form Status] = " & "Received" & "" _
should read
& "Set [Form Status] = 'Received'" & _
Is [Form No] a number ? If it's a string, you need to provide quotes, like this:
& "Set [Form No] = '" & Me.FormSerialNo & "'" _

Too few parameters, expected 1? error access

I am getting the above error whilst running this commmand:
CurrentDb.Execute "UPDATE VolunteerDetails " & _
" SET FirstName=" & Me.frst_Name_txt & _
", LastName='" & Me.lst_Name_txt & "'" & _
", PostalCode='" & Me.pst_Code_txt & "'" & _
" WHERE VolsID=" & Me.vol_ID_txt
Can anyone explain why?
Thanks!
I think your problem is in the FirstName value. It seems to be a string and so you need to put your value inside single quotes
CurrentDb.Execute "UPDATE VolunteerDetails " & _
" SET FirstName='" & Me.frst_Name_txt & "'" & _
", LastName='" & Me.lst_Name_txt & "'" & _
", PostalCode='" & Me.pst_Code_txt & "'" & _
" WHERE VolsID=" & Me.vol_ID_txt
Said that, please read about Sql Injection and Parameterized queries. Your code, as is, it is very weak and could fail simply if someone types a LastName like O'Malley (the single quote breaks your string concatenation)
As pointed by a comment above, I suppose that these variables are not TextBoxes but just string variables. If these are TextBoxes then, the value to set the field to comes from the Text property of the TextBox. Something like
" SET FirstName='" & Me.frst_Name_txt.Text & "'"