Object Required Error MS Access SQL - 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.

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';

UPDATE Query in MS Access using SQL in VBA with declared strings

been a while since I've posted so please forgive any formatting issues. Looking to update a table field with a value from another record in the same field in the same table.
I've declared two strings, and the strDeal string is coming back with the correct values when debugging. However when I introduce the string into the sql statement I can't the query to update the field. I'm not sure exactly what isn't working correctly, so any help would be appreciated.
The basics are I'm trying to update the Case_Qty field with a value from the same field in the same table based on the returned value from a subquery. Thanks!
Dim strDeal As String
Dim strSQL As String
strDeal = DMax("[Deal_No]", "[tblStructuresNoDAworking]", "[Structure_Name] = Forms!frmStructNoDASetup!Structure_Name AND [FG_Ind] = 0 AND [Deal_No] < Forms!frmStructNoDASetup!Deal_No")
strSQL = "UPDATE tblStructuresNoDAworking SET tblStructuresNoDAworking.Case_Qty = (SELECT [Case_Qty] FROM tblStructuresNoDAworking WHERE [Structure_Name] = '" & Me.Structure_Name.Value & "' AND [Deal_No] = '" & strDeal & "') WHERE [Structure_Name] = '" & Me.Structure_Name.Value & "' AND [Deal_No] = '" & Me.Deal_No.Value & "'"
DoCmd.RunSQL (strSQL)
Try this where you concatenate the values from the form:
Dim strDeal As String
Dim strSQL As String
strDeal = DMax("[Deal_No]", "[tblStructuresNoDAworking]", "[Structure_Name] = '" & Forms!frmStructNoDASetup!Structure_Name & "' AND [FG_Ind] = 0 AND [Deal_No] < '" & Forms!frmStructNoDASetup!Deal_No & "'")
strSQL = "UPDATE tblStructuresNoDAworking " & _
"SET tblStructuresNoDAworking.Case_Qty = " & _
" (SELECT [Case_Qty] " & _
" FROM tblStructuresNoDAworking " & _
" WHERE [Structure_Name] = '" & Me.Structure_Name.Value & "' AND [Deal_No] = '" & strDeal & "') " & _
"WHERE [Structure_Name] = '" & Me.Structure_Name.Value & "' AND [Deal_No] = '" & Me.Deal_No.Value & "'"
DoCmd.RunSQL strSQL
For numeric Deal:
Dim Deal As Long
Dim strSQL As String
Deal = DMax("[Deal_No]", "[tblStructuresNoDAworking]", "[Structure_Name] = '" & Forms!frmStructNoDASetup!Structure_Name & "' AND [FG_Ind] = 0 AND [Deal_No] < '" & Forms!frmStructNoDASetup!Deal_No & "'")
strSQL = "UPDATE tblStructuresNoDAworking " & _
"SET tblStructuresNoDAworking.Case_Qty = " & _
" (SELECT [Case_Qty] " & _
" FROM tblStructuresNoDAworking " & _
" WHERE [Structure_Name] = '" & Me.Structure_Name.Value & "' AND [Deal_No] = " & Deal & ") " & _
"WHERE [Structure_Name] = '" & Me.Structure_Name.Value & "' AND [Deal_No] = " & Me.Deal_No.Value & ""
DoCmd.RunSQL strSQL

VBA error matching date from table and date from query when date has leading zero eg 01/04 or 02/04

I have a query in MS Access called qryRMCountStudentsBySessionWP. The results are shown below.
I have some VBA code shown below. Basically if the Timetable_Date, Timetable_Session and Location match Exam_Date, Exam_Session and Exam_Location of a record in a table called Invigilation, then changes are made to that record's start time.
This works fabulously for 30/03/2020 and 31/03/2020 but not any of the April dates. I think this is because of leading zeros but I just can't figure out how to fix it. Any ideas?
Private Sub wordProcessing()
Dim dbs As Database
Dim name As String
Dim SQL As String
Dim rstAllSessions As Recordset
Set dbs = CurrentDb
SQL = "SELECT Timetable_Date, Timetable_Session, Location, CountOfStudent_Ref FROM qryRMCountStudentsBySessionWP;"
Set rstAllSessions = dbs.OpenRecordset(SQL)
rstAllSessions.MoveFirst
Do Until rstAllSessions.EOF ' for each sesson update invigilation table
If rstAllSessions.Fields(3) >= 10 Then
If rstAllSessions.Fields(1) = "A" Then
dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('08:30:00'), Notes = 'WP Exam with 10 or more students' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
Else
dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('13:00:00'), Notes = 'WP Exam with 10 or more students' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
End If
ElseIf rstAllSessions.Fields(3) >= 5 Then
If rstAllSessions.Fields(1) = "A" Then
dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('08:35:00'), Notes = 'WP Exam with 5 or more students' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
Else
dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('13:05:00'), Notes = 'WP Exam with 5 or more students' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
End If
ElseIf rstAllSessions.Fields(3) > 1 Then
If rstAllSessions.Fields(1) = "A" Then
dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('08:40:00'), Notes = 'WP Exam with >1 and <5 students' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
Else
dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('13:10:00'), Notes = 'WP Exam with >1 and <5 students' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
End If
Else
If rstAllSessions.Fields(1) = "A" Then
dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('08:45:00'), Notes = 'WP Exam with 1 student' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
Else
dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('13:15:00'), Notes = 'WP Exam with 1 student' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
End If
End If
' doesn't do dates with leading 0s?????
rstAllSessions.MoveNext
Loop
rstAllSessions.Close
dbs.Close
End Sub
Date values are numeric, thus no leading zeroes. But you must pass properly formatted string expressions for the date values when concatenating:
"UPDATE Invigilation SET Start_Time = TimeSerial(13,15,0), Notes = 'WP Exam with 1 student' WHERE Invigilation.Exam_Date = #" & Format(rstAllSessions!Timetable_Date, "yyyy\/mm\/dd") & "# AND Invigilation.Exam_Location = '" & rstAllSessions!Location & "' AND Invigilation.Exam_Session = '" & rstAllSessions!Timetable_Session & "';"

Access VBA using SQL UPDATE

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.

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