cheekybuddha
AWF VIP
- Local time
- Today, 05:38
- Joined
- Jul 21, 2014
- Messages
- 2,753
I don't think the "upsert" works with SQL server but someone will correct me if I am wrong.
T-SQL MERGE statement
I don't think the "upsert" works with SQL server but someone will correct me if I am wrong.
That means you need a timer event running 24/7 to identify when new files have been added.Perhaps you could store the files incrementally in a table as they are generated/come into existence,
Dim strMsg As String
Dim strSql As String
Dim rsFilesDead As DAO.Recordset
'Only do the section below if the user did NOT choose a directory to search.
If Not Me.chkDir Then
'SQL below is to identify all records not found in active directory search
strSql = "SELECT tblClips.ID, tblClips.blnAlive " & _
"FROM tblClips LEFT JOIN rsFilesFound ON tblClips.fldLocation = rsFilesFound.fldFileAddress " & _
"WHERE (((tblClips.blnAlive)=True) AND ((rsFilesFound.fldFileAddress) Is Null));"
Set rsFilesDead = CurrentDb.OpenRecordset(strSql)
If rsFilesDead.RecordCount > 0 Then
rsFilesDead.MoveLast
rsFilesDead.MoveFirst
End If
strMsg = MsgBox("The SQL Query contains " & rsFilesDead.RecordCount & " references.", vbOKCancel, "TESTING")
If strMsg = vbCancel Then Exit Sub
'Insert UPDATE Function on rsFilesDead to set the blnAlive field to FALSE.
End If
rsFilesFound
is an odd name for a table or query!Dim strMsg As String
Dim strSql As String
If Not Me.chkDir Then
'SQL below is to count all records not found in active directory search
strSql = "SELECT COUNT(*) " & _
"FROM tblClips c LEFT JOIN rsFilesFound ff ON c.fldLocation = ff.fldFileAddress " & _
"WHERE c.blnAlive = True AND ff.fldFileAddress IS NULL;"
With CurrentDb.OpenRecordset(strSql)
strMsg = "The SQL Query contains " & .Fields(0) & " references."
.Close
End With
If MsgBox(strMsg, vbOKCancel, "TESTING") = vbCancel Then Exit Sub
strSQL = "UPDATE tblClips c1 " & _
"SET c1.blnAlive = False " & _
"WHERE c1.ID IN (" & _
"SELECT c2.ID " & _
"FROM tblClips c2 LEFT JOIN rsFilesFound ff ON c2.fldLocation = ff.fldFileAddress " & _
"WHERE c2.blnAlive = True AND ff.fldFileAddress IS NULL" & _
");"
CurrentDb.Execute strSQL, dbFailOnError
End If
Wow. Your code worked perfectly on the first go. I clearly have much more to learn in the land of SQL. Thank you very much for the help.rsFilesFound
is an odd name for a table or query!
Try:
Code:Dim strMsg As String Dim strSql As String If Not Me.chkDir Then 'SQL below is to count all records not found in active directory search strSql = "SELECT COUNT(*) " & _ "FROM tblClips c LEFT JOIN rsFilesFound ff ON c.fldLocation = ff.fldFileAddress " & _ "WHERE c.blnAlive = True AND ff.fldFileAddress IS NULL;" With CurrentDb.OpenRecordset(strSql) strMsg = "The SQL Query contains " & .Fields(0) & " references." .Close End With If MsgBox(strMsg, vbOKCancel, "TESTING") = vbCancel Then Exit Sub strSQL = "UPDATE tblClips c1 " & _ "SET c1.blnAlive = False " & _ "WHERE c1.ID IN (" & _ "SELECT c2.ID " & _ "FROM tblClips c2 LEFT JOIN rsFilesFound ff ON c2.fldLocation = ff.fldFileAddress " & _ "WHERE c2.blnAlive = True AND ff.fldFileAddress IS NULL" & _ ");" CurrentDb.Execute strSQL, dbFailOnError End If
So a question. Was it necessary to do the c1 alias in the creation of the 2nd sql statement, or was that a convenience to aid in the readability of the code? I'm just trying to understand if the SQL alias in the first statement construction could NOT be reused during the SQL statement second construction... Sorry, I may just be confusedrsFilesFound
is an odd name for a table or query!
Try:
Code:Dim strMsg As String Dim strSql As String If Not Me.chkDir Then 'SQL below is to count all records not found in active directory search strSql = "SELECT COUNT(*) " & _ "FROM tblClips c LEFT JOIN rsFilesFound ff ON c.fldLocation = ff.fldFileAddress " & _ "WHERE c.blnAlive = True AND ff.fldFileAddress IS NULL;" With CurrentDb.OpenRecordset(strSql) strMsg = "The SQL Query contains " & .Fields(0) & " references." .Close End With If MsgBox(strMsg, vbOKCancel, "TESTING") = vbCancel Then Exit Sub strSQL = "UPDATE tblClips c1 " & _ "SET c1.blnAlive = False " & _ "WHERE c1.ID IN (" & _ "SELECT c2.ID " & _ "FROM tblClips c2 LEFT JOIN rsFilesFound ff ON c2.fldLocation = ff.fldFileAddress " & _ "WHERE c2.blnAlive = True AND ff.fldFileAddress IS NULL" & _ ");" CurrentDb.Execute strSQL, dbFailOnError End If
Certainly it helps with the readability - to distinguish the version of table tblClips that you are updating from the version of the same table that is being read.Was it necessary to do the c1 alias in the creation of the 2nd sql statement, or was that a convenience to aid in the readability of the code?
Dim strMsg As String
Dim strSql As String
Dim strFrom As String
If Not Me.chkDir Then
'SQL below is to count all records not found in active directory search
strFrom = "FROM tblClips c LEFT JOIN rsFilesFound ff ON c.fldLocation = ff.fldFileAddress " & _
"WHERE c.blnAlive = True AND ff.fldFileAddress IS NULL"
strSql = "SELECT COUNT(*) " & strFrom & ";"
With CurrentDb.OpenRecordset(strSql)
strMsg = "The SQL Query contains " & .Fields(0) & " references."
.Close
End With
If MsgBox(strMsg, vbOKCancel, "TESTING") = vbCancel Then Exit Sub
strSQL = "UPDATE tblClips c1 " & _
"SET c1.blnAlive = False " & _
"WHERE c1.ID IN (" & _
"SELECT c.ID " & strFrom & _
");"
CurrentDb.Execute strSQL, dbFailOnError
End If
strFrom
otherwise it will bork the UPDATE query by terminating it early.