Fromat excell sheets from vb 6 application

Hi I am calling excell from vb 6  and filling it with the record set adodc2.recordset that is comming from an sql database.
the first int in the database comes into the excell file as a number. then the next row comeas as a date which is correct. The problem is that excell treis to make the following numbers into dates. Not good.
How can I format the cells in the excell to reflect the database?
here is the code I am using. Also i am not getting any headers with the code, and am not sure of how to get that. so in essence is a two part question.

Dim rst As ADODB.Recordset
Dim xl As Object
Dim wbk As Object
 
  Set xl = CreateObject("Excel.Application")
  xl.Visible = True


   Set rst = Adodc2.Recordset
 
  If Not rst.EOF Then
    Set wbk = xl.Workbooks.Add
   
    wbk.Sheets(1).Range("A1").Select
    wbk.ActiveSheet.Cells
    wbk.ActiveSheet.Cells.CopyFromRecordset rst
   
  End If
  Set rst = Nothing

Thanks in advance.

Shayne
shayne23dAsked:
Who is Participating?
 
gigglickConnect With a Mentor Commented:
Shayne --

Just a help note in general.  For most things you need to do in Excel you can go to Tools --> Macro   --> Record new macro from the main menu.  Do the editing you want to do from VB and then end the macro.  After you are done go to Tools --> Macro --> Macros (now select the Macro you just created). --> Step Into .

The VB code you need to format/edit shown in the macro will be the same code you need for the front end of your program.   (You may need to delete extra "junk" code that excel adds but it makes life much easier).

Also, remember to add your excel application or sheet variable name in front of the lines of code.

Hope this helps.
0
 
ampapaCommented:
This link will help with the field names:http://www.exceltip.com/show_tip/Import_and_Export_in_VBA/Import_data_from_a_closed_workbook_(ADO)_using_VBA_in_Microsoft_Excel/429.html

As far as formating goes you can apply regular Excel formats to the data, e.g.  Format("A1","#,##0.00").

Are you having a specific format issue? Are you re-running the query on an excel worksheet that has been formatted as dates?
0
 
ampapaCommented:
A little clarification about the formatting, you'll have to apply a format to the data after your sub finishes as it is pulled in as a unit of data as opposed to line by line field by field.


Dim rst As ADODB.Recordset
Dim xl As Object
Dim wbk As Object
Dim i as integer

  Set xl = CreateObject("Excel.Application")
  xl.Visible = True


   Set rst = Adodc2.Recordset
 
  If Not rst.EOF Then
    Set wbk = xl.Workbooks.Add
    Set TargetCell = TargetRange.Cells(1, 1)

        'This should pull in the Field names
        For i = 0 To rst.Fields.Count - 1
            TargetCell.Offset(0, i).Formula = rst.Fields(i).Name
        Next i

    wbk.Sheets(1).Range("A2").Select
    wbk.ActiveSheet.Cells
    wbk.ActiveSheet.Cells.CopyFromRecordset rst
   
  End If
  Set rst = Nothing
0
 
shayne23dAuthor Commented:
so far so good. the one thing that is not clear to me.
Say I need cell e to be formatted in excell as currency.
how to do that?
0
All Courses

From novice to tech pro — start learning today.