473,836 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

On error, delete file to be imported in a folder

547 Contributor
I import files created by my scanner from the folder "c:\RT\rfidlogs \" folder.
If the data type imported is not in number type format, then i would like to delete files in there using something like
Expand|Select|Wrap|Line Numbers
  1. Kill "C:\RT\rfidlogs\*.out"
the code i use currently looks like this and works fine if the data imported is in number format. Please be so kind to assist. I
Expand|Select|Wrap|Line Numbers
  1.   Const TheDirectory = "c:\RT\rfidlogs\"
  2.     Dim TheFile As String
  3.     Dim TheData As String
  4.     Dim iRetValue As Long
  5.     Dim MyDB As DAO.Database
  6.     Dim rst As DAO.Recordset
  7.       TheFile = Dir(TheDirectory & "*.OUT")
  8.     If TheFile <> "" Then
  9.         Open TheDirectory & TheFile For Input As #1
  10.             Line Input #1, TheData
  11.   iRetValue = sndPlaySound(CurrentProject.Path & "\bleep4.wav", SND_ASYNC)
  12.  If IsNull([TheData]) Then Exit Sub
  13.     Set MyDB = CurrentDb
  14.     Set rst = MyDB.OpenRecordset("RaceTimingT", dbOpenDynaset, dbAppendOnly)
  15.  
  16.      With rst
  17.             .AddNew        
  18.          ![RaceNumber] = TheData
  19.          ![RaceFinishTime] = Format(Now(), "General Date")
  20.  End With
  21.     rst.close
  22.     Set rst = Nothing
  23.     Set MyDB = Nothing
  24.         Close #1
  25.         Kill TheDirectory & TheFile
  26.             End If
  27.        DoCmd.GoToRecord , "", acNewRec
  28.     Exit Sub
Aug 6 '12
24 20305
neelsfer
547 Contributor
thx a million twinnyfo - your time is appreciated and i learned a lot
I could not get it to work 100% with your code, but i adapted my error trapping using your vba and it seems to work fine now.
This is the vba that seems to work fine. If you have any other suggestions - pls tell me
Expand|Select|Wrap|Line Numbers
  1. Public Sub Importdata()
  2. On Error GoTo Importdata_Err
  3.   'On Error GoTo BillRedEnd
  4.     Const TheDirectory = "c:\rt\rfidlogs\"
  5.     Dim TheFile As String
  6.     Dim TheData As String
  7.     Dim MyDB As DAO.Database
  8.     Dim rst As DAO.Recordset
  9.       TheFile = Dir(TheDirectory & "*.OUT")
  10.     If TheFile <> "" Then
  11.         Open TheDirectory & TheFile For Input As #1
  12.             Line Input #1, TheData
  13.     If IsNull([TheData]) Then
  14.     Exit Sub
  15.     ElseIf [TheData] Like "*[!0-9]*" Then
  16.     Close #1
  17.       Kill TheDirectory & TheFile
  18.       DoCmd.GoToRecord , "", acNewRec
  19.     Else
  20.       Set MyDB = CurrentDb
  21.    Set rst = MyDB.OpenRecordset("RaceTimingT", dbOpenDynaset, dbAppendOnly)
  22.  
  23.     With rst
  24.            .AddNew
  25.         ![RaceNumber] = TheData
  26.          ![RaceFinishTime] = Format(Now(), "General Date")
  27.          ![RaceDate] = [Forms]![frmRtMain]![RacingDate]
  28.          ![RaceName] = [Forms]![frmRtMain]![RaceName]
  29.         .update
  30.        rst.close
  31.     End With
  32.    Set rst = Nothing
  33.    Set MyDB = Nothing
  34.    Close #1
  35.    Kill TheDirectory & TheFile
  36.    DoCmd.GoToRecord , "", acNewRec
  37.  Importdata_Err:
  38. Exit Sub
  39.  MsgBox Err.Number & " " & Err.Description
  40.  End If
  41. End If
  42. End Sub
