Private Sub myComboBox_Change()
Dim startDate As Date
Dim endDate As Date
Dim period As String
Dim dayOfWeek As Integer
Dim monthOfQuarter As Integer
period = Me.myComboBox
startDate = DateSerial(Year(Date), Month(Date), Day(Date))
Select Case period
Case "Today"
endDate = DateAdd("d", 1, startDate)
'deletes all records in holding table "tblsearchengine05"
st_sql = "Delete [tblsearchengine05].[startdate]FROM tblsearchengine05"
Application.DoCmd.RunSQL (st_sql)
' updates the holding table "tblSearchEngine05" to reflect the correct start and finish date
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("tblSearchEngine05", dbOpenTable)
rst.AddNew
rst!startDate = startDate
rst!FinishDate = endDate
rst.Update
rst.Close
Set rst = Nothing
' clears the Query02DateSelect column on table tblSearchEngine01
st_sql = "UPDATE tblSearchEngine01 SET tblSearchEngine01.Query02DateSelect = NULL"
Application.DoCmd.RunSQL (st_sql)
' updates the column [tblSearchEngine01].[query02dateselect] to identify which records contain the specified date range criteria
st_sql = "UPDATE tblSearchEngine01, tblSearchEngine05 SET tblSearchEngine01.Query02DateSelect = '1' WHERE (((tblSearchEngine01.[Automatic date of entry])>=([tblSearchEngine05].[StartDate]) And (tblSearchEngine01.[Automatic date of entry])<=([tblSearchEngine05].[FinishDate])))"
Application.DoCmd.RunSQL (st_sql)
How to execute an update SQL query with a where statement referencing values in another table
-
16-07-2023 - |
Question
I would like to update records in a table using and SQL query which uses a where statement referencing another values located in another table.
In the example below, i have taken values according to a select query i built and relocated them to a table called "tblSearchEngine05". At that point, i am trying to update a column in table "tblSearchEngin01" using a conditional query based on the values located in "tblSEarchEngine05". I am trying to use the two populated fields [tblSearchEngine05].[enddate] and [tblSearchEngine05].[startdate] as the values for the condition. Unfortunately there seems to be a syntax error message.
Private Sub myComboBox_Change()
Dim startDate As Date
Dim endDate As Date
Dim period As String
Dim dayOfWeek As Integer
Dim monthOfQuarter As Integer
period = Me.myComboBox
startDate = DateSerial(Year(Date), Month(Date), Day(Date))
Select Case period
Case "Today"
endDate = DateAdd("d", 1, startDate)
'deletes all records in holding table "tblsearchengine05"
st_sql = "Delete [tblsearchengine05].[startdate]FROM tblsearchengine05"
Application.DoCmd.RunSQL (st_sql)
' updates the holding table "tblSearchEngine05" to reflect the correct start and finish date
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("tblSearchEngine05", dbOpenTable)
rst.AddNew
rst!startDate = startDate
rst!FinishDate = endDate
rst.Update
rst.Close
Set rst = Nothing
' clears the Query02DateSelect column on table tblSearchEngine01
st_sql = "UPDATE tblSearchEngine01 SET tblSearchEngine01.Query02DateSelect = NULL"
Application.DoCmd.RunSQL (st_sql)
' updates the column [tblSearchEngine01].[query02dateselect] to identify which records contain the specified date range criteria
st_sql = "UPDATE tblSearchEngine01, tblSearchEngine05 SET tblSearchEngine01.Query02DateSelect = '1' WHERE (((tblSearchEngine01.[Automatic date of entry])>=([tblSearchEngine05].[StartDate]) And (tblSearchEngine01.[Automatic date of entry])<=([tblSearchEngine05].[FinishDate])))"
Application.DoCmd.RunSQL (st_sql)
........
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow