I am having great difficculty uploading anything on this site. I have changed browsers and followed your steps on how to attached, nothings works. Thats why i am posting links.
When i try to run this code below, you will see that a previous Macro 'CreateDocumentStorageTable' pops up and i just cant get rid of it.Please assist.
Code:
Option Compare Database
Private Sub Form_Open(Cancel As Integer)
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim sqlQuery As String
Dim testDateThreshold As Date
Dim blockNr As Long
Dim blockName As String
Dim virusType As String
Dim originalTestDate As Date
Dim retestDueDate As Date
Dim retestExists As Variant
Dim messageText As String
' Set the date threshold (5 years ago from today)
testDateThreshold = DateAdd("yyyy", -5, Date)
' Open the current database
Set db = CurrentDb()
' SQL query to find blocks that tested positive (A/EC) 5+ years ago
sqlQuery = "SELECT Nr, Block, TestResult, TestDate FROM TblBlocks " & _
"WHERE TestResult='A/EC' AND TestDate <= #" & Format(testDateThreshold, "MM/DD/YYYY") & "#"
Set rs = db.OpenRecordset(sqlQuery, dbOpenDynaset)
' Initialize message text
messageText = "The following blocks require retesting:" & vbCrLf & vbCrLf
' Check if records exist
If Not rs.EOF Then
While Not rs.EOF
blockNr = rs!Nr
blockName = rs!Block
virusType = "A/EC" ' We are filtering for this specific test result
originalTestDate = rs!TestDate
retestDueDate = DateAdd("yyyy", 5, originalTestDate)
' Check if this block is already in the Retest Schedule table
retestExists = Nz(DLookup("Nr", "TblBlockRetesting", "Nr=" & blockNr & " AND VirusType='" & virusType & "'"), 0)
' If the block is not already scheduled for retesting, insert it
If retestExists = 0 Then
db.Execute "INSERT INTO TblBlockRetesting (Nr, BlockName, VirusType, OriginalTestDate, NextDueDate, Retest_Status) " & _
"VALUES (" & blockNr & ", '" & Replace(blockName, "'", "''") & "', '" & virusType & "', #" & Format(originalTestDate, "MM/DD/YYYY") & "#, #" & Format(retestDueDate, "MM/DD/YYYY") & "#, 'Due');"
End If
' Add block info to the message pop-up
messageText = messageText & "Block Nr: " & blockNr & " | Block: " & blockName & _
" | Due Date: " & Format(retestDueDate, "dd-mmm-yyyy") & vbCrLf
' Move to next record
rs.MoveNext
Wend
' Show pop-up reminder if there are overdue tests
MsgBox messageText, vbExclamation, "Retesting Alert"
Else
MsgBox "No blocks require retesting.", vbInformation, "Retesting Check Complete"
End If
' Close resources
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub