[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How convert exported file that contains just one record to an Excel file but in one column

Posted on 2014-08-12
14
Medium Priority
?
123 Views
Last Modified: 2014-08-13
I'm exporting a query result to an Excel file via:

DoCmd.TransferSpreadsheet acExport, 10, "qryNotes", Environ("userprofile") & "\Desktop\Notes.xlsx", True

Open in new window


But I need to somehow end up with the field in columns in the Excel file rather than rows.

Is this even possible?

--Steve
0
Comment
Question by:SteveL13
  • 5
  • 5
  • 4
14 Comments
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 40256534
You could try to modify the method shown here:

http://www.experts-exchange.com/Database/MS_Access/Q_28467368.html

/gustav
0
 

Author Comment

by:SteveL13
ID: 40256844
Well, I'm stuck again.  Here is what the export looks like now:

Current
And here's what I want it to look like:

Would like
0
 
LVL 31

Expert Comment

by:Helen Feddema
ID: 40257230
This can be done, but not using TransferSpreadsheet (unless you do the fields to columns conversion first in Access, and then export the resulting table).  Here is some code to export from a one-row table directly to an Excel workbook:
Public Sub FieldsToColumns()
'Created by Helen Feddema 30-Jul-2014
'Last modified by Helen Feddema 12-Aug-2014

On Error GoTo ErrorHandler

   Dim appExcel As New Excel.Application
   Dim wkb As Excel.Workbook
   Dim rst As DAO.Recordset
   Dim fld As DAO.Field
   Dim intFieldCount As Integer
   Dim intCount As Integer
   Dim strFieldName As String
   Dim varFieldValue As Variant
   Dim sht As Excel.Worksheet
   Dim intRow As Integer
   Dim strRange As String
   Dim rng As Excel.Range
   
   Set rst = CurrentDb.OpenRecordset("tblEmployeesOneRecord")
   intFieldCount = rst.Fields.Count
   Debug.Print "No. of Fields: " & intFieldCount
   Set wkb = appExcel.Workbooks.Add
   Set sht = wkb.Sheets(1)
   intRow = 1
   
   For intCount = 0 To intFieldCount - 1
      strFieldName = rst.Fields(intCount).Name
      Debug.Print "Field name: " & strFieldName
      strRange = "A" & CStr(intRow)
      Set rng = sht.Range(strRange)
      rng.Value = strFieldName
      varFieldValue = rst.Fields(intCount)
      Debug.Print "Field value: " & varFieldValue
      strRange = "B" & CStr(intRow)
      Set rng = sht.Range(strRange)
      rng.Value = varFieldValue
      intRow = intRow + 1
   Next intCount
   
   appExcel.Visible = True
   
ErrorHandlerExit:
   Set appExcel = Nothing
   Exit Sub

ErrorHandler:
   MsgBox "Error No: " & Err.Number _
      & " in FieldsToColumns procedure; " _
      & "Description: " & Err.Description
   Resume ErrorHandlerExit

End Sub

Open in new window

0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 31

Expert Comment

by:Helen Feddema
ID: 40257232
Don't try this with an OLE object field -- it won't work.
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 40257813
If you don't want to mess with Excel, you can use a temp table with two text fields:

    FieldName
    FieldValue

Call it tblNotes.
Then run this code to transpose the fields and values:
Public Function FillNotes()

    Dim dbs         As DAO.Database
    Dim rstSource   As DAO.Recordset
    Dim rstTarget   As DAO.Recordset
    Dim fld         As DAO.Field
    
    Set dbs = CurrentDb
    Set rstSource = dbs.OpenRecordset("qryNotes")
    Set rstTarget = dbs.OpenRecordset("tblNotes")
    
    While rstTarget.EOF = False
        rstTarget.Delete
        rstTarget.MoveNext
    Wend
    
    If rstSource.RecordCount = 1 Then
        For Each fld In rstSource.Fields
            rstTarget.AddNew
                rstTarget.Fields(0).Value = fld.Name
                rstTarget.Fields(1).Value = CStr(Nz(fld.Value))
            rstTarget.Update
        Next
    End If
    rstTarget.Close
    rstSource.Close
    
    Set rstTarget = Nothing
    Set rstSource = Nothing
    
    Set dbs = Nothing
    
    ' Run export.
    DoCmd.TransferSpreadsheet acExport, 10, "tblNotes", Environ("userprofile") & "\Desktop\Notes.xlsx", True
    
End Function

Open in new window

/gustav
0
 

Author Comment

by:SteveL13
ID: 40258424
Helen,

The code is working perfectly.  The module does what I wanted it to do.   Now I need the function to save the Excel file to the user's desktop automatically.  Something like this I guess.  But I don't know how to code it and then where to put the code.  

DoCmd.TransferSpreadsheet acExport, 10, "I don't know what to put here", Environ("userprofile") & "\Desktop\Notes.xlsx", True
0
 

Author Comment

by:SteveL13
ID: 40258439
One more thing... I need it to enter only the most recent record in the Excel file.
0
 
LVL 52

Assisted Solution

by:Gustav Brock
Gustav Brock earned 500 total points
ID: 40258453
But you had only one record ...

Else use Top 1 in your query and sort on your "recent" field:

Select Top 1 * From tblYourTable Order By Id Desc

/gustav
0
 

Author Comment

by:SteveL13
ID: 40258500
Gustav:  Works.  But I still need to somehow save the Excel file to the user's desktop automatically.  Something like this I guess.  But I don't know how to code it and then where to put the code.  

 DoCmd.TransferSpreadsheet acExport, 10, "I don't know what to put here", Environ("userprofile") & "\Desktop\Notes.xlsx", True
0
 
LVL 31

Expert Comment

by:Helen Feddema
ID: 40258570
For saving the workbook, you need to specify a path and file name, and possibly a file format (Excel version).  What should they be?  Let me know, and I will post revised code.
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 40258573
No, that's from Access. In Excel, you will add a code line to SaveAs the workbook (can't recall the full syntax off head).

