Solved

Fromat excell sheets from vb 6 application

Posted on 2004-10-29
172 Views
Last Modified: 2010-05-02
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
0
Question by:shayne23d
    4 Comments
     
    LVL 8

    Expert Comment

    by:ampapa
    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
     
    LVL 8

    Expert Comment

    by:ampapa
    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
     

    Author Comment

    by:shayne23d
    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
     
    LVL 5

    Accepted Solution

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Product Review - Android Remix

    Come along for the ride with our Senior Product Manager, Brian Matis, as he reviews the Android Remix.

    Suggested Solutions

    Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now