Aug 16 '12 #21
neelsfer
547 Contributor
thx a million twinnyfo - your time is appreciated and i learned a lot
I could not get it to work 100% with your code, but i adapted my error trapping using your vba and it seems to work fine now.
This is the vba that seems to work fine. If you have any other suggestions - pls tell me
Expand|Select|Wrap|Line Numbers
  1. Public Sub Importdata()
  2. On Error GoTo Importdata_Err
  3.   'On Error GoTo BillRedEnd
  4.     Const TheDirectory = "c:\rfidlogs\"
  5.     Dim TheFile As String
  6.     Dim TheData As String
  7.     Dim MyDB As DAO.Database
  8.     Dim rst As DAO.Recordset
  9.       TheFile = Dir(TheDirectory & "*.OUT")
  10.     If TheFile <> "" Then
  11.         Open TheDirectory & TheFile For Input As #1
  12.             Line Input #1, TheData
  13.     If IsNull([TheData]) Then
  14.     Exit Sub
  15.     ElseIf [TheData] Like "*[!0-9]*" Then
  16.     Close #1
  17.       Kill TheDirectory & TheFile
  18.       DoCmd.GoToRecord , "", acNewRec
  19.     Else
  20.       Set MyDB = CurrentDb
  21.    Set rst = MyDB.OpenRecordset("RaceTimingT", dbOpenDynaset, dbAppendOnly)
  22.  
  23.     With rst
  24.            .AddNew
  25.         ![RaceNumber] = TheData
  26.          ![RaceFinishTime] = Format(Now(), "General Date")
  27.          ![RaceDate] = [Forms]![frmRtMain]![RacingDate]
  28.          ![RaceName] = [Forms]![frmRtMain]![RaceName]
  29.         .update
  30.        rst.close
  31.     End With
  32.    Set rst = Nothing
  33.    Set MyDB = Nothing
  34.    Close #1
  35.    Kill TheDirectory & TheFile
  36.    DoCmd.GoToRecord , "", acNewRec
  37.  Importdata_Err:
  38. Exit Sub
  39.  MsgBox Err.Number & " " & Err.Description
  40.  End If
  41. End If
  42. End Sub
Aug 16 '12 #22
neelsfer
547 Contributor
Still on this problem with another twist in the tail.
Using the above code i have a problem when the ".out" file is blank (empty) or when it only contains "0" or "00" or "000" or "0000". It must then be killed in the c:\rfidlogs folder.(lines 12 and 16)
Please assist
Expand|Select|Wrap|Line Numbers
  1.  Const TheDirectory = "c:\rfidlogs\"
  2.     Dim TheFile As String
  3.     Dim TheData As String
  4.     Dim MyDB As DAO.Database
  5.     Dim rst As DAO.Recordset
  6.       TheFile = Dir(TheDirectory & "*.OUT")
  7.     If TheFile <> "" Then
  8.         Open TheDirectory & TheFile For Input As #1
  9.             Line Input #1, TheData
  10.     If IsNull([TheData]) Then
  11.     Exit Sub
  12. ElseIf [TheData] = "" Then
  13. Close #1
  14.    Kill TheDirectory & TheFile
  15.    DoCmd.GoToRecord , "", acNewRec
  16.  ElseIf [TheData] = "0" Or [TheData] = "00" or [TheData] = "000" Or [TheData] = "0000" then
  17.       Close #1
  18.       Kill TheDirectory & TheFile
  19.       DoCmd.GoToRecord , "", acNewRec
  20.  ElseIf [TheData] Like "*[!0-9]*" Then
  21.     Close #1
  22.       Kill TheDirectory & TheFile
  23.       DoCmd.GoToRecord , "", acNewRec
  24.  Else
  25.       Set MyDB = CurrentDb
  26.    Set rst = MyDB.OpenRecordset("RaceTimingT", dbOpenDynaset, dbAppendOnly)
  27.  
  28.     With rst
  29.            .AddNew
  30.         ![RaceNumber] = TheData
  31.          ![RaceFinishTime] = Format(Now(), "General Date")
  32.          ![RaceDate] = [Forms]![frmRtMain]![RacingDate]
  33.          ![RaceName] = [Forms]![frmRtMain]![RaceName]
  34.         .update
  35.        rst.close
  36.     End With
  37.    Set rst = Nothing
  38.    Set MyDB = Nothing
  39.    Close #1
  40.    Kill TheDirectory & TheFile
  41.    DoCmd.GoToRecord , "", acNewRec
The blank file and with zero's looks like this - below
Attached Files
File Type: zip blank text file.zip (138 Bytes, 73 views)
File Type: zip file with zeros.zip (496 Bytes, 72 views)
Aug 27 '12 #23
twinnyfo
3,653 Recognized Expert Moderator Specialist
neels,

