+ Reply to Thread
Results 1 to 11 of 11

Not able to paste recordset in excel spreadsheet

Hybrid View

  1. #1
    Registered User
    Join Date
    11-11-2013
    Location
    pune
    MS-Off Ver
    Excel 2003
    Posts
    8

    Not able to paste recordset in excel spreadsheet

    Hi,

    Below is my code -(Excel 2013)

    Sub Button1_Click()
    Dim cnn As ADODB.Connection
       Set cnn = New ADODB.Connection
    
    Dim sSQLQry As String
    Dim ReturnArray
    Dim mrs As New ADODB.Recordset
    Dim DBPath As String, sconnect As String
    
    
       cnn.ConnectionString = "DSN=ABC_32;UID=xxx@xxx.com;PWD=xxxx;"
       cnn.Open
    
       ' Find out if the attempt to connect worked.
       If cnn.State = adStateOpen Then
          MsgBox "Welcome to Pubs!"
       Else
          MsgBox "Sorry. No Pubs today."
       End If
        Set mrs = cnn.Execute("select rowid from schema.table where accnumber = 'ABC'")
        MsgBox mrs("rowid")
    
    ReturnArray = mrs.GetRows
    Sheet1.Range("A2").CopyFromRecordset mrs
    
    mrs.Close
    
       ' Close the connection.
       cnn.Close
    End Sub
    From the above code I am able to get the record in Msgbox but not able to paste in a excel cells
    Please let me know what I am doing wrong ?

  2. #2
    Forum Contributor
    Join Date
    09-03-2015
    Location
    IE
    MS-Off Ver
    2003 - 2016
    Posts
    258

    Re: Not able to paste recordset in excel spreadsheet

    Probably because after using GetRows, you are at the end of the recordset and there's nothing more to get.

    Comment only, Not tested.

    Also, perhaps you need to check your logic. If the connection is not opened, you display a message box but allow the code to continue as if it was.

  3. #3
    Registered User
    Join Date
    11-11-2013
    Location
    pune
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Not able to paste recordset in excel spreadsheet

    Ok , please suggest how can I paste the recordset in excel

  4. #4
    Forum Contributor
    Join Date
    09-03-2015
    Location
    IE
    MS-Off Ver
    2003 - 2016
    Posts
    258

    Re: Not able to paste recordset in excel spreadsheet

    Do you understand the concept of recordsets and record pointers?

  5. #5
    Registered User
    Join Date
    11-11-2013
    Location
    pune
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Not able to paste recordset in excel spreadsheet

    No I dont ,in fact I am just starting with VBA from yesterday.
    I have googled and seemed it work like the result set can be pasted via CopyFromRecordset command.

    If you point to anything useful that will be great.

  6. #6
    Forum Contributor
    Join Date
    09-03-2015
    Location
    IE
    MS-Off Ver
    2003 - 2016
    Posts
    258

    Re: Not able to paste recordset in excel spreadsheet

    I am just starting with VBA from yesterday
    http://www.excelforum.com/showthread...t=#post3470996

    I would suggest that, instead of blindly copying something you do not understand, you would learn more by studying the Properties, Methods and Events available for a recordset.

    Basic introduction to recordsets: https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx
    Recordset Properties/Methods: https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx

  7. #7
    Registered User
    Join Date
    11-11-2013
    Location
    pune
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Not able to paste recordset in excel spreadsheet

    Sorry but not helpful.

  8. #8
    Registered User
    Join Date
    11-11-2013
    Location
    pune
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Not able to paste recordset in excel spreadsheet

    This is urgent for me so I have posted and in all these threads back and forth if I could have get the specific answer that would have been a great help.
    I would definitely study about it more but now I need a quick solution to this .
    So please if anyone else could guide me I will really appreciate that.
    And all of you thanks for taking your time out .

  9. #9
    Forum Contributor
    Join Date
    09-03-2015
    Location
    IE
    MS-Off Ver
    2003 - 2016
    Posts
    258

    Re: Not able to paste recordset in excel spreadsheet

    OK, sorry if you decided those links were not helpful. They contain all the information you need, particularly with regards to moving the record pointer.

    You're not asking for 'guidance', you've got that, and I know I could simply say what to do, but that assertion that you're "starting with VBA since yesterday" wasn't true so I'm out of this.

  10. #10
    Registered User
    Join Date
    11-11-2013
    Location
    pune
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Not able to paste recordset in excel spreadsheet

    No issues , thanks .

  11. #11
    Registered User
    Join Date
    11-11-2013
    Location
    pune
    MS-Off Ver
    Excel 2003
    Posts
    8

    Red face Re: Not able to paste recordset in excel spreadsheet

    Not the smartest query but this will work in case anyone has the same problem
    Sub Button1_Click()
    Dim cnn As ADODB.Connection
       Set cnn = New ADODB.Connection
    
    
    Dim Cmd1 As New ADODB.Command
    Dim ReturnArray As DAO.Recordset
    Dim mrs As New ADODB.Recordset
    Dim rsTest As ADODB.Recordset
    Dim DBPath As String, sconnect As String
    
       ' Open a connection using an ODBC DSN "Pubs".
       cnn.ConnectionString = "DSN=XXX;UID=XXX@xxx.com;PWD=xxxx;"
       cnn.Open
    
       ' Find out if the attempt to connect worked.
       If cnn.State = adStateOpen Then
          MsgBox "Echo!"
       Else
          MsgBox "Sorry."
       End If
       
           Cmd1.ActiveConnection = cnn
           Cmd1.CommandText = "sql query"
           mrs.Open Cmd1
           
           
    
     If Not mrs.EOF Then
            Sheets("Sheet2").Range("A1").CopyFromRecordset mrs
        End If
    
    mrs.Close
    
       ' Close the connection.
       cnn.Close
    End Sub
    Any improvements/suggestions are most welcome.

    Thanks,

+ 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. Open another recordset within current recordset vba
    By aman1234 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-02-2015, 04:32 AM
  2. Paste recordset on next empty row inside a table
    By baijixu in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-22-2014, 03:13 PM
  3. Recordset cross referencing spreadsheet data not updating missing values
    By Sc0tt1e in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-20-2013, 08:36 AM
  4. Automated find and paste URL in Excel Spreadsheet
    By chattemer in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-14-2013, 03:29 PM
  5. Replies: 1
    Last Post: 03-09-2006, 02:22 PM
  6. [SOLVED] Recordset's RowID as part of Recordset
    By Trip in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-22-2005, 06:55 PM
  7. Replies: 1
    Last Post: 09-20-2005, 07:05 PM

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