Too few parameters, expected 1? error access - mysql

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 & "'"

Related

Access VBA code : How to define multiple WHERE statements ? UPDATE - SET - WHERE statement

I got following code to work, except for the last line.
So I want to update a table called loggingX , this is working with the code below, except for I want the WHERE clause to not only check for 1 field (this is working) , but I want the WHERE to also check for field WHid to be a fixed value.
I would like to know how I can add multiple parts to my WHERE statement here. UPDATE should only be done if these 2 conditions below are met. I only have trouble and want to know how to put both conditions in the WHERE clause.
stdid=" & Me.txtID.Tag
WHid=" & Me.txtWHid
Complete update statement for current DB (AND is not working):
CurrentDb.Execute "UPDATE loggingX " & _
" SET stdid=" & Me.txtID & _
", stdname='" & Me.txtName & "'" & _
", gender='" & Me.cboGender & "'" & _
", phone='" & Me.txtPhone & "'" & _
", address='" & Me.txtAddress & "'" & _
", WHid='" & Me.txtWHid & "'" & _
" WHERE stdid=" & Me.txtID.Tag
" AND WHid=" & Me.txtWHid
You're missing an ampersand and underscore on the second to last line:
CurrentDb.Execute "UPDATE loggingX " & _
" SET stdid=" & Me.txtID & _
", stdname='" & Me.txtName & "'" & _
", gender='" & Me.cboGender & "'" & _
", phone='" & Me.txtPhone & "'" & _
", address='" & Me.txtAddress & "'" & _
", WHid='" & Me.txtWHid & "'" & _
" WHERE stdid=" & Me.txtID.Tag & _
" AND WHid=" & Me.txtWHid

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.

3011 error code access recordset vba

This is the code that I have worked over many many times, fix one error code3061, then another, now this. ANY IDEAS WHY THIS ERROR IS HAPPENING, all objects spelled correctly?
Dim strSQL As String
Dim strForms As String
strForms = [Forms]![frmEnterResRecordset]![txtPhone]
MsgBox strForms
strSQL = "SELECT tblCustomer.IDCustomer, tblCustomer.PHONE, tblCustomer.LASTNAME, " & _
"tblCustomer.FIRSTNAME, tblCustomer.NAME, tblCustomer.EMAIL " & _
"FROM tblCustomer " & _
"WHERE (((tblCustomer.PHONE) Like " & "*'" & strForms & "'*" & "));"
Check your syntax near the Like operator. The asterisks are outside the single quotes. Try replacing it as follows:
strSQL = "SELECT tblCustomer.IDCustomer, tblCustomer.PHONE, tblCustomer.LASTNAME, " & _
"tblCustomer.FIRSTNAME, tblCustomer.NAME, tblCustomer.EMAIL " & _
"FROM tblCustomer " & _
"WHERE (((tblCustomer.PHONE) Like " & "'*" & strForms & "*'" & "));"
Something I like to do when building dynamic SQL statements like this is to print it to the debug window with the following statement:
debug.print strSQL
It's easier to spot statements like:
Like *'my_entered_value'*

How to add a new record from unbound fields of two tables in to a subform

Hey there Good Day can anyone help me please?
I got a mainform and a subform and Ive created a query to join the two tables and ive tried to use the query to insert fields which is unbound, my subform fields which im getting from my join query if I insert into my fields it says and click the Add button I get a error it says:
Run-time error '3134':
Syntax error in insert into statement.
Code I used below:
Private Sub Add_Click()
CurrentDb.Execute "INSERT INTO PlantTransactionQuery(TransactionID,Plant Number,Categories,Description,Location,TransactionDate,Opening_Hours,Closing_Hours,Hours Worked,Fuel,Fuel Cons Fuel/Hours,Hour Meter Replaced,Comments)" & _
"VALUES(" & Me.txt13 & ",'" & Me.txt1 & "','" & Me.txt2 & "','" & Me.txt3 & "','" & Me.txt4 & "','" & Me.txt5 & "','" & Me.txt6 & "','" & Me.txt7 & "','" & Me.txt8 & "','" & Me.txt9 & "','" & Me.txt10 & "'," & Me.txt11 & "," & Me.txt12 & ")"
PlantTransactionsubform.Form.Requery
End Sub
I dont know if u can insert into a query and i dont know which name I must put into brackets for reserved name.
Any help will be much appreciated of how to add two tables in a subform in a button onclick on the same page or even maybe i made a mistake in my code for me everything looks good.
Thanks in advance
You need to put square brackets [] around any table or field name that
contains spaces or "funny characters", or
is an Access reserved word.
Try this instead:
CurrentDb.Execute "INSERT INTO PlantTransactionQuery (TransactionID,[Plant Number],Categories,Description,Location,TransactionDate,Opening_Hours,Closing_Hours,[Hours Worked],Fuel,[Fuel Cons Fuel/Hours],[Hour Meter Replaced],Comments) " & _
"VALUES (" & Me.txt13 & ",'" & Me.txt1 & "','" & Me.txt2 & "','" & Me.txt3 & "','" & Me.txt4 & "','" & Me.txt5 & "','" & Me.txt6 & "','" & Me.txt7 & "','" & Me.txt8 & "','" & Me.txt9 & "','" & Me.txt10 & "'," & Me.txt11 & "," & Me.txt12 & ")"
Edit
You can make SQL statements easier to verify visually if you break them up line-by-line, something like this:
CurrentDb.Execute _
"INSERT INTO [PlantTransactionQuery] (" & _
"[TransactionID], " & _
"[Plant Number], " & _
"[Categories], " & _
"[Description], " & _
"[Location], " & _
"[TransactionDate], " & _
"[Opening_Hours], " & _
"[Closing_Hours], " & _
"[Hours Worked], " & _
"[Fuel], " & _
"[Fuel Cons Fuel/Hours], " & _
"[Hour Meter Replaced], " & _
"[Comments] " & _
") VALUES (" & _
Me.txt13 & ", " & _
"'" & Me.txt1 & "', " & _
"'" & Me.txt2 & "', " & _
"'" & Me.txt3 & "', " & _
"'" & Me.txt4 & "', " & _
"#" & Me.txt5 & "#, " & _
"'" & Me.txt6 & "', " & _
"'" & Me.txt7 & "', " & _
"'" & Me.txt8 & "', " & _
"'" & Me.txt9 & "', " & _
"'" & Me.txt10 & "', " & _
Me.txt11 & ", " & _
Me.txt12 & " " & _
")"
The 3061 error message "Too few parameters..." can happen if a field (column) name is mis-spelled, or a field name that happens to be a reserved word is not enclosed in square brackets. I've edited the query above so all table and column names are bracketed, just to be safe.
Also, check that you are quoting strings and are not quoting numbers. A mistake there could also result in the aforementioned error.
As for [TransactionDate], you probably should un-bind the text box for consistency. Notice that the value for that field is enclosed in hash marks (#) instead of single quotes. You can add a "date" input mask to the text box, or you can use something like...
Format(CDate(Me.txt5), "yyyy-mm-dd")
...to format the date for you.
Finally, be aware that building SQL strings like that leaves you open to SQL injection problems, so you might also consider replacing that .Execute with something like
Dim rst as DAO.Recordset
Set rst = CurrentDB.OpenRecordset("[PlantTransactionQuery]", dbOpenTable)
rst.AddNew
rst![TransactionID] = CLng(Me.txt13)
rst![Plant Number] = Me.txt1
rst![Categories] = Me.txt2
...
rst![Comments] = Me.txt12
rst.Update