One option you may want to incorporate with this code, although it would be more of a larger step, is to establish flag within your code first, to establish 1) is this a file that you can use and then 2) do I save the data and/or 3) do I kill the file. It seems like you kill the file no matter what, so you may not need that flag.

The point here is that your code would look at a file, evaluate it first and simply respond to the rest of your code that there are certain other actions to be done. Then, you create separate functions to perform those specific actions.

For example:

Expand|Select|Wrap|Line Numbers
  1. Private Sub ImportData()
  2.     'Determine the File Name
  3.     If CheckValid(FileName) Then
  4.         'Do the things you do for valid file names
  5.     Else
  6.         'Just Kill the file
  7.     End If
  8. End Sub
  9.  
  10. Private Function CheckValid(FileNmae) as Boolean
  11.     'Go through your steps of validation here
  12. End Function
  13.  
This allows you to separate what you are actually doing, and, in the cases with very complex validation functions, aids in troubleshooting and is somewhat cleaner programming.

Just a thought, even though the concept may throw a wrench into what you already have working. Not sure if this helps or just confuses more.....
Aug 30 '12 #24
neelsfer
547 Contributor
thx for ideas its appreciated
Aug 31 '12 #25

Sign in to post your reply or Sign up for a free account.

Similar topics

2
4130
by: Jake | last post by:
What's wrong with this code? Thie file is there, why cant it find it? Thanks in advance! <% myfile = request.querystring("filename") response write myfile myFSO.DeleteFile(Server.MapPath(" & myfile & ")) SET myFSO = NOTHING %>
5
9758
by: Jon Maz | last post by:
Hi there, I am experimenting with the FileSystemWatcher object. I have set the NotifyFilter as follows: myFileSystemWatcher.NotifyFilter = NotifyFilters.Security | NotifyFilters.CreationTime | NotifyFilters.LastWrite | NotifyFilters.Size | NotifyFilters.Attributes |
4
3781
by: Michael K. | last post by:
Hi there I am having trouble deleting an uploaded file that I have just written to disc. I am using the SaveAs method of a HttpPostedFile to write the file to disc, then straight after I'm trying to use kill(FILE) to delete it. I get the error "The process cannot access the file "FILE.JPG" because it is being used by another process." If I use the Explorer-program to try and delete the file afterwards I get the error "Cannot delete...
3
3808
by: guoqi zheng | last post by:
Dear sir, There is an import tool on my web application, user can upload Ms Access file, the file is saved at a temp folder on the server. After the import is finished, for security reason, I need to delete the imported Ms Access file, however, I always get above error, "The process cannot access the file because it is being used by another process" I did close the datareader and connection object. What else can I do to delete this...
2
5049
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified folder structure and naming conventions and then send a Net send message to those users telling them to rectify. The information I want to get is when you select the file/folder and then: Properties -> Security Tab -> Advanced Button -> Owner Tab ->...
5
39082
by: wo20051223 | last post by:
Deleting some files with C# fails with "Access to the path 'X' is denied". I have files copied from a CD that I burned (and not locked by a process) and a text file that I created in Windows Explorer. I can delete all of them through Windows Explorer. I can programmatically delete the text file but not the others. Permissions: - All files have the same ACL.
3
1722
by: dadapeer | last post by:
Hai Every body ......... iam want one clarification.that folder can be delete automatically by setting time and date period to delete.iff possible give me u r answer thanks..........
20
11544
by: kuzure | last post by:
Dear friends, Is there anyway to write a program in vb in order to delete a file in my computer?For instance, I have a .txt file in C Drive, which is "C:\testing.txt". So, now I want to click a button in vb and delete this file. Of course, I would like to delete any file from the computer, so I hope can have a text box or some drop down box that can choose any files from my computer. Is it possible? Appreciate for any reply. Thank you....
6
2622
by: pankajprakash | last post by:
hi, i have a windows form and there is a text box to select the file of folder to delete. i need a progress bar during the file of folder deletion.
4
4108
by: id10t error | last post by:
Hello, I am making a program that will not have an user interaction to delete certian files. I found this line of code. My.Computer.FileSystem.DeleteFile("C:\POLLJP.DWN", FileIO.UIOption.AllDialogs, FileIO.RecycleOption.DeletePermanently) The problem is that a message box comes up and asks "Are you sure you want to delete file"file name yes or no". Does anyone know a way around this. I want it to delete everytime no questions asked...
0
9814
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9666
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10838
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10544
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10250
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9369
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7788
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5645
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3111
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.