473,466 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Can Access/VBA codes be reading things wrongly?

36 New Member
Hi all,

I managed to write a code that will go through a table and remove any faulty data from it.

My system generates out a monthly text file containing all the events that have happened over the month. Each event is detailed within 6 lines of text, any more or any less is considered faulty. The start of each event is determined by "0~0~ES~1~".

Here's the weird part, I tested my program with a test file which consists of about a thousand lines of text, and it runs perfectly. However, when I used it on the actual file which consists of about half a million lines of text, it's always giving me back different data.

E.g. my first test on the actual file generated 92123 records, whereas my second test generated 91800.

I did not change my codes at all, so could this be a bug?

I really have no idea how to rectify this as I see nothing wrong with my codes or data.
Expand|Select|Wrap|Line Numbers
  1. Sub Del_Invalid(strSearch As String)
  2.  
  3.     Dim db As Database, rst As Recordset, lngcnt As Long, k As Long
  4.  
  5.     On Error GoTo Del_Invalid_Err
  6.  
  7.     Set db = CurrentDb
  8.     Set rst = db.OpenRecordset("Oct", dbOpenTable)
  9.  
  10.     lngcnt = 0
  11.     With rst
  12.  
  13.         Do While Not .EOF
  14.             Debug.Print !Field1
  15.  
  16.             If InStr(1, !Field1, "0~0~ES~1~") > 0 Then
  17.                 If lngcnt > 6 Then
  18.                     'Delete offending records
  19.                     .MovePrevious
  20.                     For k = 1 To lngcnt
  21.                         .Delete
  22.                         .MovePrevious
  23.                     Next
  24.                     lngcnt = 0
  25.                 Else
  26.                     lngcnt = 1
  27.                 End If
  28.             Else
  29.                 lngcnt = lngcnt + 1
  30.             End If
  31.             .MoveNext
  32.         Loop
  33.         ' In case last one was offending
  34.         If lngcnt > 6 Then
  35.             'Delete offending records
  36.             .MovePrevious
  37.             For k = 1 To lngcnt
  38.                 .Delete
  39.                 .MovePrevious
  40.             Next
  41.         End If
  42.  
  43.     End With
  44.  
  45. Del_Invalid_Exit:
  46.     On Error Resume Next
  47.     rst.Close
  48.     Set rst = Nothing
  49.     Set db = Nothing
  50.     Exit Sub
  51.  
  52. Del_Invalid_Err:
  53.     MsgBox "Error " & Str$(Err.Number) & " = " & Err.Description
  54.     Resume Del_Invalid_Exit
  55.  
  56. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command0_Click()
  2.     Call Del_Invalid("0~0~ES~1~")
  3. End Sub
Nov 17 '14 #1
7 1139
jforbes
1,107 Recognized Expert Top Contributor
I doubt you are running into a bug. I assume you have exclusive access to the table that you are modifying, if not maybe there is a record lock issue.

One thing that concerns me is the .MovePrevious method. Moving through a Recordset and deleting records is tricky enough without moving in both directions. You could be running into a timing issue with your reads and writes. Given this, you might want to try to lockdown your Recordset when opening it. I haven't tested this out, but you might want to do something like:
Expand|Select|Wrap|Line Numbers
  1. Set rst = db.OpenRecordset("Oct", dbOpenTable,dbDenyRead,dbPessimistic)
A way you might be able to sidestep this is to create a query to perform your deletes and execute it. What you are doing is complex, but it doesn't seem too complex for a query.
Nov 18 '14 #2
shawnnnnnnn
36 New Member
Hi jforbes,

I just did a thorough check. My codes are working fine, but I'll try your suggestion to be safe :)

Anyway, I think the problem lies with Access's import function. I imported my data using the external data tab and with the same settings, but when I search for, example record number "523111", both tables will show a different value in the field. I feel that my 500000+ line text file may be too much for access to handle properly. So I'm gonna try splitting it and importing again.
Nov 18 '14 #3
Rabbit
12,516 Recognized Expert Moderator MVP
When you import data, there's no way to guarantee the order in the tables unless you write a script to import it line by line.
Nov 18 '14 #4
shawnnnnnnn
36 New Member
Hi rabbit,

Would you by any chance know how to or can provide me an example of the code to import line by line?
Nov 18 '14 #5
Rabbit
12,516 Recognized Expert Moderator MVP
An example wouldn't be much help because the bulk of it would change depending on the file layout. But the basic steps are:

1) Read a file line by line
2) Break out the line into the fields you need (this is where the bulk of the code would be and also where the changes are depending on the layout)
3) Insert that row of data into the database table with an autonumber field.

Steps 1 and 3 are fairly standard. Number 2 is where there are lots of changes depending on your file layout.
Nov 18 '14 #6
shawnnnnnnn
36 New Member
Hey rabbit,

I've been reading on this recordset method in http://bytes.com/topic/access/answer...into-ms-access

However, I'm still unable to figure out how to modify the code to work with my data due to my poor VBA knowledge. I'm really bad with this "Mid$" stuff.

Basically, I just want each line in my text file to occupy one field in my table first. I want the entire line (each line has a different number of characters) to be placed into the field, so I don't think I really need to do the Mid$ thingy right?

E.g.
Line 1 is placed in Row 1 Column 1
Line 2 is placed in Row 2 Column 1
Line 3 is placed in Row 3 Column 1
.
.
.
Line X is placed in Row X Column 1
Nov 19 '14 #7
Rabbit
12,516 Recognized Expert Moderator MVP
Yes, you're right. If you only have one field, then you don't need to mess with the Mid stuff.
Nov 19 '14 #8

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

Similar topics

1
by: Nasser | last post by:
Hello, I am coding a mathematical model with VC++. When I debug the code, I face with no erroe, but during executing, I face with below error "Unhandled exception at 0x0040c275 in Tar.exe:...
0
by: Bruce Pataki | last post by:
I am creating an MFC application with activeX document server support. The application runs perfectly fine when i run as a standalone application. But when i run the application in Internet...
2
by: Jim | last post by:
Lately, I've been seeing a lot of those dynamically created access code images on web forms. You know, some little jpg or gif image that has a random series of letters and numbers. The form says...
2
by: bhreddy | last post by:
Hi All, Can someone help me out how can I resolve the error "0xC0000005: Access violation reading location 0x513112f4"? Steps I followed... 1. I ran the application at DOS prompt 2. After...
0
by: Yanping Zhang | last post by:
Hi All, I need to use this C routine in python and there is a void pointer parameter in it: (this routine was written by someone else): myfunc(int a, (void *)userdata, bool b) I saw someone...
2
by: Jim Langston | last post by:
Microsoft Visual C++ .net 2003. I was just curious what numeric_limits for std::string would give me so wrote this: #include <iostream> #include <string> #include <limits> int main() {...
3
by: raghunadhs | last post by:
Hi all, i have developed a ".dll" in vc++, regarding to find out CRC32. and in my V.B 6.0 application i am calling that .dll. If i run my v.b application it is working means.. i am able to find...
13
by: ycinar | last post by:
A quick question: Why doesn't the following code catch the Access violation reading location exception? it crashes on line if ( xyz ) with an Access violation reading location exception. Is not...
3
by: Pinux | last post by:
Hi, I am writing a multi-threads encryption application. The idea of the code is to create a number of threads to encrypt files. I have a thread pool say the maximum threads is 10. If the number...
2
by: alexandertfg | last post by:
I finished working on my first draft for my email homework for my C++ class but when debugging I recieved a Access violation reading location 0x00370000 error. I know that it has to do with a stack...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.