|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Hi,
I am trying to read data from csv file. Code is given below. My problem is I could not read values in zip code if it is in format 99999-9999. Although it could read the values like 99999 in the same column. Also, I am surprised that it could read value in this format 99999-9999 if all values are in that format. Only mix of formats it can't read. If you try this code try it with mix data. I mean 99999 in some cells of ZipCode and 99999-9999 in some other cells of ZipCode. Code:
Set fso = New Scripting.FileSystemObject
'################## CHANGE THE NAME OF THE FILE..........
strFileName = App.Path & "\MyFile.csv"
'##################
CSVFile = strFileName
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
'
'Define File System object for reading the CSV file
'
Set FileCSV = fso.GetFile(CSVFile)
Set tsmax = FileCSV.OpenAsTextStream(ForReading, TristateFalse)
While Not tsmax.AtEndOfStream
no_line = no_line + 1
Text = tsmax.ReadLine
Wend
If no_line = 1 Then
MsgBox "No line found in csv"
End If
Set tsmax = Nothing
'
'Create connection string for reading CSV datacontent
'
Dim objConn As ADODB.Connection
Dim objRs As ADODB.Recordset
Set objConn = New ADODB.Connection
objConn.Open "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
"Dbq=" & FileCSV.ParentFolder & ";" & _
"Extensions=asc,csv,tab,txt;" & _
"Persist Security Info=False"
'
'Create Record Set for reading CSV file content
'
Set objRs = New ADODB.Recordset
objRs.Open "Select * From " & FileCSV.Name, objConn, adOpenStatic, adLockReadOnly, adCmdText
'
'Transfer Records from from CSV file to Tem database
'
Dim objAccessRs As ADODB.Recordset
Dim llngCounter As Long
Set objAccessRs = New ADODB.Recordset
llngCounter = 2
Do While Not objRs.EOF
If Trim(objRs("PersonID")) <> "" And IsNumeric(objRs("PersonID")) Then
objAccessRs.Open "Select * from tblPerson Where PersonID = " & objRs("PersonID"), _
mobjConn, 3, 3
If Not objAccessRs.EOF Then
'objAccessRs("PersonID") = Trim(objRs("PersonID"))
objAccessRs("Prefix") = Trim(objRs("Prefix"))
objAccessRs("LName") = Trim(objRs("LName"))
objAccessRs("FName") = Trim(objRs("FName"))
objAccessRs("MI") = Trim(objRs("MI"))
objAccessRs("Title1") = Trim(objRs("Title1"))
objAccessRs("Zip") = Trim(objRs("Zip"))
End If
objAccessRs.Close
End If
objRs.MoveNext
llngCounter = llngCounter + 1
Loop
Any help/suggestion is appreciated. Thanks, Dev |
|
#2
|
||||
|
||||
|
Even i have encountered this problem. u can try the following code for inseting csv data into access
Private Sub Command1_Click() 'working fine cnn.Open ' open the access connection here cmd.ActiveConnection = cnn ' open csv file for reading Open FileCSV.Name For Input As #1 Line Input #1, strline ' this for skipping the headings in the file i.e column names Do While Not EOF(1) ' Read a line from the file. Line Input #1, strline varFields = Split(strline, ",") If Trim(varfields(0)) <> "" And IsNumeric(varfields(0)) Then objAccessRs.Open "Select * from tblPerson Where PersonID = " & varfields(0), mobjConn, 3, 3 If Not objAccessRs.EOF Then 'objAccessRs("PersonID") = varfields(0) objAccessRs("Prefix") = varfields(1) objAccessRs("LName") = "'" & varfields(2) & "'" objAccessRs("FName") = "'" & varfields(3) & "'" objAccessRs("MI") = "'" & varfields(4) & "'" objAccessRs("Title1") = "'" & varfields(5) & "'" objAccessRs("Zip") = varfields(6) End If objAccessRs.Close End If cmd.Execute Loop ' Close the text file. Close #1 End Sub I hope this is of some help to u |
|
#3
|
||||
|
||||
|
thanks,
but this is not full-proof solution. will create problem when data contains a comma. splitting on , will create problem. Quote:
|
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > problem reading csv file |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|