Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old December 30th, 2004, 12:17 AM
dev77's Avatar
dev77 dev77 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Location: On d Earth ofcourse..!!
Posts: 440 dev77 User rank is Major (30000 - 40000 Reputation Level)dev77 User rank is Major (30000 - 40000 Reputation Level)dev77 User rank is Major (30000 - 40000 Reputation Level)dev77 User rank is Major (30000 - 40000 Reputation Level)dev77 User rank is Major (30000 - 40000 Reputation Level)dev77 User rank is Major (30000 - 40000 Reputation Level)dev77 User rank is Major (30000 - 40000 Reputation Level)dev77 User rank is Major (30000 - 40000 Reputation Level)dev77 User rank is Major (30000 - 40000 Reputation Level)dev77 User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 16 h 13 m 33 sec
Reputation Power: 372
Arrow problem reading csv file

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

Reply With Quote
  #2  
Old January 3rd, 2005, 03:03 AM
hithere's Avatar
hithere hithere is offline
Learner
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Location: India
Posts: 450 hithere User rank is Sergeant (500 - 2000 Reputation Level)hithere User rank is Sergeant (500 - 2000 Reputation Level)hithere User rank is Sergeant (500 - 2000 Reputation Level)hithere User rank is Sergeant (500 - 2000 Reputation Level)hithere User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 5 Days 12 h 13 m 21 sec
Reputation Power: 17
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

Reply With Quote
  #3  
Old January 10th, 2005, 06:26 AM
dev77's Avatar
dev77 dev77 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Location: On d Earth ofcourse..!!
Posts: 440 dev77 User rank is Major (30000 - 40000 Reputation Level)dev77 User rank is Major (30000 - 40000 Reputation Level)dev77 User rank is Major (30000 - 40000 Reputation Level)dev77 User rank is Major (30000 - 40000 Reputation Level)dev77 User rank is Major (30000 - 40000 Reputation Level)dev77 User rank is Major (30000 - 40000 Reputation Level)dev77 User rank is Major (30000 - 40000 Reputation Level)dev77 User rank is Major (30000 - 40000 Reputation Level)dev77 User rank is Major (30000 - 40000 Reputation Level)dev77 User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 16 h 13 m 33 sec
Reputation Power: 372
Red face not fullproof solution

thanks,
but this is not full-proof solution.
will create problem when data contains a comma.

splitting on , will create problem.

Quote:
Originally Posted by hithere
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

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingVisual Basic Programming > problem reading csv file


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT