Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Fromat excell sheets from vb 6 application

Posted on 2004-10-29
4
Medium Priority
?
223 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
Comment
Question by:shayne23d
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Expert Comment

by:ampapa
ID: 12446590
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
ID: 12446640
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
ID: 12446892
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:
gigglick earned 2000 total points
ID: 12447047
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

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

610 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