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. - Public Sub GetCSV_FileData()
-
-
Dim cnn As ADODB.Connection
-
Dim cn As ADODB.Connection
-
Dim rs As ADODB.Recordset
-
Dim f As Integer
-
Dim NumberOfRows As Integer
-
Dim RowNumber As Integer
-
Dim strSQL As String
-
Dim InsertSQL As String
-
Dim StrFolder As String
-
-
' Database Columns'
-
-
Dim EmployeeNumberTemp As String
-
Dim FullNameTemp As String
-
Dim ElementNameTemp As String
-
Dim TempAmount As String
-
Dim Amount As Currency
-
Dim PayDateTemp As Date
-
Dim ResultType As String
-
Dim PeriodNameTemp As String
-
Dim CSVFileName As String
-
-
Set cn = New ADODB.Connection
-
Set cnn = Application.CurrentProject.Connection
-
-
CSVFileName = "Test_Extract.csv"
-
-
'On Error Resume Next
-
strSQL = "SELECT * FROM " & CSVFileName
-
StrFolder = "C:\Documents and Settings\chris\Desktop"
-
cn.Open "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
-
"Dbq=" & StrFolder & ";" & _
-
"Extensions=asc,csv,tab,txt;"
-
'On Error GoTo 0
-
If cn.State <> adStateOpen Then Exit Sub
-
Set rs = New ADODB.Recordset
-
'On Error Resume Next
-
'rs.Open strSQL, cn, adOpenForwardOnly, adLockReadOnly, adCmdText
-
rs.Open strSQL, cn, adOpenDynamic, adLockReadOnly, adCmdText
-
' On Error GoTo 0
-
If rs.State <> adStateOpen Then
-
cn.Close
-
Set cn = Nothing
-
Exit Sub
-
End If
-
Do Until rs.EOF
-
' the field headings
-
For f = 0 To rs.Fields.Count - 1
-
Select Case f
-
Case 0
-
PayDateTemp = rs.Fields(f).Name
-
Case 1
-
FullNameTemp = rs.Fields(f).Name
-
Case 2
-
EmployeeNumberTemp = rs.Fields(f).Name
-
Case 3
-
ElementNameTemp = rs.Fields(f).Name
-
Case 4
-
TempAmount = rs.Fields(f).Name
-
Amount = Replace(TempAmount, "#", ".")
-
Case 5
-
ResultType = rs.Fields(f).Name
-
Case 6
-
PeriodNameTemp = rs.Fields(f).Name
-
Case 7
-
End Select
-
Next f
-
-
' A Lot of logic will go here to do validation
-
-
-
' If the record is valid Insert it then go on to the next.
-
-
InsertSQL = "INSERT INTO PAY_RUN_RESULTS (EMPLOYEE_NUMBER, EMPLOYEE_FULL_NAME,AMOUNT,RESULT_TYPE, PAY_DATE) VALUES ('" & EmployeeNumberTemp & "', '" & FullNameTemp & "','" & Amount & "', '" & ResultType & "','" & PayDateTemp & "' );"
-
cnn.Execute (InsertSQL)
-
rs.MoveNext
-
PayDateTemp = 0
-
FullNameTemp = ""
-
EmployeeNumberTemp = ""
-
ElementNameTemp = ""
-
TempAmount = ""
-
Amount = 0
-
ResultType = ""
-
PeriodNameTemp = ""
-
Loop
-
rs.Close
-
Set rs = Nothing
-
cn.Close
-
Set cn = Nothing
-
Set cnn = Nothing
-
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.
2 1977
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
instead of the contents of the fields
-Stewart
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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. ...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
| |