+ Reply to Thread
Results 1 to 3 of 3

Copy data from Access table into Excelsheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-20-2009
    Location
    bristol
    MS-Off Ver
    Excel 2003
    Posts
    883

    Copy data from Access table into Excelsheet

    Hi Guys

    In the table ScanDate has number data type and NewBatchNo has Text data type. The following code is meant to copy data from the table where scandate is between two dates and NewBatchNo is null. but when I run it then it stops responding. What could be the error in the following code.

    Private Sub Workbook_Open()
    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim i As Long
    i = 1
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet1")
        Set cn = New ADODB.Connection
        cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
            "Data Source=J:\WorkQueue.mdb;"
        Set rs = CreateObject("ADODB.Recordset")
       strsql = "select * from CocunutBatchTBL1 where ScanDate>=20131120 and Scandate<=20140829" rs.Open strsql, cn
      Do While Not rs.EOF
      If rs.fields("NewBatchNo") <> Null Then
      ws.Cells(i, 1).Value = rs.fields("BatchNo")
      i = i + 1
      End If
      Loop
      
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Copy data from Access table into Excelsheet

    rs.Open strsql, cn

    is this on its own line?

    Also, why not put the null in the query to?
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Forum Contributor
    Join Date
    02-20-2009
    Location
    bristol
    MS-Off Ver
    Excel 2003
    Posts
    883

    Re: Copy data from Access table into Excelsheet

    The following code works. Thanks
    Private Sub Workbook_Open()
    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim i As Long
    Dim j, k As Long
    j = 20130820
    k = 20140829
    i = 1
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet1")
        Set cn = New ADODB.Connection
        cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
            "Data Source=J:\WorkQueue.mdb;"
        Set rs = CreateObject("ADODB.Recordset")
        strsql = "select * from CocunutBatchTBL1 where ScanDate>=" & j & " and scandate<=" & k & " and NewBatchNo<>Null"
        rs.Open strsql, cn
        rs.MoveFirst
       Do While Not rs.EOF
     
      ws.Cells(i, 1).Value = rs.fields("BatchNo")
      i = i + 1
      rs.MoveNext
      Loop
      
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Copy data from Access table to Excelsheet using Excel/VBA
    By aman1234 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-12-2014, 08:25 AM
  2. Fetch Data from Access table into Excelsheet
    By aman1234 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-07-2014, 11:35 AM
  3. Extract data from Access table into excelsheet
    By aman1234 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-16-2012, 05:36 AM
  4. Transfer data from excelsheet to access table
    By aman1234 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-16-2011, 04:35 AM
  5. Transfer data from Access table to Excelsheet
    By aman1234 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-27-2010, 03:38 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1