Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 44
Default Return table fieldname on recordset

Hi,

I am trying to include the field name of the table to return to a excel
sheet, but not a clue on how to call out the field names for all the columns
in the table using recordset. The results i got so far returns only the data
but not the field name or title in first row. how can i include that in the
recordset coding?

Below is my code for reference. can anyone advise? thanks in advance.

Option Compare Database

Public Function CopyRecordset2XL()
Dim objXLApp As Object
Dim objXLWb As Object
Dim objXLWs As Object
Dim strWorkBook As String
Dim strWorkSheet As String
Dim lngSheets As Long 'sheet number
Dim lngCount As Long 'counter

Dim MyDB As DAO.Database
Set MyDB = CurrentDb
Dim RecordMRP As DAO.Recordset

Dim strCrit As String
Dim strSQL As String


strCrit = [Forms]![QuerybyYear]![CustPN]

strSQL = "SELECT [2006 Full].Account,[2006 Full].[Customer PN], [2006
Full].[Mfg PN], "
strSQL = strSQL & "[2006 Full].[FC Load Date],[2006 Full].[LT Qty],
[2006 Full].[Cust OH],"
strSQL = strSQL & "[2006 Full].[Req Resv], [2006 Full].[MRP Resv], [2006
Full].[MRP BO], "
strSQL = strSQL & "[2006 Full].ATS, [2006 Full].[YTD Sales], [2006
Full].[Whse ATS], [2006 Full].[Avg Cost]"
strSQL = strSQL & "FROM [2006 Full] "
strSQL = strSQL & "WHERE [2006 Full].[Customer PN]='" & strCrit & "'"
'you have extra left bracket plus string parameter should be in quotes
Set RecordMRP = MyDB.OpenRecordset(strSQL)

Set objXLApp = CreateObject("Excel.Application")

'name and full path to use to save the xls file
'only create workbooks with 1 sheet
lngCount = objXLApp.SheetsInNewWorkbook 'save user's setting
strWorkBook = "\\txfil001\MKoh$\My WorkStation\Copy of Copy of MRPbyPN.xls"
objXLApp.SheetsInNewWorkbook = 1 'set for only 1 sheet
Set objXLWb = objXLApp.Workbooks.Open(strWorkBook)
objXLApp.SheetsInNewWorkbook = lngCount 'restore user's setting

strWorkSheet = "Sheet1"

Set objXLWs = objXLWb.Worksheets(strWorkSheet)
objXLWs.Range("A1").CopyFromRecordset RecordMRP 'I want to copy start from
Header!
objXLWs.Columns.AutoFit

objXLWb.Save
objXLWb.Close

Set objXLWs = Nothing
Set objXLApp = Nothing

RecordMRP.Close
Set RecordMRP = Nothing
Set MyDB = Nothing
MsgBox ("Finally, i get it to work!!!")

End Function

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default Return table fieldname on recordset

Hi,
I am not using DAO but ADO.

I think you can use the following :
Sub Print_Field_Names()
Dim fld As Field

Set rst = Me.Recordset
For Each fld in rst.Fields
' Print field names.
Debug.Print fld.Name
Next
End Sub
ore something like (untested)
dim x
for x=o to rst.fields.count-1' filed collection is O based in ADO
debug.print rst.field(x).name
next x
HTH
Regards
Jean-Yves

"Junior728" wrote in message
...
Hi,

I am trying to include the field name of the table to return to a excel
sheet, but not a clue on how to call out the field names for all the
columns
in the table using recordset. The results i got so far returns only the
data
but not the field name or title in first row. how can i include that in
the
recordset coding?

Below is my code for reference. can anyone advise? thanks in advance.

Option Compare Database

Public Function CopyRecordset2XL()
Dim objXLApp As Object
Dim objXLWb As Object
Dim objXLWs As Object
Dim strWorkBook As String
Dim strWorkSheet As String
Dim lngSheets As Long 'sheet number
Dim lngCount As Long 'counter

Dim MyDB As DAO.Database
Set MyDB = CurrentDb
Dim RecordMRP As DAO.Recordset

Dim strCrit As String
Dim strSQL As String


strCrit = [Forms]![QuerybyYear]![CustPN]

strSQL = "SELECT [2006 Full].Account,[2006 Full].[Customer PN], [2006
Full].[Mfg PN], "
strSQL = strSQL & "[2006 Full].[FC Load Date],[2006 Full].[LT Qty],
[2006 Full].[Cust OH],"
strSQL = strSQL & "[2006 Full].[Req Resv], [2006 Full].[MRP Resv],
[2006
Full].[MRP BO], "
strSQL = strSQL & "[2006 Full].ATS, [2006 Full].[YTD Sales], [2006
Full].[Whse ATS], [2006 Full].[Avg Cost]"
strSQL = strSQL & "FROM [2006 Full] "
strSQL = strSQL & "WHERE [2006 Full].[Customer PN]='" & strCrit & "'"
'you have extra left bracket plus string parameter should be in quotes
Set RecordMRP = MyDB.OpenRecordset(strSQL)

Set objXLApp = CreateObject("Excel.Application")

'name and full path to use to save the xls file
'only create workbooks with 1 sheet
lngCount = objXLApp.SheetsInNewWorkbook 'save user's setting
strWorkBook = "\\txfil001\MKoh$\My WorkStation\Copy of Copy of
MRPbyPN.xls"
objXLApp.SheetsInNewWorkbook = 1 'set for only 1 sheet
Set objXLWb = objXLApp.Workbooks.Open(strWorkBook)
objXLApp.SheetsInNewWorkbook = lngCount 'restore user's setting

strWorkSheet = "Sheet1"

Set objXLWs = objXLWb.Worksheets(strWorkSheet)
objXLWs.Range("A1").CopyFromRecordset RecordMRP 'I want to copy start from
Header!
objXLWs.Columns.AutoFit

objXLWb.Save
objXLWb.Close

Set objXLWs = Nothing
Set objXLApp = Nothing

RecordMRP.Close
Set RecordMRP = Nothing
Set MyDB = Nothing
MsgBox ("Finally, i get it to work!!!")

End Function




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Import CSV fieldname problem SingLoke Excel Programming 6 February 6th 07 12:35 PM
Return ADO recordset to worksheet Terri[_6_] Excel Programming 3 June 9th 06 05:23 PM
How to know Rank Of FieldName ?? Samary Excel Worksheet Functions 0 April 26th 06 04:23 AM
ADO cloned recordset doesn't return field names quartz[_2_] Excel Programming 3 March 16th 05 09:49 PM
return recordset Laurent M Excel Discussion (Misc queries) 4 January 26th 05 09:43 AM


All times are GMT +1. The time now is 08:51 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"