chizzy42
09-28-2014, 04:05 AM
Hi,
My aim is to import text files from a server and put them in an access database, the code I have works for this task, but occasionally there are file read and write errors due to the text files being accessed at the time they’re being written by a labview app. This results in a message box appearing and the code stopping updating, this has to run 24/7 why im im trying to make it error free. I’ve added an error handler to detect an error to prevent the hang up and to email me when this occurs. My issue is that when the error occurs and is flagged the program sends out an email every time the code runs , it’s on a timer so runs every few minutes.
Could someone check the code please and tell me how to have the email happen only once….im assuming that the error is somehow trapped in the code and that is why it is getting sent out every refresh.
(i) Is there a way to flush the error so when the code runs again , there are no errors trapped.
(ii) Ideally I’d like to see what DoCmd.TransferText block is causing the error( there could be around 40 DoCmd.TransferText blocks)..should I have an error handler around every DoCmd.TransferText block I have to identify where the error originates or is there another way to achieve this.
Any other feedback on the code would be appreciated to.
Thanks in advance
ian
Private Sub Form_Timer()
On Error GoTo Import_Err
Dim DateStringrockD As String
DoCmd.SetWarnings False
DateStringrockD = (Format(Day(Now()) + 1, "dd") & "D" & Format(Month(Now()) + 1, "dd") & Format(Now(), "yy"))
If Dir("T:\HOS UK07\Test data\siskin\siskinRF\" & DateStringsisN & ".txt") <> "" Then
DoCmd.TransferText acImportDelim, "FCS", "tblFCS", "T:\HOS UK07\Test data\siskin\siskinRF\" & DateStringsisN & ".txt"
End If
Dim DateStringsisD As String
DoCmd.SetWarnings False
DateStringsisD = (Format(Day(Now()) + 1, "dd") & "D" & Format(Month(Now()) + 1, "dd") & Format(Now(), "yy"))
If Dir("T:\HOS UK07\Test data\siskin\siskinRF\" & DateStringsisD & ".txt") <> "" Then
DoCmd.TransferText acImportDelim, "FCS", "tblFCS", "T:\HOS UK07\Test data\siskin\siskinRF\" & DateStringsisD & ".txt"
End If
'''''''''''''''ERROR HANDLING'''''''''''''''''''''''''''''
Import_Exit:
Exit Sub
Import_Err:
Dim appOutLook As Object
Dim MailOutLook As Object
'assign our object references
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(0)
With MailOutLook
'set the recipient list
.To = "emailaddress"
'set the subject
.Subject = "Trap Error"
'set the body text
.Body = "A Trap Error has occured"
'send the email
.Send
End With
'get rid of object references
Set appOutLook = Nothing
Set MailOutLook = Nothing
Resume Import_Exit
End Sub
My aim is to import text files from a server and put them in an access database, the code I have works for this task, but occasionally there are file read and write errors due to the text files being accessed at the time they’re being written by a labview app. This results in a message box appearing and the code stopping updating, this has to run 24/7 why im im trying to make it error free. I’ve added an error handler to detect an error to prevent the hang up and to email me when this occurs. My issue is that when the error occurs and is flagged the program sends out an email every time the code runs , it’s on a timer so runs every few minutes.
Could someone check the code please and tell me how to have the email happen only once….im assuming that the error is somehow trapped in the code and that is why it is getting sent out every refresh.
(i) Is there a way to flush the error so when the code runs again , there are no errors trapped.
(ii) Ideally I’d like to see what DoCmd.TransferText block is causing the error( there could be around 40 DoCmd.TransferText blocks)..should I have an error handler around every DoCmd.TransferText block I have to identify where the error originates or is there another way to achieve this.
Any other feedback on the code would be appreciated to.
Thanks in advance
ian
Private Sub Form_Timer()
On Error GoTo Import_Err
Dim DateStringrockD As String
DoCmd.SetWarnings False
DateStringrockD = (Format(Day(Now()) + 1, "dd") & "D" & Format(Month(Now()) + 1, "dd") & Format(Now(), "yy"))
If Dir("T:\HOS UK07\Test data\siskin\siskinRF\" & DateStringsisN & ".txt") <> "" Then
DoCmd.TransferText acImportDelim, "FCS", "tblFCS", "T:\HOS UK07\Test data\siskin\siskinRF\" & DateStringsisN & ".txt"
End If
Dim DateStringsisD As String
DoCmd.SetWarnings False
DateStringsisD = (Format(Day(Now()) + 1, "dd") & "D" & Format(Month(Now()) + 1, "dd") & Format(Now(), "yy"))
If Dir("T:\HOS UK07\Test data\siskin\siskinRF\" & DateStringsisD & ".txt") <> "" Then
DoCmd.TransferText acImportDelim, "FCS", "tblFCS", "T:\HOS UK07\Test data\siskin\siskinRF\" & DateStringsisD & ".txt"
End If
'''''''''''''''ERROR HANDLING'''''''''''''''''''''''''''''
Import_Exit:
Exit Sub
Import_Err:
Dim appOutLook As Object
Dim MailOutLook As Object
'assign our object references
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(0)
With MailOutLook
'set the recipient list
.To = "emailaddress"
'set the subject
.Subject = "Trap Error"
'set the body text
.Body = "A Trap Error has occured"
'send the email
.Send
End With
'get rid of object references
Set appOutLook = Nothing
Set MailOutLook = Nothing
Resume Import_Exit
End Sub