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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
gigglickCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.