Parameters Error MS Access SQL - sql

I'm getting a Parameters error here: db.Execute strSQL, dbFailOnError saying "too few parameters, expected 1". I'm not sure exactly why. I'm referencing both tables. I'm also not 100% this is written correctly Workername = " & DLookup("username", "attendance", GetNextAssignee("program", "Language", "username")) I want [Workername] field to update to the workername that is linked to the GetNextAssignee("program", "Language", "username") which I'm not getting and it could be connected to the this error.
Public Function AssignNullProjects() As Long
Dim db As dao.Database
Dim rs As dao.Recordset
Dim strSQL As String
Set db = CurrentDb
strSQL = "SELECT CFRRRID, [program], [language] FROM CFRRR WHERE assignedto Is Null"
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
If Not rs.BOF And Not rs.EOF Then
While Not rs.EOF
strSQL = "UPDATE CFRRR,attendance SET assignedto = " & GetNextAssignee("program", "Language", "username") & ", assignedby = " & Forms!Supervisor!NavigationSubform!assignedby.Value & ", Dateassigned = #" & Now & "#, actiondate = #" & Now & "#, Workername = " & _
DLookup("username", "attendance", GetNextAssignee("program", "Language", "username")) & ", WorkerID = " & DLookup("UserID", "attendance", GetNextAssignee("program", "Language", "username")) & " WHERE CFRRRID = " & rs!CFRRRID
Debug.Print strSQL
db.Execute strSQL, dbFailOnError
rs.MoveNext
Wend
End If
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End Function
Here is what the Debug.Print strSQL shows:
UPDATE CFRRR,attendance
SET
assignedto = 7,
assignedby = 33,
Dateassigned = #5/16/2015 11:16:31 AM#,
actiondate = #5/16/2015 11:16:31 AM#,
Workername = Lillian,
WorkerID = 6
WHERE CFRRRID = 6

It seems Workername is a text field and you want to store the string Lillian there. Add quotes so the db engine will understand Lillian is literal text instead of the name of a parameter.
UPDATE CFRRR
SET
assignedto = 7,
assignedby = 33,
Dateassigned = Now(),
actiondate = Now(),
Workername = 'Lillian',
WorkerID = 6
WHERE CFRRRID = 6
Since the db engine supports the Now() function, you can ask it to store the value of Now() in your Dateassigned and actiondate fields. That is simpler than taking the value of Now() in VBA and then adding # characters around that value to concatenate into the statement text.
I'm skeptical that UPDATE CFRRR,attendance was the right choice. That would be a CROSS JOIN between the two tables and Access may therefore decide the query is not updateable. I suggest you UPDATE just the CFRRR table.

Related

Update SQL MS Access 2010

