• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Extract from Access to Excel

I have a database (Access) that contains 64 columns of data. I am only interested in pulling 14 of those columns into Excel.


My code works to pull all the data into. So, how do I choose only the columns of data that I want?


Here is the code:


Sub Import_AccessData()

Dim cnt As ADODB.Connection

Dim rst1 As ADODB.Recordset, rst2 As ADODB.Recordset

Dim stDB As String, stSQL1 As String

Dim stConn As String

Dim wbBook As Workbook

Dim wsSheet1 As Worksheet

Dim lnField As Long, lnCount As Long


'Instantiate the ADO-objects.

Set cnt = New ADODB.Connection

Set rst1 = New ADODB.Recordset


Set wbBook = ThisWorkbook

Set wsSheet1 = wbBook.Worksheets(1)


'Path to the database.

stDB = "C:My DocumentsTestConversion DB 2012.mdb"


'Create the connectionstring.

stConn = "Provider=Microsoft.Jet.OLEDB.4.0;" _

& "Data Source=" & stDB & ";"


'The 1st raw SQL-statement to be executed.

stSQL1 = "SELECT * FROM serviceinfo"


'Clear the worksheet.

wsSheet1.Range("A1").CurrentRegion.Clear


With cnt

.Open (stConn) 'Open the connection.

.CursorLocation = adUseClient 'Necessary to disconnect the recordset.

End With


With rst1

.Open stSQL1, cnt 'Create the recordset.

Set .ActiveConnection = Nothing 'Disconnect the recordset.

End With


With wsSheet1

.Cells(2, 1).CopyFromRecordset rst1 'Copy the 1st recordset.

End With


'Release objects from the memory.

rst1.Close

Set rst1 = Nothing

Set rst2 = Nothing

cnt.Close

Set cnt = Nothing

End Sub
 
stSQL1 = "SELECT * FROM serviceinfo"


The "*" means select all data in a sql statement. If you dont want them all then you need to do something like:


'strSQL1= "SELECT [TABLE1], [TABLE2] FROM [ServiceInfo]"


that will give you the data of table1 and table2 (of course you would use the actual columns you want.
 
not a problem at all. I know how frustrating it can get. Really starting to get into T-SQL. Not sure how involved you want/need to get, but just picking up a book or two on T-SQL and sql server really helped.
 
LOL, I suppose at times it can. But the more I go into it, the more logical things get. Not for everyone, thats for sure.
 
Back
Top