Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Read Excel and Text Files Using ADO
[ All Languages » VB »  ADO]

Total Hit ( 4660)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Click here to copy the following block
'Read Excel File Using ADO
Public Function Read_Excel _
     (ByVal sFile _
     As String) As ADODB.Recordset

   On Error GoTo fix_err
   Dim rs As ADODB.Recordset
   Set rs = New ADODB.Recordset
   Dim sconn As String

   rs.CursorLocation = adUseClient
   rs.CursorType = adOpenKeyset
   rs.LockType = adLockBatchOptimistic

   sconn = "DRIVER=Microsoft Excel Driver (*.xls);" & "DBQ=" & sFile
   rs.Open "SELECT * FROM [sheet1$]", sconn
   Set Read_Excel = rs
   Set rs = Nothing
   Exit Function
fix_err:
   Debug.Print Err.Description + " " + _
         Err.Source, vbCritical, "Import"
   Err.Clear
End Function

'*********************************************************************
'Read Text files
'You can use Extended Properties='text;FMT=Delimited'"
'By adding a third argument we can tell ADO that the file doesn't contain headers.
'The argument named HDR takes YES or NO .
'connOpen "Provider=Microsoft.Jet" _
'     & ".OLEDB.4.0;Data Source=" & App.Path _
'     & ";Extended Properties='text;HDR=NO;" _
'     & "FMT=Delimited'"
'You can use Microsoft Text Driver or Microsoft.Jet
'*********************************************************************

Public Function Read_Text_File() As ADODB.Recordset

   Dim rs As ADODB.Recordset
   Set rs = New ADODB.Recordset
   Dim conn As ADODB.Connection
   Set conn = New ADODB.Connection
   conn.Open "DRIVER={Microsoft Text Driver (*.txt; *.csv)};" & _
         "DBQ=" & App.Path & ";", "", ""

   rs.Open "select * from [test#txt]", conn, adOpenStatic, _
         adLockReadOnly, adCmdText
   Set Read_Text_File = rs
   Set rs = Nothing
   Set conn = Nothing
End Function


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.