473,386 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Unable to read past teh first record in ADO recordset

I have a really strange problem. I am using Microsoft Access 2003 under Windows XP Professional.

I am trying to read a CSV file using VBA in Access using the following procedure.
Expand|Select|Wrap|Line Numbers
  1. Public Sub GetCSV_FileData()
  2.  
  3. Dim cnn As ADODB.Connection
  4. Dim cn As ADODB.Connection
  5. Dim rs As ADODB.Recordset
  6. Dim f As Integer
  7. Dim NumberOfRows As Integer
  8. Dim RowNumber As Integer
  9. Dim strSQL As String
  10. Dim InsertSQL As String
  11. Dim StrFolder As String
  12.  
  13. ' Database Columns'
  14.  
  15. Dim EmployeeNumberTemp As String
  16. Dim FullNameTemp As String
  17. Dim ElementNameTemp As String
  18. Dim TempAmount As String
  19. Dim Amount As Currency
  20. Dim PayDateTemp As Date
  21. Dim ResultType As String
  22. Dim PeriodNameTemp As String
  23. Dim CSVFileName As String
  24.  
  25. Set cn = New ADODB.Connection
  26. Set cnn = Application.CurrentProject.Connection
  27.  
  28. CSVFileName = "Test_Extract.csv"
  29.  
  30. 'On Error Resume Next
  31. strSQL = "SELECT * FROM " & CSVFileName
  32. StrFolder = "C:\Documents and Settings\chris\Desktop"
  33. cn.Open "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
  34.     "Dbq=" & StrFolder & ";" & _
  35.     "Extensions=asc,csv,tab,txt;"
  36. 'On Error GoTo 0
  37. If cn.State <> adStateOpen Then Exit Sub
  38. Set rs = New ADODB.Recordset
  39. 'On Error Resume Next
  40. 'rs.Open strSQL, cn, adOpenForwardOnly, adLockReadOnly, adCmdText
  41. rs.Open strSQL, cn, adOpenDynamic, adLockReadOnly, adCmdText
  42.  '   On Error GoTo 0
  43. If rs.State <> adStateOpen Then
  44.     cn.Close
  45.     Set cn = Nothing
  46.     Exit Sub
  47.  End If
  48.     Do Until rs.EOF
  49.        ' the field headings
  50.        For f = 0 To rs.Fields.Count - 1
  51.           Select Case f
  52.              Case 0
  53.                PayDateTemp = rs.Fields(f).Name
  54.              Case 1
  55.                FullNameTemp = rs.Fields(f).Name
  56.              Case 2
  57.                EmployeeNumberTemp = rs.Fields(f).Name
  58.              Case 3
  59.                  ElementNameTemp = rs.Fields(f).Name
  60.              Case 4
  61.                 TempAmount = rs.Fields(f).Name
  62.                 Amount = Replace(TempAmount, "#", ".")
  63.              Case 5
  64.                ResultType = rs.Fields(f).Name
  65.              Case 6
  66.               PeriodNameTemp = rs.Fields(f).Name
  67.              Case 7
  68.            End Select
  69.        Next f
  70.  
  71. '  A Lot of logic will go here to do validation
  72.  
  73.  
  74. '  If the record is valid Insert it then go on to the next.
  75.  
  76.        InsertSQL = "INSERT INTO PAY_RUN_RESULTS (EMPLOYEE_NUMBER, EMPLOYEE_FULL_NAME,AMOUNT,RESULT_TYPE, PAY_DATE)  VALUES ('" & EmployeeNumberTemp & "', '" & FullNameTemp & "','" & Amount & "', '" & ResultType & "','" & PayDateTemp & "' );"
  77.        cnn.Execute (InsertSQL)
  78.        rs.MoveNext
  79.        PayDateTemp = 0
  80.        FullNameTemp = ""
  81.        EmployeeNumberTemp = ""
  82.        ElementNameTemp = ""
  83.        TempAmount = ""
  84.        Amount = 0
  85.        ResultType = ""
  86.        PeriodNameTemp = ""
  87.     Loop
  88.     rs.Close
  89.     Set rs = Nothing
  90.     cn.Close
  91.     Set cn = Nothing
  92.     Set cnn = Nothing
  93. End Sub
The test datafile has 16 rows. When the procedure completes, I have in the database table 16 rows (as expected) however the data the rows contain are from the first line of the file. can anyone suggest what I'm doing wrong.

Thanks in advance.
Mar 13 '08 #1
2 1977
Stewart Ross
2,545 Expert Mod 2GB
Hi. Kinda tricky to read your code without code tags on, but what strikes me is that in your case selectors you are only referring to the field names
Expand|Select|Wrap|Line Numbers
  1. rs.Fields(f).Name
instead of the contents of the fields
Expand|Select|Wrap|Line Numbers
  1. rs.Fields(f).Value
-Stewart
Mar 13 '08 #2
Sorry about the delay in getting back, I was ill - pneumonia. That worked thanks very much obvious once you know. Problem for me being a newie to Access is that I asked someone how to do, did who probably did not know.
Apr 11 '08 #3

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

Similar topics

2
by: Jayjay | last post by:
When it comes to access, I'm pretty good using the built in features and can come up with some pretty complex functions to get what I need. But we have this database I'm doing for work that is...
1
by: ano1optimist | last post by:
Has anyone had success with using a command collection with parameters to run a stored procedure from sql server? I'm frustrated and have been spending way too much time trying to make this work. ...
8
by: Chris Bailiss | last post by:
Hi all, I am unable to run any VBA in access XP. I get the compile error of 'Error In Loading DLL'. Behaviour of the wizards is also patchy. I cannot get past the first screen in most (i.e....
4
by: Ryan | last post by:
Access 2002, Windows XP, 1 Front end MDB, 1 Back end MDB I have a subform which points to a table. Navigating from one record to the next is obviously dead straight forward. However, this seems...
10
by: Lyn | last post by:
I have a form set to Single Form mode with which I can cycle through the records in a table via Next and Previous buttons. To avoid users pressing the Previous button on the first record and the...
6
by: sghi | last post by:
Hi All, I'm new to this group and quite new to access/vba. So, shortly after beginning to write a simple application for my wife, I came across a blocking problem: I need to intercept the sql...
7
by: uarana | last post by:
Hi All, Can someone please help me with the following code. I've been working on this for the past 2 days and i can't seem to get past this obstacle. Problem: The code opens up the Table...
2
by: pmfunder | last post by:
Hi all, I'm new to the site so please forgive me if I missed one of the guidelines that I read. I have a large budget database using Access 2000. I'm trying to make the budget table into a...
3
by: Osamede.Zhang | last post by:
I have some code like this: SqlCommand cmd = new SqlCommand("get_storeid_byuser", cn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@username", SqlDbType.VarChar, 50).Value...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.