This is wrecking my brains for 4 hours now,
I have a Table named BreakSked,
and I this button to update the table with the break end time with this sql:
strSQL1 = "UPDATE [BreakSked] SET [BreakSked].[EndTime] = " & _
Me.Text412.Value & " WHERE [BreakSked].AgentName = " & Me.List423.Value _
& " AND [BreakSked].ShiftStatus = '1'"
CurrentDB.Execute strSQL1
Text412 holds the current system time and List423 contains the name of the person.
I'm always getting this
"Run-time error 3075: Syntax Error (missing operator) in query
expression '03:00:00 am'
Any help please?
EDIT: Thanks, now my records are updating. But now its adding another record instead of updating the record at hand. I feel so silly since my program only has two buttons and I can't figure out why this is happening.
Private Sub Form_Load()
DoCmd.GoToRecord , , acNewRec
End Sub
Private Sub Command536_Click()
strSQL1 = "UPDATE BreakSked SET BreakSked.EndTime = '" & Me.Text412.Value & "',BreakSked.Duration = '" & durationz & "' " & vbCrLf & _
"WHERE (([BreakSked].[AgentID]='" & Me.List423.Value & "'));"
CurrentDb.Execute strSQL1
CurrentDb.Close
MsgBox "OK", vbOKOnly, "Added"
End Sub
Private Sub Command520_Click()
strSql = "INSERT INTO BreakSked (ShiftDate,AgentID,StartTime,Status) VALUES ('" & Me.Text373.Value & "', '" & Me.List423.Value & "', '" & Me.Text373.Value & "','" & Me.Page657.Caption & "')"
CurrentDb.Execute strSql
CurrentDb.Close
MsgBox "OK", vbOKOnly, "Added"
End Sub
You wouldn't need to delimit Date/Time and text values if you use a parameter query.
Dim strUpdate As String
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
strUpdate = "PARAMETERS pEndTime DateTime, pAgentName Text ( 255 );" & vbCrLf & _
"UPDATE BreakSked AS b SET b.EndTime = [pEndTime]" & vbCrLf & _
"WHERE b.AgentName = [pAgentName] AND b.ShiftStatus = '1';"
Debug.Print strUpdate ' <- inspect this in Immediate window ...
' Ctrl+g will take you there
Set db = CurrentDb
Set qdf = db.CreateQueryDef("", strUpdate)
qdf.Parameters("pEndTime").Value = Me.Text412.Value
qdf.Parameters("pAgentName").Value = Me.List423.Value
qdf.Execute dbFailOnError
And if you always want to put the current system time into EndTime, you can use the Time() function instead of pulling it from a text box.
'qdf.Parameters("pEndTime").Value = Me.Text412.Value
qdf.Parameters("pEndTime").Value = Time() ' or Now() if you want date and time
However, if that is the case, you could just hard-code the function name into the SQL and dispense with one parameter.
"UPDATE BreakSked AS b SET b.EndTime = Time()" & vbCrLf & _
As I said in my comment you need to wrap date fields in "#" and string fields in escaped double quotes
strSQL1 = "UPDATE [BreakSked] SET [BreakSked].[EndTime] = #" & _
Me.Text412.Value & "# WHERE [BreakSked].AgentName = """ & Me.List423.Value & _
""" AND [BreakSked].ShiftStatus = '1'"

Why is an action query not working in access VBA?

I tested an UPDATE query in Access's query design, and it works, but when I try to use it in my module, I get the error:
Invalid SQL statement; expected... or 'UPDATE'.
My query:
strSql = "UPDATE " & rs.Fields("tableName") & _
" SET " & rs.Fields("foreignKeyName") & " = " & rsContacts.Fields("contactId") & _
" WHERE contactId = " & ContactID
rs: a table that has tableName, foriegnKeyName of the tables to update
rsContacts: a list of contactIds (currently standing on a particular one).
The actual string comes out like this:
UPDATE myTable SET ContactId = 5 WHERE contactId = 2
If the query works, and it is an action query, why am I getting this error?
This is my full code:
Public Sub updateChildTables(ByVal ContactID As Long, ByVal CompanyID As Long)
Dim strSql As String
Dim rs As Recordset
Dim rsPending As Recordset
strSql = "SELECT contactID FROM contacts _
WHERE companyId = " & CompanyID & " and contactId <> " & ContactID
Set rs = CurrentDb.OpenRecordset(strSql)
If Not (rs.BOF And rs.EOF) Then
rs.MoveFirst
strSql = "SELECT * FROM childTables"
Set rsChild = CurrentDb.OpenRecordset(strSql)
rsChild.MoveFirst
Do While Not rsChild.EOF
strSql = "UPDATE " & rsChild.Fields("tableName") & " SET " & rsChild.Fields("foreignKeyName") & " = " & rs.Fields("contactId") & " WHERE contactId = " & ContactID
DoCmd.RunSQL strSql
rs.moveNext
Loop
rsChild.Close
Set rsChild = Nothing
End If
Here is my idea for debugging and possibly even resolving this.
Create a query from within Access normally -- name it UpdateMyTable, for the sake of this example.
Then, rather than using the DoCmd, actually execute this specific query from your VBA.
Dim qry As QueryDef
strSql = "UPDATE " & rsChild.Fields("tableName") & " SET " & _
rsChild.Fields("foreignKeyName") & " = " & _
rs.Fields("contactId") & " WHERE contactId = " & ContactID
Set qry = CurrentDb.QueryDefs("UpdateMyTable")
qry.SQL = strSql
qry.Execute
The big advantage of this is that you can very easily debug this from within Access to both see the rendered SQL and manually run it / tweak it.

String error in SQL MS Access

I'm not sure if my title is 100% accurate, but I think that is the problem in my code. I got this code working, but when I made changes to other portions of my code and it stopped working. Here's the full SQL:
UPDATE CFRRR
SET assignedto = " & GetNextAssignee("program", "Language", "username") & ",
assignedby = '"
& Forms!Supervisor!NavigationSubform!assignedby.Value
& "', Dateassigned = #"
& Now & "#, actiondate = #"
& Now & "#, Workername = '"
& DLookup("username", "attendance", "userID = "
& GetNextAssignee("program", "Language", "username"))
& "', WorkerID = " & DLookup("userID", "attendance", "userID = "
& GetNextAssignee("program", "Language", "username"))
& " WHERE CFRRRID = "
& rs!CFRRRID
The error I am getting is here:
Workername = '"
& DLookup("username", "attendance", "userID = "
& GetNextAssignee("program", "Language", "username"))
This is the output I am getting:
UPDATE CFRRR SET assignedto = 6,
assignedby = '33',
Dateassigned = #5/17/2015 7:46:40 PM#,
actiondate = #5/17/2015 7:46:40 PM#,
Workername = 'Valentino',
WorkerID = 7 WHERE CFRRRID = 40
This is the output I should be getting:
UPDATE CFRRR SET assignedto = 6,
assignedby = '33',
Dateassigned = #5/17/2015 7:46:40 PM#,
actiondate = #5/17/2015 7:46:40 PM#,
Workername = 'John',
WorkerID = 6 WHERE CFRRRID = 40
And here:
WorkerID = "
& DLookup("userID", "attendance", "userID = "
& GetNextAssignee("program", "Language", "username"))
Here is the GetNextAssignee code for reference:
Public Function GetNextAssignee(program As String,
language As String,
username As String) As Long
Dim db As dao.Database
Dim rs As dao.Recordset
Dim strSQL As String
Set db = CurrentDb
strSQL = "SELECT TOP 1
userID
FROM attendance as a,
CFRRR WHERE a.Status = 'Available'
AND a.Programs LIKE CFRRR.program
AND a.Language = CFRRR.language
ORDER BY TS ASC, userID, CFRRRID"
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
If Not rs.BOF And Not rs.EOF Then
strSQL = "UPDATE attendance
SET TS= " & DMax("[TS]", "attendance") + 1
& " WHERE [userID]=" & rs!userID
& " AND [Status]=""Available"""
db.Execute strSQL, dbFailOnError
GetNextAssignee = rs!userID
Else
GetNextAssignee = 0
End If
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End Function
Change your code as below
Dim i as long
and inside your while loop of AssignNullProjects change the query like this
If Not rs.BOF And Not rs.EOF Then
While Not rs.EOF
i = GetNextAssignee("program", "Language", "username")
strSQL = "UPDATE CFRRR SET assignedto = " & i & ",
assignedby = '" & Forms!Supervisor!NavigationSubform!assignedby.Value
& "', Dateassigned = #" & Now & "#, actiondate = #"
& Now & "#, Workername = '"
& _DLookup("username", "attendance", "userID = " & i)
& "', WorkerID = " & i & " WHERE CFRRRID = " & rs!CFRRRID
Debug.Print strSQL
db.Execute strSQL, dbFailOnError
rs.MoveNext
Wend
End If

