Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export table field names 1

Status
Not open for further replies.

gj0519

MIS
May 8, 2003
69
US
Does anyone know how to export the field names along with records from a table. I can export all of my records but would like the field names as well.

Thanks,

GJ
 
I can export all of my records
How did you that ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Here is my code.
Code:
Function Export()

    Dim xlobj As Object
    Dim objWKB As Object, objSHT As Object
    Dim rs As DAO.Recordset
    Dim db As Database
    Dim strSh As String
    
    Const conSHEET1 = "SHARES"
    
    Const conBK = "C:\Documents and Settings\Workstation\Desktop\Shares.xls"
    Set xlobj = CreateObject("excel.application")
    Set db = CurrentDb
    xlobj.Application.Visible = True
    With xlobj
       .workbooks.Open filename:=conBK
    
    strSh = ("SELECT D1NAME AS NAME, MEMBER_NBR FROM tblShares")
    Set rs = db.OpenRecordset(strSh, dbOpenSnapshot)
    Set objSHT = .worksheets(conSHEET1)
    With objSHT
       .range("A1").copyfromrecordset rs
    End With
    .activeworkbook.Close savechanges:=True
    xlobj.Quit
    
    End With
    Set xlobj = Nothing
          
    
End Function
 
With objSHT
For i = 0 to rs.Fields.Count - 1
.Cells(1, i + 1).Value = rs.Fields(i).Name
Next i
.Range(.Cells(1, 1), .Cells(1, rs.Fields.Count)).Font.Bold = True
.Range("A2").CopyFromRecordset rs
End With


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
You might find the following approach a little easier:
Code:
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel97, sQueryName, conBK

You would need to store your select statement in a query to use this technique.
 
PHV, I didn't see your response when I posted mine (it was a timing thing). Sorry.
 
ddiamond, why Sorry ?
They are no proprietary threads here !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top