/gustav
0
 

Author Comment

by:SteveL13
ID: 40258581
Wouldn't it be:

Environ("userprofile") & "\Desktop\Notes.xlsx"
0
 
LVL 31

Expert Comment

by:Helen Feddema
ID: 40258697
Which code are you using?
0
 
LVL 31

Accepted Solution

by:
Helen Feddema earned 500 total points
ID: 40258727
Here is my code, modified to save the workbook to the user's desktop.  Modify the user name and file name as you wish.

Public Sub FieldsToColumns()
'Created by Helen Feddema 30-Jul-2014
'Last modified by Helen Feddema 12-Aug-2014

On Error GoTo ErrorHandler

   Dim appExcel As New Excel.Application
   Dim wkb As Excel.Workbook
   Dim rst As DAO.Recordset
   Dim fld As DAO.Field
   Dim intFieldCount As Integer
   Dim intCount As Integer
   Dim strFieldName As String
   Dim varFieldValue As Variant
   Dim sht As Excel.Worksheet
   Dim intRow As Integer
   Dim strRange As String
   Dim rng As Excel.Range
   Dim strXLFile As String
   
   Set rst = CurrentDb.OpenRecordset("tblEmployeesOneRecord")
   intFieldCount = rst.Fields.Count
   Debug.Print "No. of Fields: " & intFieldCount
   Set wkb = appExcel.Workbooks.Add
   Set sht = wkb.Sheets(1)
   intRow = 1
   
   For intCount = 0 To intFieldCount - 1
      strFieldName = rst.Fields(intCount).Name
      Debug.Print "Field name: " & strFieldName
      strRange = "A" & CStr(intRow)
      Set rng = sht.Range(strRange)
      rng.Value = strFieldName
      varFieldValue = rst.Fields(intCount)
      Debug.Print "Field value: " & varFieldValue
      strRange = "B" & CStr(intRow)
      Set rng = sht.Range(strRange)
      rng.Value = varFieldValue
      intRow = intRow + 1
   Next intCount
   
   strXLFile = "C:\Users\Helen Feddema\Desktop\From Access.xlsx"
   wkb.SaveAs FileName:=strXLFile
   appExcel.Visible = True
   
ErrorHandlerExit:
   Set appExcel = Nothing
   Exit Sub

ErrorHandler:
   MsgBox "Error No: " & Err.Number _
      & " in FieldsToColumns procedure; " _
      & "Description: " & Err.Description
   Resume ErrorHandlerExit

End Sub

Open in new window

0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Suggested Courses

825 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