Results 1 to 8 of 8

Thread: Exporting data to Excel from vb6 [RESOLVED]

  1. #1

    Thread Starter
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Resolved Exporting data to Excel from vb6 [RESOLVED]

    I am writing a program where I am collecting data from different users. It could be say their contact details...

    I have two questions...

    QUESTION ONE: Every day a new excel file should be created. The name of that file could be the date, say ddmmyyyy.xls

    QUESTION TWO: How do I transfer the data to DDMMYYYY.xls?

    a)if it has to be exported from access database to excel then how do i do that programatically?

    b)if different users update the form at the same time then how do i update the excel file at the same time?

    rgds

    koolsid
    Last edited by Siddharth Rout; Feb 11th, 2005 at 07:22 AM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Exporting data to Excel from vb6

    1. Set your filename like this:
    VB Code:
    1. Dim sFileName as String
    2. sFileName = "c:\my folder\" & Format(date, "ddmmyyyy") & ".xls"
    2. a) Set up the filename as above, and get the data into a recordset. Then do this:
    VB Code:
    1. Dim oXLApp as Object       'Declare the object variables
    2. Dim oXLBook as Object
    3. Dim oXLSheet as Object
    4.   Set oXLApp = CreateObject("Excel.Application")  'Create a new instance of Excel
    5.   If Dir(sFileName) = "" Then
    6.     Set oXLBook = oXLApp.Workbooks.Add  'File doesnt exist - add a new workbook
    7.   Else
    8.     Set oXLBook = oXLApp.Workbooks.Open(sFileName)  'File exists - load it
    9.   End If
    10.   Set oXLSheet = oXLBook.Worksheets(1)  'Work with the first worksheet
    11.  
    12.   oXLSheet.UsedRange.Clear
    13. 'replace "oRecordset" with the name of your recordset
    14.   oXLSheet.Range("A1").CopyFromRecordset [b]oRecordset[/b]
    15.  
    16.   Set oXLSheet = Nothing               'disconnect from the Worksheet
    17.   oXLBook.SaveAs sFileName  'Save (and disconnect from) the Workbook
    18.   oXLBook.Close SaveChanges:= False
    19.   Set oXLBook = Nothing
    20.   oXLApp.Quit                                'Close (and disconnect from) Excel
    21.   Set oXLApp = Nothing
    This assumes that you want all of the data to be replaced, if this isnt the case you'll need to use different code instead of the middle section.

    2. b) erm.. basically you can't, but this is very quick so it shouldn't be an issue.
    Last edited by si_the_geek; Feb 11th, 2005 at 04:57 AM. Reason: oXLSheet was not defined (oXLBook was twice!)

  3. #3

    Thread Starter
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Exporting data to Excel from vb6

    Thanx si_the_geek

    I'll try that code right away.

    I can get the data into a recordset from an access table but i haven't tried the same with excel... any tips on that???

    any components/references to be selected before i go ahead with the coding?

    cheers mate

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Exporting data to Excel from vb6

    I can get the data into a recordset from an access table but i haven't tried the same with excel... any tips on that???
    I'm not sure what you mean.. I thought you wanted to get the data from Access? The code I posted will take a recordset (which you have got from anywhere you like) and put it into Excel.

    any components/references to be selected before i go ahead with the coding?
    As I have posted it, no. I did it this way to save issues when it is deployed to your users (different versions of Excel still work with this code, but not the "proper" version below).

    If you want to make changes to the code yourself then I would recommend adding "Microsoft Excel X.X Object Library", and changing the "Dim .." lines to:
    VB Code:
    1. Dim oXLApp as Excel.Application       'Declare the object variables
    2. Dim oXLBook as Excel.Workbook
    3. Dim oXLSheet as Excel.Worksheet
    ..and changing it back when you deploy it to your users.

    ps: I spotted an error in the Dim lines in my post above, I have now changed it!

  5. #5

    Thread Starter
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Exporting data to Excel from vb6

    Sorry to confuse you... even i got confused for a sec

    Amazing it works...

    Thanks a ton si_the_geek

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Exporting data to Excel from vb6 [RESOLVED]

    No problem, I'm happy to help

  7. #7
    New Member
    Join Date
    Sep 2012
    Posts
    3

    Re: Exporting data to Excel from vb6 [RESOLVED]

    Hi to all. I've got the same problem... exporting data from vb 6.0 to excel... I'm new with programming and I really need help. I have made a form in visual basic with data for partnes from ms sql base - fields are ID, Name, Adress, City... and that information i have to put in excel file... I've tried to change the code from that you wrote it above, but it can't work... Please help me. Here is a picture how it must looks like... Name:  01.PNG
Views: 21739
Size:  9.2 KB

    sorry if my eng is not very good...

    Edit: Problem is resolved!
    Last edited by Vespertilio; Sep 26th, 2012 at 07:42 AM.

  8. #8
    Registered User
    Join Date
    Sep 2013
    Posts
    4

    Red face Exporting data to Excel from vb6 ( Answer )

    Dim WoStr As String
    Dim WoCon As New ADODB.Connection
    WoStr = "PROVIDER=MICROSOFT.JET.OLEDB.3.51; DATA SOURCE = "databasename.mdb;Jet OLEDB:"
    WoCon.ConnectionString = WoStr
    WoCon.Open
    Dim Exl As New ADODB.Recordset
    Exl.Open "Select Fld1, Fld2 from StpOrder", WoStr, 1, 2
    Dim ObjExcel As New Excel.Application


    Dim XLCoun As Integer
    With ObjExcel
    .Visible = True
    .Workbooks.Add
    XLCoun = 6
    For i = 0 To Exl.RecordCount - 1

    .Range("A" & XLCoun).Select: .ActiveCell.FormulaR1C1 = Format(Exl!Fld1, "dd-mm-yyyy"): .ActiveCell.ColumnWidth = 12
    .Range("B" & XLCoun).Select: .ActiveCell.FormulaR1C1 = Exl!Fld2
    XLCoun = XLCoun + 1
    next
    end with

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width