form not found error. MS Access SQL

I'm getting an error saying that Access cannot find the referenced form CFRRR but there is definitely a form there. Not sure if I'm just not writing the code correctly.
Public Function AssignNullProjects() As Long
Dim db As dao.Database
Dim rs As dao.Recordset
Dim strSQL As String
Set db = CurrentDb
strSQL = "SELECT CFRRRID, [program], [language] FROM CFRRR WHERE assignedto Is Null"
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
If Not rs.BOF And Not rs.EOF Then
While Not rs.EOF
strSQL = "UPDATE CFRRR SET assignedto = " & GetNextAssignee & ", assignedby = " & [Forms]![CFRRR]![assignedby] & ", Dateassigned = #" & Now & "#, actiondate = #" & Now & "#, Workername = " & _ [Forms]![CFRRR]![assignedto] & ", WorkerID = " & [Forms]![CFRRR]![assignedto] & " WHERE CFRRRID = " & rs!CFRRRID
db.Execute strSQL, dbFailOnError
rs.MoveNext
Wend
End If
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End Function
Public Function GetNextAssignee(program As String, Language As String) As Long
' Returns UserID as a Long Integer with the lowest [TS] value,
' and updates same [TS] by incremented with 1.
Dim db As dao.Database
Dim rs As dao.Recordset
Dim strSQL As String
Set db = CurrentDb
strSQL = "SELECT TOP 1 WorkerID FROM attendance WHERE [Programs] LIKE '*" & program & "*' AND [Language] = '" & Language & "' AND [Status] = '" & Available & "' ORDER BY TS ASC"
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
If Not rs.BOF And Not rs.EOF Then
'Found next assignee, update date/time stamp
'strSQL = "UPDATE tblUser SET TS = " & DMax("[TS]", tblUser) + 1 & " WHERE [WorkerID]= " & rs!workerid
strSQL = "UPDATE attendance SET TS = " & DMax("[TS]", "attendance") + 1 & " WHERE [WorkerID]= " & rs!workerid
db.Execute strSQL, dbFailOnError
GetNextAssignee = rs!workerid
Else
'Field TS has NO VALUE FOR ALL RECORDS!
'Code calling this function should check for a return of 0 indicating an error.
GetNextAssignee = 0
End If
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End Function

