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
Related
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
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 & "';"
Can the query in the code below be converted to Select / Like / Or ?
Private Sub cmdQDef_Click()
Dim qd As DAO.QueryDef, db As DAO.Database
Dim ssql As String, WhereName As String, WhereTitle As String
Set db = CurrentDb
If Me.FilterName & "" = "" Then
DoCmd.OpenQuery "q_Search_qdef"
Exit Sub
Else
End If
ssql = "Select * From Employees"
Set qd = db.QueryDefs("q_Search_qdef")
WhereName = "'" & Replace(Me.FilterName, ",", "','") & "'"
WhereTitle = "'" & Replace(Me.FilterTitle, ",", "','") & "'"
ssql = ssql & " Where [First name] In(" & WhereName & ")AND " & _
"[Job Title] In (" & WhereTitle & ")"
qd.SQL = ssql
DoCmd.OpenQuery "q_Search_qdef"
End Sub
Yes:
WhereName = "'*" & Replace(Me.FilterName, ",", "','") & "*'"
WhereTitle = "'" & Replace(Me.FilterTitle, ",", "','") & "'"
ssql = ssql & " Where ([First name] Like " & WhereName & ") OR " & _
"([Job Title] In (" & WhereTitle & ")"
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.
I have this code work for deleting one or multiple records from order table when user select order from checkboxes. Now that I have inventory module, I want my inventory record update qty (add the product quantity back to current inventory) once the order had been canceled.
Actually, this shouldn't be problem if the code was implement for delete one record at a time. However, this deleting code is made for deleting multiple records which, with my skill, I couldn't figure out how to add another updating Sql command into. So could you please help me. Thank you very much.
Below is my existing code..
<%
call navigation
url = main_area & "?" & "page=" & page_current
return_page = "../backend/" & url
req_status = request("type")
if req_status = "restore" then
req_status = False
else
req_status = True
end if
record = request("bill_id")
timestamp = now()
record = trim(record)
if len(record) > 3 then ' multiple records was selected
arrVals = split(record,",")
strSql = ""
strSql = strSql & "DELETE * FROM tbl_bill_total WHERE "
for i = 0 to ubound(arrVals)
if i = 0 then
strSql = strSql & "bill_id IN ("& trim(arrVals(i)) & " "
else
strSql = strSql & ","& trim(arrVals(i)) & ""
end if
next
strSql = strSql & ") "
strSql2 = strSql2 & "DELETE * FROM tbl_order WHERE "
for t = 0 to ubound(arrVals)
if t = 0 then
strSql2 = strSql2 & " tbl_order.bill_id IN ("& trim(arrVals(t)) & " "
else
strSql2 = strSql2 & ","& trim(arrVals(t)) & ""
end if
next
strSql2 = strSql2 & "); "
else
strSql = "DELETE * FROM tbl_bill_total WHERE bill_id=" & record & " "
strSql2 = "DELETE * FROM tbl_order WHERE bill_id =" & record & " "
end if
Call DBConnOpen()
Set Rs = Server.CreateObject("ADODB.Recordset")
response.write strSql
conn.Execute strSql
conn.Execute strSql2
Call DBConnClose()
response.redirect return_page
%>
and this is the SQL statement that I want to add into. Since it need pd_id to execute, I think this should be execute before execute the above SQL statements.
Set rsOrder = conn.Execute("SELECT * FROM tbl_order WHERE bill_id = " & record & "" )
pd_id = rsOrder.fields.item("pd_id")
od_qty = rsOrder.fields.item("od_qty")
Set rsInventory = conn.Execute("UPDATE tbl_inventory SET inv_qty_act = inv_qty_act + " & od_qty & ", inv_date = " & date() & " WHERE pd_id = '" & pd_id & "'" )
(Working Code)
With #John provided solution now it can update qty back to database for one/multiple records selected.
Below is the working code that had eliminated addition ')'
<%
Call DBConnOpen()
Set Rs = Server.CreateObject("ADODB.Recordset")
call navigation
url = main_area & "?" & "page=" & page_current
return_page = "../backend/" & url
req_status = request("type")
if req_status = "restore" then
req_status = False
else
req_status = True
end if
record = request("bill_id")
timestamp = now()
record = trim(record)
if len(record) > 3 then ' multiple records was selected
arrVals = split(record,",")
strSql = ""
strSql = strSql & "DELETE * FROM tbl_bill_total WHERE "
for i = 0 to ubound(arrVals)
if i = 0 then
strSql = strSql & "bill_id IN ("& trim(arrVals(i)) & " "
else
strSql = strSql & ","& trim(arrVals(i)) & ""
end if
next
strSql = strSql & ") "
strSql2 = strSql2 & "DELETE * FROM tbl_order WHERE "
for t = 0 to ubound(arrVals)
Set rsOrder = conn.Execute("SELECT * FROM tbl_order WHERE bill_id = " & arrVals(t) & "")
pd_id = rsOrder.fields.item("pd_id")
od_qty = rsOrder.fields.item("od_qty")
od_qty = DzToPcs(od_qty)
conn.Execute("UPDATE tbl_inventory SET inv_qty_act = inv_qty_act + " & od_qty & ", inv_date = " & date() & " WHERE pd_id = '" & pd_id & "'" )
if t = 0 then
strSql2 = strSql2 & " tbl_order.bill_id IN ("& trim(arrVals(t)) & " "
else
strSql2 = strSql2 & ","& trim(arrVals(t)) & ""
end if
next
strSql2 = strSql2 & "); "
' response.Write "strSql3 = " & strSql3
else
Set rsOrder = conn.Execute("SELECT * FROM tbl_order WHERE bill_id = " & record & " ")
pd_id = rsOrder.fields.item("pd_id")
od_qty = rsOrder.fields.item("od_qty")
od_qty = DzToPcs(od_qty)
conn.Execute("UPDATE tbl_inventory SET inv_qty_act = inv_qty_act + " & od_qty & ", inv_date = date() WHERE pd_id = '" & pd_id & "'" )
strSql = "DELETE * FROM tbl_bill_total WHERE bill_id=" & record & " "
strSql2 = "DELETE * FROM tbl_order WHERE bill_id =" & record & " "
end if
'Call DBConnOpen() --> move to top line
'Set Rs = Server.CreateObject("ADODB.Recordset") --> move to top line
'response.write strSql2
conn.Execute strSql
conn.Execute strSql2
Call DBConnClose()
response.redirect return_page
%>
I would suggest that you do the inventory reversal in the loop, something like this:
<%
call navigation
url = main_area & "?" & "page=" & page_current
return_page = "../backend/" & url
req_status = request("type")
if req_status = "restore" then
req_status = False
else
req_status = True
end if
record = request("bill_id")
timestamp = now()
record = trim(record)
if len(record) > 3 then ' multiple records was selected
arrVals = split(record,",")
strSql = ""
strSql = strSql & "DELETE * FROM tbl_bill_total WHERE "
for i = 0 to ubound(arrVals)
if i = 0 then
strSql = strSql & "bill_id IN ("& trim(arrVals(i)) & " "
else
strSql = strSql & ","& trim(arrVals(i)) & ""
end if
next
strSql = strSql & ") "
strSql2 = strSql2 & "DELETE * FROM tbl_order WHERE "
for t = 0 to ubound(arrVals)
Set rsOrder = conn.Execute("SELECT * FROM tbl_order WHERE bill_id = " & arrVals(t)) & "" )
pd_id = rsOrder.fields.item("pd_id")
od_qty = rsOrder.fields.item("od_qty")
conn.Execute("UPDATE tbl_inventory SET inv_qty_act = inv_qty_act + " & od_qty & ", inv_date = " & date() & " WHERE pd_id = '" & pd_id & "'" )
if t = 0 then
strSql2 = strSql2 & " tbl_order.bill_id IN ("& trim(arrVals(t)) & " "
else
strSql2 = strSql2 & ","& trim(arrVals(t)) & ""
end if
next
strSql2 = strSql2 & "); "
else
Set rsOrder = conn.Execute("SELECT * FROM tbl_order WHERE bill_id = " & record & "" )
pd_id = rsOrder.fields.item("pd_id")
od_qty = rsOrder.fields.item("od_qty")
conn.Execute("UPDATE tbl_inventory SET inv_qty_act = inv_qty_act + " & od_qty & ", inv_date = " & date() & " WHERE pd_id = '" & pd_id & "'" )
strSql = "DELETE * FROM tbl_bill_total WHERE bill_id=" & record & " "
strSql2 = "DELETE * FROM tbl_order WHERE bill_id =" & record & " "
end if
Call DBConnOpen()
Set Rs = Server.CreateObject("ADODB.Recordset")
response.write strSql
conn.Execute strSql
conn.Execute strSql2
Call DBConnClose()
response.redirect return_page
%>