+ Reply to Thread
Results 1 to 6 of 6

Read from SQL variable column name to excel sheet

  1. #1
    Registered User
    Join Date
    08-31-2012
    Location
    The Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    5

    Read from SQL variable column name to excel sheet

    Hello all,

    I hope somebody can help me out.

    In my code the SQL column name is a variable.
    The columns are named: KWR1, KWR1, KWR3 and KWR4 (where I use a variable for the number (mytel2))
    The choice is made in an userform (4 choices).
    Afterwards I would like to copy the data from one of the columns in a range.
    Below is the code I have, so far, but get an error at: Range("A" & mytel2) = rc![" & kolom & "]

    Please Login or Register  to view this content.

  2. #2
    Registered User
    Join Date
    03-31-2006
    MS-Off Ver
    Excel 2003
    Posts
    76

    Re: Read from SQL variable column name to excel sheet

    What is in rc![" & kolom & "]? Does the recordset open at all?

  3. #3
    Registered User
    Join Date
    08-31-2012
    Location
    The Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Read from SQL variable column name to excel sheet

    I am sorry, I don't understand exactly what you mean?
    rc![" & kolom & "] = the data from column KWR1 or KWR2 or KWR3 or KWR4.
    I have opened the recordset in a part of the code just above the code which is displayed in post 1.

    The strange thing is, that the variable in rc.Open "SELECT [" & kolom & "] FROM " & jaar & " WHERE [" & kolom & "] != ''", con
    works fine, but when I would like to retrieve the data to copy that in the cell in Excel, I get an error
    Range("A" & mytel2) = rc![" & kolom & "]

  4. #4
    Registered User
    Join Date
    03-31-2006
    MS-Off Ver
    Excel 2003
    Posts
    76

    Re: Read from SQL variable column name to excel sheet

    Please Login or Register  to view this content.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Read from SQL variable column name to excel sheet

    How many records are returned?

    What error do you get with this code?
    Please Login or Register  to view this content.
    Does this work?
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  6. #6
    Registered User
    Join Date
    08-31-2012
    Location
    The Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Read from SQL variable column name to excel sheet

    Sorry for my late response, but....

    Thanks!!! using rc.Fields(kolom) works great!
    Problem solved

+ 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. Replies: 2
    Last Post: 01-22-2014, 05:36 PM
  2. [SOLVED] Copy variable range from sheet to the last row with a specific blank column in new sheet
    By seputus in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-22-2012, 07:29 PM
  3. How to create Read/Write VBA Userform that references variable cells/rows in a sheet?
    By Sivart9876 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-16-2012, 10:58 AM
  4. read column row by row and reference to different sheet
    By petebmor in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 08-14-2012, 09:43 PM
  5. read down column L row by row, if cell has a value copy entire row to new sheet
    By myjebay1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-02-2012, 03:56 PM

Tags for this Thread

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