Object Required Error MS Access SQL

I'm stuck in this SQL statement in my VBA coding. I'm trying to complete an update query with an DLookUp but I may not have written the code correctly. Basically I want to WorkerID and Workername to come from the DLookUp from the UserID used to get the data here GetNextAssignee("program", "Language", "username"). I'm getting an object required error here:
"UPDATE CFRRR,attendance SET assignedto = " & GetNextAssignee("program", "Language", "username") & ", assignedby = " & Forms!Supervisor!NavigationSubform!assignedby.Value & ", Dateassigned = #" & Now & "#, actiondate = #" & Now & "#, Workername = " & DLookup(attendance.username, "attendance", "username = UserID") & ", WorkerID = " & DLookup(attendance.userID, "attendance", "WorkerID = UserID") & " WHERE CFRRRID = " & rs!CFRRRID
Here's the full code for context:
Set db = CurrentDb
strSQL = "SELECT CFRRRID, [program], [language] FROM CFRRR WHERE assignedto Is Null"
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
If Not rs.BOF And Not rs.EOF Then
While Not rs.EOF
strSQL = "UPDATE CFRRR,attendance SET assignedto = " & GetNextAssignee("program", "Language", "username") & ", assignedby = " & Forms!Supervisor!NavigationSubform!assignedby.Value & ", Dateassigned = #" & Now & "#, actiondate = #" & Now & "#, Workername = " & _
DLookup(attendance.username, "attendance", "username = UserID") & ", WorkerID = " & DLookup(attendance.userID, "attendance", "WorkerID = UserID") & " WHERE CFRRRID = " & rs!CFRRRID
Debug.Print strSQL
db.Execute strSQL, dbFailOnError
rs.MoveNext
Wend
End If
Thank you!
Your DLookup expressions are not correct.
The target field of the DLookup is a string-expression as well. So it should be:
... & DLookup("username", "attendance", "username = UserID") & ...
There are multiple occurances of this error in your SQL.
In addition to that, you should check the criteria expression as well:
..."username = UserID"...
That looks suspicious to me. But I can't tell if it is correct or not without knowing the table and data structure.