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

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

Related

How concatenate recordset variables to use filter method - Vba

I would like to concatenate multiple variables on my recordset filter. Here more information :
rs is recordset
titre can be M. or Mme or S. (come from rs)
Nom is of the form (come from rs) : FirstName LastName (with space between)
but I can't. I tried :
space = " "
rs.Filter = "[titre+ space + Nom] = '" & oLookFullName & "' and nomEntreprise = '" & objContact.CompanyName & "'"
Concatenation = rs!titre + " " + rs!Nom
rs.Filter = "Concatenation = '" & oLookFullName & "'"
Any ideas ?
EDIT
#Gustav, I tried your code with split and it seems the filter contains the correct value but just after I have this if loop and in this case rs.EOF is true while the contact exists... why ?
If rs.EOF Then 'Or (rs.BOF = True) Then
Debug.Print oLookFullName & " is not found."
End If
Try with:
rs.Filter = "[titre] & ' ' & [Nom] = '" & oLookFullName & "' And [nomEntreprise] = '" & objContact.CompanyName & "'"
or:
rs.Filter = "[titre] = '" & Split(oLookFullName, " ")(0) & "' And [Nom] = '" & Split(oLookFullName, " ")(1) & "' And [nomEntreprise] = '" & objContact.CompanyName & "'"

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.

Passing an Empty/Null Date variable in VBA to an SQL UPDATE statement

I have an excel userform with various textboxes, some are fields to enter dates. The user can then save their entries.
At this point, I connect to an access backend via an ADO connection. The values entered by a user are passes to an SQL string, e.g.
strSQL = "UPDATE tblDECONVERSION_DATA SET tblDECONVERSION_DATA.Status = '" & NewBusiness_WorkQueue.Decon_CaseStatus & "', " & _
"tblDECONVERSION_DATA.DMS = '" & NewBusiness_WorkQueue.Decon_DMS & "', " & _
"tblDECONVERSION_DATA.DateRecieved = #" & Format(NewBusiness_WorkQueue.Decon_DateRecieved, "mm/dd/yyyy") & "#, " & _
"tblDECONVERSION_DATA.WireDate = #" & Format(NewBusiness_WorkQueue.Decon_WireDate, "mm/dd/yyyy") & "#, " & _
"tblDECONVERSION_DATA.LastEditXID = '" & CurrUser & "', tblDECONVERSION_DATA.LastEditDate = #" & Now & "# " & _
"WHERE (((tblDECONVERSION_DATA.CaseID)=" & ID & "));"
adoRecSet.Open Source:=strSQL, ActiveConnection:=dbconnect, CursorType:=adOpenDynamic, LockType:=adLockOptimistic
However, some of the date fields can be left blank, meaning for example the NewBusiness_WorkQueue.Decon_DateRecieved variable being empty. This causes a syntax error. How can I pass a Null or Empty date variable in the SQL statement that both VBA and the access database will accept?
strSQL = "UPDATE tblDECONVERSION_DATA SET tblDECONVERSION_DATA.Status = '" & _
NewBusiness_WorkQueue.Decon_CaseStatus & "', " & _
"tblDECONVERSION_DATA.DMS = '" & NewBusiness_WorkQueue.Decon_DMS & "', " & _
"tblDECONVERSION_DATA.DateRecieved = " & _
DateOrNull(NewBusiness_WorkQueue.Decon_DateRecieved) & ", " & _
"tblDECONVERSION_DATA.WireDate = " & _
DateOrNull(NewBusiness_WorkQueue.Decon_WireDate) & ", " & _
"tblDECONVERSION_DATA.LastEditXID = '" & CurrUser & _
"', tblDECONVERSION_DATA.LastEditDate = #" & Now & "# " & _
"WHERE tblDECONVERSION_DATA.CaseID=" & ID & ";"
An example function:
Function DateOrNull(v) As String
Dim rv as String
If IsDate(v) Then
rv = " #" & Format(v, "mm/dd/yyyy") & "# "
Else
rv = " null "
End If
DateOrNull = rv
End Function

SQL UPDATE Statement in excel VBA not working

I have this code to update database with SQL but it is not working
Call Connect_to_db
strSQL = "UPDATE StockTable " & _
"SET StockTable.Selected = '" & Sheets("InfoStockDes").Range("g" & x) & "' " & _
"WHERE OwnerName = '" & Sheets("InfoStockDes").Range("a" & x) & WHERE OwnerShipMethod = Sheets("InfoStockDes").Range("b" & g) & WHERE StockName = Sheets("InfoStockDes").Range("c" & g) & WHERE Quantity = Sheets("InfoStockDes").Range("d" & g) "' "
cn.Execute strSQL
Call Close_db
This is your code as posted, but then with some extra linebreaks and outlining. Maybe you can spot the obvious errors yourself better now?
Call Connect_to_db
strSQL = "UPDATE StockTable " & _
"SET StockTable.Selected = '" & Sheets("InfoStockDes").Range("g" & x) & "' " & _
"WHERE OwnerName = '" & Sheets("InfoStockDes").Range("a" & x) & _
WHERE OwnerShipMethod = Sheets("InfoStockDes").Range("b" & g) & _
WHERE StockName = Sheets("InfoStockDes").Range("c" & g) & _
WHERE Quantity = Sheets("InfoStockDes").Range("d" & g) "' "
cn.Execute strSQL
Call Close_db
In this version I have changed the extra WHERE clauses to AND, and I haev added some obviously missing " and '.
Call Connect_to_db
strSQL = "UPDATE StockTable " & _
"SET StockTable.Selected = '" & Sheets("InfoStockDes").Range("g" & x) & "' " & _
"WHERE OwnerName = '" & Sheets("InfoStockDes").Range("a" & x) & "' " & _
"AND OwnerShipMethod = '" & Sheets("InfoStockDes").Range("b" & g) & "' " & _
"AND StockName = '" & Sheets("InfoStockDes").Range("c" & g) & "' " & _
"AND Quantity = '" & Sheets("InfoStockDes").Range("d" & g) & "' "
cn.Execute strSQL
Call Close_db
I guess this shoudl work better allready, if it doesn't please tell us what is not working, what error messages, if any, you are getting, and please show the actual content of strSQL before yuo execute it. That string should contain a straightforward SQL statement, but there might be errors in it, so have a look at that and, if necessary, post it for us to look at :)