MS Access - UPDATE SQL Query Error 3061 - sql

I try to update data in a table with text delivered from a InputBox.
Private Sub editname_Click()
Dim GivenNameTB As String
Dim EnterName As String
Dim SQl As String
Dim LocationID As Integer
Me.txt_name.SetFocus
GivenNameTB = Me.txt_name.Text
EnterName = InputBox("Change name", "Change name", GivenNameTB)
LocationID = Me.list_ma.Column(1)
SQl = " Update tWorkers SET GivenName = forms!mainform!EnterName WHERE tWorkers.IDName = forms!mainform!LocationID "
CurrentDb.Execute SQl
End Sub
However, I get error code 3061 "Too few parameters. Expected 2"
EDIT:
The table structure of tWorkers:
IDName - auto-increment (primary key)
LastName - text
GivenName - text
I'm targeting column GivenName by SET GivenName = ..., and the row by LocationID.
LocationID gets its value from the list field list_ma. The list field consists of five columns whereas IDName is column 2.
My whole point is to update a field in a table. A text box in my form shows a name which can be edited by clicking a button. Then a inputbox pops up. The entered string should be saved in the desired field.

Re-reading your question, your data lives in VBA variables, not in form controls. So you can't read the parameters from the form (DoCmd.RunSQL won't help).
You must construct the SQL string dynamically, best using CSql() by Gustav :
SQL = "Update tWorkers SET GivenName = " & CSql(EnterName) & _
" WHERE tWorkers.IDName = " & CSql(LocationID)
Debug.Print SQL
CurrentDb.Execute SQL

I think you need DoCmd.RunSQL rather than CurrentDb.Execute.
Private Sub editname_Click()
Dim GivenNameTB As String
Dim EnterName As String
Dim SQl As String
Dim LocationID As Integer
Me.txt_name.SetFocus
GivenNameTB = Me.txt_name.Text
EnterName = InputBox("Change name", "Change name", GivenNameTB)
LocationID = Me.list_ma.Column(1)
SQL = " Update tWorkers SET GName = " & chr(34) & EnterName & chr(34) & " WHERE tWorkers.IDName = " & LocationID
Debug.Print SQL -- For checking what code we are running.
DoCmd.RunSQL SQL
End Sub

Related

How to reference data which is stored in a Table for VBA Code?

I want to run my codes which are stored in a specific field of a table in a text type.
My table contains below fields:
CodeID as int
RibbonTabID as Nvarchar(50)
ActionButtonCode as ntext
.
I generate a custom ribbon in access and name the id of this button as "B_01".
In my table, I write B_01 in RibbonTabID field and "DoCmd.OpenForm "Loginform", , , , , acDialog" in ActionButtonCode field.
I write this code in VBA to execute the action in the specific field.
Public Sub OnActionButton(control As IRibbonControl)
'Callbackname in XML File "onAction"
Select Case control.id
Case control.id
Dim RunAction As String
Dim dbHadish As DAO.Database
Dim rstEmployees As DAO.Recordset
Dim strFirstName As String
Set dbHadish = CurrentDb
Set rstEmployees = dbHadish.OpenRecordset("dbo_tblOnActionButton", dbOpenDynaset, dbSeeChanges)
rstEmployees.MoveFirst
strFirstName = rstEmployees!ActionCode
Shell (strFirstName)
Case Else
MsgBox "Button " & control.id & " click"
End Select
End Sub
This piece of does not work.
I also use Dlookup as below, but this code also does not work.
Dim RunAction As String
Set RunAction = DLookup("[ActionCode]", "[dbo_tblOnActionButton]", "[RibbonTabID] = '" & "B_10" & "'")
Eval (RunAction)

Access VBA RunSQL Error

I'm trying to insert into a table the top x records from a query data-set. However I keep getting the run-time error '3061' Too few parameters. Expected 1. I've tried a few things but it doesn't make any difference.
Private Sub UpdateGA_Click()
Dim dbs As DAO.Database
Dim strSQL As String
Dim rounds As Integer
Dim playerid As Long
Set dbs = CurrentDb
rounds = Me.NoOfScores.Value
playerid = Me.lngPlayerID.Value
strSQL = "INSERT INTO [tbl_TopRounds] ( [lngPlayerID], [lngRoundID], [dblPlayedTo], [dteRoundDate]) " & _
" SELECT TOP " & Me.NoOfScores.Value & " [lngPlayerID], [lngRoundID], [dblPlayedTo], [dteRoundDate] FROM qry_LastRounds" & _
" WHERE [lngPlayerID] = " & Me.lngPlayerID.Value & _
" ORDER BY [dblPlayedTo], [dteRoundDate] DESC;"
dbs.Execute strSQL
End Sub
I'm expecting that x (based on a form parameter) records will be written to the tbl_TopRounds.
VB Error on executing
Any help would be appreciated.
Normally this error means that you made a mistake in field name. Set breakpoint on last command, run the sub, open immediate window and type ?strSQL. Copy received SQL text and try to run it in query builder, it will show column name which doesn't exist in the table. The error may be in the query qry_LastRounds, check it first by opening

