Results 1 to 5 of 5

Thread: ADO and Excel 2k[RESOLVED for now]

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    ADO and Excel 2k[RESOLVED for now]

    I am connecting to an Excel spreadsheet using ADO,
    but the resulting recordset only has values for the text cells
    and not any date or numeric cells. I don't see why the recordset
    is not picking up the cells other than text.

    Any ideas???

    Code:
    Dim sSQL As String
    Dim oRsE As ADODB.Recordset
    Dim oCnnE As New ADODB.Connection
    
    oCnnE.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; DATA Source=" 
    & frmMain.lblSVR_Path.Caption & ";Extended Properties=Excel 8.0;"
    oCnnE.Open
    
    sSQL = "SELECT"
    sSQL = sSQL & " *"
    sSQL = sSQL & " FROM"
    sSQL = sSQL & " [Worksheet]"
        
    Set oRsE = New ADODB.Recordset
    oRsE.Open sSQL, oCnnE, adOpenStatic, adLockReadOnly, adCmdText
    
    If IsDate(oRsE!F1) = True Then...
    The F1 Field contains several rows of dates and they all come in as Null.

    Thanks for any help.
    Last edited by RobDog888; Mar 20th, 2003 at 12:17 PM.

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    I suspect this could be down to the 'adCmdText' argument, not sure about that one though. Here's a sample testing one I've just produced which works though, stating from range A1, copy this into a new spreadsheet & save it as "C:\book1.xls":
    Code:
    1	01/01/2003	a
    2	02/01/2003	b
    3	03/01/2003	c
    4	04/01/2003	d
    5	05/01/2003	e
    Then in a new vb project, add a reference to the ADO object library and use this:
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim sSQL As String
    3.     Dim oCnnE As ADODB.Connection
    4.     Dim oRsE As ADODB.Recordset
    5.    
    6.     Set oCnnE = New ADODB.Connection
    7.    
    8.     oCnnE.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; " & _
    9.     "DATA Source=C:\book1.xls;Extended Properties=Excel 8.0;"
    10.    
    11.     oCnnE.Open
    12.    
    13.         sSQL = "SELECT * from [Sheet1$]"
    14.            
    15.         Set oRsE = New ADODB.Recordset
    16.             oRsE.Open sSQL, oCnnE, , , adCmdText
    17.            
    18.             Do Until oRsE.EOF
    19.                 Print oRsE.Fields(0) & "  " & oRsE.Fields(1) & _
    20.                 "  " & oRsE.Fields(2)
    21.                 oRsE.MoveNext
    22.             Loop
    23.         oRsE.Close
    24.         Set oRsE = Nothing
    25.    
    26.     oCnnE.Close
    27.     Set oCnnE = Nothing
    28. End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    The tests work fine so it has to be something with the workbook.
    The workbook is generated by another program. If I copy the worksheet
    to another workbook it won't work either, but if I manually type some of the data
    into another workbook then it works.

    Oh well, I don't have all day for this so I will just use the Excel object instead.

  4. #4
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    What if you called the sheet & altered every cells format to be text, does this maybe copy over?

    ActiveSheet.Cells.NumberFormat = "@"

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  5. #5

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    So your saying to open the spreadsheet and format the cells
    to all be text and then read it in?
    Then it should be able to read in the values.
    I will test it out this weekend and let you know.

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width