Results 1 to 7 of 7

Thread: Export to excel from ADO, and change the names of a columns

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2009
    Posts
    364

    Export to excel from ADO, and change the names of a columns

    Hi,
    is there any possible to change the names of a columns exporting data to excel from mdb file.

    I would like to change "AD" column in mdb to "Index" in Excel. Of course I could change the names in mdb but some of them are very long, and ADO cannot get it.

    I mean how to change in excel Field Name from ADO

    Thanks
    Last edited by Fraps; Apr 21st, 2009 at 08:31 AM.

  2. #2
    Addicted Member
    Join Date
    Mar 2008
    Posts
    143

    Re: Export to excel from ADO, and change the names of a columns

    i think you can change the cell values by creating excel worksheet objects and can access each cell individually i hope it might show you some way or other :-)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2009
    Posts
    364

    Re: Export to excel from ADO, and change the names of a columns

    That's right I would like to change the cell names in Excel document. They should be different than in mdb file. Do you have any example for that?

    Thanks

  4. #4
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: Export to excel from ADO, and change the names of a columns

    Quote Originally Posted by Fraps View Post
    That's right I would like to change the cell names in Excel document. They should be different than in mdb file. Do you have any example for that?

    Thanks
    How are you doing the export? Could you post your code?
    If you are using the built-in feature to export, the column names will not change.

    Attached Images Attached Images  
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2009
    Posts
    364

    Re: Export to excel from ADO, and change the names of a columns

    Hi abhijit

    here below is my code ( class module ):

    Code:
    Public Sub ExportToExcel(Optional SaveFile As Boolean = False, _
                             Optional VisibleInstance As Boolean = True, _
                             Optional Password As String = "", _
                             Optional WriteResPassword As String = "", _
                             Optional ReadOnlyRecommended As Boolean = False, _
                             Optional HeaderFont As String = "Tahoma", _
                             Optional HeaderFontSize As Integer = 9)
    
    Dim iRowIndex As Integer, avRows As Variant, ErrorOccured As Boolean
    Dim iFieldCount As Integer, objExcel As Object, objTemp As Object
    Dim iColIndex As Integer, iRecordCount As Integer
    
    'I know that some tweaking are left here... I gave priority to compability here
    
        On Error GoTo hell
    
        RaiseEvent ExportStarted(EXCEL)
    
        With ADODBRecordset
            .MoveFirst
            avRows = .GetRows()                  'Read all the records in an
            iRecordCount = UBound(avRows, 2) + 1 'array and determine how
            iFieldCount = UBound(avRows, 1) + 1  'many fields in an array
            Set objExcel = CreateObject("Excel.Application")
            objExcel.Visible = VisibleInstance
            objExcel.Workbooks.Add
    
            Set objTemp = objExcel           'Ensure excel remains visible
    
            If Val(objExcel.Application.Version) >= 8 Then
                Set objExcel = objExcel.ActiveSheet
            End If
    
            iRowIndex = 1
    
            'Place Name of the fields
            For iColIndex = 1 To iFieldCount
                With objExcel.Cells(iRowIndex, iColIndex)
                    .Value = ADODBRecordset.Fields(iColIndex - 1).Name
                    With .Font
                        .Name = HeaderFont      'Make the headers stand out
                        .Size = HeaderFontSize
                        .Bold = True
                    End With
                End With
            Next iColIndex
    
        End With
    
        With Progress
            .Min = 0
            .Max = ADODBRecordset.RecordCount
            .Value = 0
        End With
    
        With objExcel
            
            For iRowIndex = 2 To iRecordCount + 1
                For iColIndex = 1 To iFieldCount
                    .Cells(iRowIndex, iColIndex) = avRows(iColIndex - 1, iRowIndex - 2)
                Next iColIndex
                Progress.Value = Progress.Value + 1
                DoEventsEx
            Next iRowIndex
            .Cells(1, 1).CurrentRegion.EntireColumn.AutoFit
            If SaveFile Then
                .SaveAs ExportFilePath, , Password, WriteResPassword, ReadOnlyRecommended
            End If
        End With
        
        If Not VisibleInstance Then objExcel.Application.Quit
        Set objTemp = Nothing
        Set objExcel = Nothing
        
        RaiseEvent ExportComplete(Not ErrorOccured, EXCEL)
    
    Exit Sub
    
    hell:
        RaiseEvent ExportError(Err, EXCEL)
        If Err.Number = 0 Then
            Resume Next
            ErrorOccured = True
        End If
    
    End Sub
    Export to excel sheet works very well, only what I wanna do is to change the cell names in excel on A1, B1, C1, and so on.

    And this is my sheet. Is there any way to change the cells showed above as ID, CM1, CM2 for Index, Index2...

    Regards.
    Last edited by Fraps; Apr 21st, 2009 at 03:39 PM.

  6. #6
    Addicted Member
    Join Date
    Mar 2008
    Posts
    143

    Re: Export to excel from ADO, and change the names of a columns

    Mr.Fraps if i understand correct you almost had it, you just a need a function which gets the header name and return another name of your choice in or you can do like this
    vb Code:
    1. Dim tempval As Integer
    2. tempval = 65
    3.         'Place Name of the fields
    4.         For iColIndex = 1 To iFieldCount
    5.             With objExcel.Cells(iRowIndex, iColIndex)
    6.                 '.Value = ADODBRecordset.Fields(iColIndex - 1).Name
    7.                 .Value = CStr(Chr(tempval)) & 1
    8.                 With .Font
    9.                     .Name = HeaderFont      'Make the headers stand out
    10.                     .Size = HeaderFontSize
    11.                     .Bold = True
    12.                 End With
    13.             End With
    14.             tempval = tempval + 1
    15.         Next iColIndex
    a little tweaking required if z1 occurs you have to reset the tempval and convert the static 1 to a variable to hold 1 and increase once 26
    HTH

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2009
    Posts
    364

    Re: Export to excel from ADO, and change the names of a columns

    Thanks for the suggestions.

    Code:
    Public Sub ExportToExcel(Optional SaveFile As Boolean = False, _
                             Optional VisibleInstance As Boolean = True, _
                             Optional Password As String = "", _
                             Optional WriteResPassword As String = "", _
                             Optional ReadOnlyRecommended As Boolean = False, _
                             Optional HeaderFont As String = "Tahoma", _
                             Optional HeaderFontSize As Integer = 9)
    
    Dim iRowIndex As Integer, avRows As Variant, ErrorOccured As Boolean
    Dim iFieldCount As Integer, objExcel As Object, objTemp As Object
    Dim iColIndex As Integer, iRecordCount As Integer
    
    'I know that some tweaking are left here... I gave priority to compability here
    
        On Error GoTo hell
    
        RaiseEvent ExportStarted(EXCEL)
    
        With ADODBRecordset
            .MoveFirst
            avRows = .GetRows()                  'Read all the records in an
            iRecordCount = UBound(avRows, 2) + 1 'array and determine how
            iFieldCount = UBound(avRows, 1) + 1  'many fields in an array
            Set objExcel = CreateObject("Excel.Application")
            objExcel.Visible = VisibleInstance
            objExcel.Workbooks.Add
    
            Set objTemp = objExcel           'Ensure excel remains visible
    
            If Val(objExcel.Application.Version) >= 8 Then
                Set objExcel = objExcel.ActiveSheet
            End If
    
            iRowIndex = 1
    
            'Place Name of the fields
            For iColIndex = 1 To iFieldCount
                With objExcel.Cells(iRowIndex, iColIndex)
                    .Value = ADODBRecordset.Fields(iColIndex - 1).Name
                    With .Font
                        .Name = HeaderFont      'Make the headers stand out
                        .Size = HeaderFontSize
                        .Bold = True
                        
                        objExcel.Cells.Range("A1").Value = "Index" 
                        objExcel.Cells.Range("B1").Value = "Index2" 
                        objExcel.Cells.Range("C1").Value = "Index3" 
                        objExcel.Cells.Range("D1").Value = "Index4" 
    
                        
                    End With
                End With
            Next iColIndex
    
        End With
    
        With Progress
            .Min = 0
            .Max = ADODBRecordset.RecordCount
            .Value = 0
        End With
    
        With objExcel
            
            For iRowIndex = 2 To iRecordCount + 1
                For iColIndex = 1 To iFieldCount
                    .Cells(iRowIndex, iColIndex) = avRows(iColIndex - 1, iRowIndex - 2)
                Next iColIndex
                Progress.Value = Progress.Value + 1
                DoEventsEx
            Next iRowIndex
            .Cells(1, 1).CurrentRegion.EntireColumn.AutoFit
            If SaveFile Then
                .SaveAs ExportFilePath, , Password, WriteResPassword, ReadOnlyRecommended
            End If
        End With
        
        If Not VisibleInstance Then objExcel.Application.Quit
        Set objTemp = Nothing
        Set objExcel = Nothing
        
        RaiseEvent ExportComplete(Not ErrorOccured, EXCEL)
    
    Exit Sub
    
    hell:
        RaiseEvent ExportError(Err, EXCEL)
        If Err.Number = 0 Then
            Resume Next
            ErrorOccured = True
        End If
    
    End Sub
    I've just put something like this in the middle of the above listing
    objExcel.Cells.Range("A1").Value = "Index"
    .......................
    .......................

    The case resolved thanks
    Last edited by Fraps; Apr 22nd, 2009 at 06:10 AM.

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