MS Access: Why am I getting an "Invalid use of property" error?

I'm getting the error in the title when using this code:
Private Sub Command12_Click()
Dim dbsCurrent As Database
Set dbsCurrent = CurrentDb
Dim query As QueryDef
Dim sql As String
item_entered = Me.Text314.Value
sql = "Update tbl_FilmZipInfo Set qty_per_unit = Me.Text317 WHERE [item] = item_entered ;"
query = CurrentDb.CreateQueryDef("UpdateFilmZip", sql)
query.Execute
End Sub
After I run the Sub, the "Private Sub Command12_Click()" line gets highlighted in yellow and the "query =" gets highlighted in blue (the same blue highlighting that a human does when copying something). Anyone know why I am getting this error? The goal here is to update a specific record in a table.
You have to use the Set keyword when assigning object variables:
Set query = CurrentDb.CreateQueryDef("UpdateFilmZip", sql)
You must concatenate correctly:
sql = "Update tbl_FilmZipInfo Set qty_per_unit = " & Me!Text317.Value & " WHERE [item] = '" & item_entered & "';"
Leave out the last quotes if item_entered is numeric.

Update with NULL for a Numeric field with a textfield in a form

I want to be able to update a numeric column to different values and also be able clear values in a field (set the field to null). I am able to update the field to any number I want but whenever I delete the value and set it to nothing, it's giving me an error.
Here's my code:
Private Sub txtPortNumber_AfterUpdate()
Dim portSQL As String
portSQL = "UPDATE Switches SET [Port Number] = " & Me.txtPortNumber & " WHERE ID = " & Me.txtID3 & ""
DoCmd.RunSQL (portSQL)
Me.Refresh
End Sub
How would you recommend me approaching this issue? I'm thinking that I need to write an IF-ELSE statement but I'm unsure of what to put.
txtID3 is a number and txtPortNumber is also numeric.
Sorry I got started on this, I thought it was VB.NET and not Access. Let me know if this works:
Private Sub txtPortNumber_AfterUpdate()
Dim portSQL As String
Dim portNumber as Integer
Dim myID as Integer
If IsNumeric(Me.txtID3) Then
myID = CInt(Me.txtID3)
If IsNumeric(Me.txtPortNumber) Then
portNumber = CInt(Me.txtPortNumber)
portSQL = "UPDATE Switches SET [Port Number] = " & portNumber & " WHERE ID = " & myID & ""
Else
portSQL = "UPDATE Switches SET [Port Number] = NULL WHERE ID = " & myID & ""
End If
DoCmd.RunSQL (portSQL)
End If
Me.Refresh
End Sub

SQL Error Access 2010 VBA Update Command

I need help figuring out an error in my SQL statement. I have been trying several things, but nothing seems to work?
This is the error message I receive
Run-time error '3075':
Syntax error (missing operator) in query expression '([description] = Manufacturing and Delivery Schedule AND [pr_num] = 83)'.
This is my code:
Private Sub Command6_Click()
' ===================================================
' Receives the selected item in the combo box
' ===================================================
' Open the Database connection
Dim data_base As Database
Set data_base = CurrentDb
' Grab description and pr number from the form
Dim desc As string
dim pr_number as long
desc = Combo4.Value
pr_number = Text8.Value
' Build the query
Dim query As String
query = "UPDATE VDR_Table " & _
"SET [received] = [p1] " & _
"WHERE ([description] = " & desc & _
" AND [pr_num] = " & pr_number & ");"
Dim rec_set As DAO.Recordset
Set rec_set = data_base.OpenRecordset(query)
' Build the QueryDef
Set qd = data_base.CreateQueryDef("")
qd.SQL = query
' Execute query
qd.Parameters("p1").Value = true
qd.Execute
' Close nad null record set
rec_set.close
set rec_set = nothing
' Close the connection to the database
data_base.Close
' Prompt the user success
MsgBox "Item has been received"
End Sub
Thanks in advance for any help!
You need to enclose the description field value you are setting in quotes since it is a string field. It should look like this:
' Build the query
Dim query As String
query = "UPDATE VDR_Table " & _
"SET [received] = [p1] " & _
"WHERE ([description] = '" & desc & _
"' AND [pr_num] = " & pr_number & ");"
Removed the links below since they don't matter in this case.
Also, I would recommend using parameters instead of string concatenations to avoid SQL injections. Here's an example of using parameters with VBA - http://support.microsoft.com/kb/181734 - and here is some reasoning on why to use parameterized sql - http://www.codinghorror.com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html.