473,498 Members | 1,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Recordset comparison

44 New Member
Evening all i am importing a text file into my database the text file is just a number and each new number is on a new line. The method i am using to get the values from the text file is
Expand|Select|Wrap|Line Numbers
  1.     Set objFSO = CreateObject("Scripting.FileSystemObject")
  2.     Set objFile = objFSO.OpenTextFile(Application.CurrentProject.Path & "\Import.txt", 1)
  3.  
  4.     Do Until objFile.AtEndOfStream
  5.         l = objFile.ReadLine
  6. Loop
  7. objFile.Close
What i would like to do with each value is to check this against the values of a column in a table. The table is called tblProduct the column is ProdID. What i would like to do is if the text file values match a value in ProdID carry on to the next text file value. If it does not match i would like it to chuck a error.
I have created a recordset but it is only checking the first value my total code is as follows
Expand|Select|Wrap|Line Numbers
  1.     Set objCHK = CreateObject("Scripting.FileSystemObject")
  2.     Set objFile = objCHK.OpenTextFile(Application.CurrentProject.Path & "\Import.txt", 1)
  3.  
  4.  
  5.   Set db = CurrentDb
  6.     Set r = db.OpenRecordset("tblProduct")
  7.  
  8.    While Not r.EOF
  9.  
  10.    o = r!ProdID
  11.  
  12.    Debug.Print o
  13.    r.MoveNext
  14.     Wend
  15.  
  16.     Do Until objFile.AtEndOfStream
  17.         c = objFile.ReadLine
  18.  
  19.     Debug.Print c
  20.     Debug.Print o
  21.  
  22.      If c = o Then
  23.      Exit Sub
  24.      Else
  25.  
  26.      End If
  27.  
  28.  
  29. Loop
  30. objFile.Close
Dec 20 '12 #1
2 1853
NeoPa
32,557 Recognized Expert Moderator MVP
May I suggest an alternative approach? If this doesn't suit then say so. I'm not saying what you're trying to do can't be done by any means, but it just seems such a convoluted approach to the problem.

Why not import the file into a basic, unrestricted, table. Clear the contents of the table first. This table is here for this specific task - just to hold imported data so that checks can be run on it.

The next step is to run checking queries on the data. Match it up to your existing table and show all those records where there is no match. A list of the failures in one go is probably better than a number of error messages.

When you're happy with your data then process it. Generally use an APPEND query to another table.

As a general rule, when working with any data in an RDBMS, let the RDBMS manage the data.
Dec 20 '12 #2
ADezii
8,834 Recognized Expert Expert
There is a very simple, basic solution to your problem assuming that the Number of Records in tblProduct is exactly equal to the Number of Lines in Import.txt. I ran the Code against 30 Records in tblProduct sequentially numbered (1-30), and 30 Lines in Import.txt with mismatches on the Odd Lines (1,3,5,7,9, etc.). Here is the Code with the generated Results. See what you think:
Expand|Select|Wrap|Line Numbers
  1. Dim db As DAO.Database
  2. Dim r As DAO.Recordset
  3. Dim TextLine
  4. Dim o
  5. Dim intLineNum As Integer
  6.  
  7. Set db = CurrentDb
  8. Set r = db.OpenRecordset("tblProduct")
  9.  
  10. r.MoveFirst     'Must be on Record# 1
  11.  
  12. Open CurrentProject.Path & "\Import.txt" For Input As #1
  13.  
  14. Do While Not EOF(1)
  15.   intLineNum = intLineNum + 1
  16.   o = r!ProdID
  17.   Line Input #1, TextLine       'Read Line into Variable
  18.     If Val(o) <> Val(TextLine) Then Debug.Print "Mismatch on Line " & intLineNum & _
  19.                                                 " ProdID: " & o
  20.       r.MoveNext
  21. Loop
  22.  
  23.  
  24. r.Close
  25. Set r = Nothing
  26.  
  27. Close #1
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Mismatch on Line 1 ProdID: 1 Actual: 11
  2. Mismatch on Line 3 ProdID: 3 Actual: 33
  3. Mismatch on Line 5 ProdID: 5 Actual: 55
  4. Mismatch on Line 7 ProdID: 7 Actual: 77
  5. Mismatch on Line 9 ProdID: 9 Actual: 99
  6. Mismatch on Line 11 ProdID: 11 Actual: 111
  7. Mismatch on Line 13 ProdID: 13 Actual: 133
  8. Mismatch on Line 15 ProdID: 15 Actual: 55
  9. Mismatch on Line 17 ProdID: 17 Actual: 177
  10. Mismatch on Line 19 ProdID: 19 Actual: 199
  11. Mismatch on Line 21 ProdID: 21 Actual: 211
  12. Mismatch on Line 23 ProdID: 23 Actual: 233
  13. Mismatch on Line 25 ProdID: 25 Actual: 55
  14. Mismatch on Line 27 ProdID: 27 Actual: 277
  15. Mismatch on Line 29 ProdID: 29 Actual: 299
Dec 21 '12 #3

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

Similar topics

5
3124
by: Shay | last post by:
essentially I am trying to do some counts based on some assumptions in the recordset. So I get the RS back, put the values into a variable, move to the next record in the RS and compare what is in...
1
1256
by: ferky | last post by:
I have this code below when I asign strOldTkinit = strNewTkinit and then read the new value for the strNewTkinit from the recordset the strOldTkinit changes the value also. So when I run my...
1
1524
by: ferky | last post by:
I have this code below when I asign strOldTkinit = strNewTkinit and then read the new value for the strNewTkinit from the recordset the strOldTkinit changes the value also. So when I run my...
9
4331
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
3
2197
by: Liz Malcolm | last post by:
Hello and TIA for guidance. I am building a reusable search procedure (thanks go to Graham Thorpe for his example that set me on my way). Everything works up until the 2nd match is found, the...
1
3488
by: John | last post by:
My report is based off of a table. I need a record on the report to be highlighted if that record ID is in a different table. Something like, if record ID exists in Query X, then conditional format...
3
6857
by: Nathan Bloomfield | last post by:
Hi there, I am having difficulty with a piece of code which would work wonders for my application if only the error trapping worked properly. Basically, it works as follows: - adds records...
5
1723
by: Newbie in ChiTown | last post by:
I created a subprocedure that includes a recordset as follows: Private Sub Combo17_Click() 'Declaring object and string variables Dim DB As Database Dim rec As Recordset ...
4
1500
by: Joseph | last post by:
Hi all- I am a former VB6 programmer and new at C# and I have a question dealing with converting some code from VB6 to C#. The code is below and essentially, what it does is gets data from a SQL...
10
2394
by: akselo | last post by:
Hi folks, I am working on a routine that will select a sample of parcels from a table. Each parcel belongs to a census tract, and depending on which tract, a certain calculation is applied. The...
0
7125
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
7004
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
7167
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
7208
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
7379
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
4593
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3095
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
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
292
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...

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.