Analyse Access data with Excel

stefan3377

New member
Local time
Today, 23:42
Joined
Dec 14, 2005
Messages
7
Access2000 converts data to Excel2000 in the following way: I have specified a column data type as long integer with no "null" decimal place - whenever I analyse the table with Excel the mentioned column suddenly has 2 decimal places??????????
On the other hand when I convert data WITH 2 decimal places from Access to Excel those are displayed as "zero" (e.g. 9,15 --> 9,00) ?!?!
Thanks for any advise!
 
Check the format on the Excel cells. What are you using to get data from the Access database to the Excel sheet?
 
procedure: I run a query which writes the result sets into an existing access table in which I defined the columns as needed - here the first mistake happens - truncation of values (e.g. 158,45 --> 158,00), so actually within access;

the "result" table is then exported by a macro and generates a new excel sheet where the second problem occurs - values of an other column are extended with 2 decimal places (e.g. 9854 --> 9854,00);

above all the generated excel file is a MS Excel 5.0/95 workmap;
 
You're not from the USA, are you? When you say 9,15 you mean 9 + 15/100 right? We write it 9.15 -- post the Visual Basic code that you're using here. I question, though, why you'd want to write it to a table then write it to Excel instead of just writing it straight to Excel ... Oh, you're using macros. They're rather limited. Can you give more details about how you're getting the data and what you want to do with it?
 
You're right, I'm not from the USA - but I'm sure you've heard from Austria (the little country which made 8 gold medals until now during the Olympics in Turin ;) I'll try my best:

*) 9.15 is exactly what I mean

*) we have designed an analyse db in access to provide some users with reports; the reports include data from a MES (manufacturing execution system) -> Oracle8i and from a AS400 system. now in this case we want to gather and store each query result in an extra access table for several reasons (traceability,...) and produce an excel sheet with the access table content -> the user can start the query with the appropriate criteria and follows a link on the LAN to collect the excel sheet (the idea/demand of producing an excel sheet comes from a public authority)

sorry, it's a little bit complicated as we have to work in a validated environment - still I hope you can help me.

*) below the VBA code:


'------------------------------------------------------------
' PoolInfoBIFA
'
'------------------------------------------------------------
Function PoolInfoBIFA()
On Error GoTo PoolInfoBIFA_Err

DoCmd.OpenQuery "PoolInfoBIFA", acDesign, acAdd
' Anfügen PoolInfoBIFA
DoCmd.RunCommand acCmdRun
DoCmd.Close acQuery, "PoolInfoBIFA"
' PoolInfoBIFA
DoCmd.OpenTable "Tab_PoolInfoBIFA", acNormal, acEdit
' Bestehende Ausgabedatei ggf. Loeschen
Call DateiEntfernen("\\Fileserver\ZDoku\Datenbanken\OctaMESAuswertungen\Downloads\PoolInfoBIFA\", "PoolInfoBIFA")
' Schreiben der Abfrage in Datei: PoolInfoBIFA
DoCmd.OutputTo , "", "MicrosoftExcel(*.xls)", "\\Fileserver\ZDoku\Datenbanken\OctaMESAuswertungen\Downloads\PoolInfoBIFA\PoolInfoBIFA.xls", False, ""
DoCmd.Close acTable, "Tab_PoolInfoBIFA"


PoolInfoBIFA_Exit:
Exit Function

PoolInfoBIFA_Err:
MsgBox Error$
Resume PoolInfoBIFA_Exit

End Function
 
I don't output the file to Excel, I open the Excel file, grab the appropriate recordset, then place information from various fields in the table into the appropriate cells in Excel. It leaves the Excel sheet open in case I need to manually change something. It saves the file so that, after I print it to mail to the state, I can close it without receiving the prompt to save. Saving it won't affect the file as new information is just written over old information.
Code:
Private Sub ExportButton_Click()
    ' Exports info to Excel
        ' Code modified from Microsoft Access 2000 Developer's Handbook, Volume I
        ' by Getz, Litwin, and Gilbert. (Sybex)
        ' Copyright 1999. All Rights Reserved.
        '
        'Modifications by Ben Purser
        'Monster, monster assist from NateO at UtterAccess
        'Additional Modifications by Bart "Banaticus" Humphries

        ' Object variables for Automation stuff
    Dim objXLApp As Object
    Dim objXLBook As Object
    Dim objExpSheet As Object
    Dim objXLRange As Object
    Dim cnn As ADODB.Connection
    
    Dim strSQL1 As String 'SQL String for all output
    
    Set cnn = CurrentProject.Connection
    'holds rs for all data output
    Dim rst As New ADODB.Recordset
    
    'define Excel file to use
    Set objXLBook = GetObject("F:\Access\DAS1.xls")
    Set objXLApp = objXLBook.Parent
            
    ' Set object references for the
    ' workbook's two worksheets
    
    Set objExpSheet = objXLBook.Worksheets("Front")
            ' Make sure both Excel and the
            ' workbook are visible (they won't
            ' be if Excel was launched by our
            ' Automation request)
            objXLApp.Visible = True
            objXLBook.Windows(1).Visible = True
    
    
    'sql for primary controls
    
    strSQL = "SELECT * FROM [App Info] WHERE [Soc Sec #] = '" & Forms![Apprentice Information]![Soc Sec #] & "';"
    
    'open Primary Controls to export
    rst.Open strSQL, cnn, adOpenKeyset, adLockOptimistic
    
    objExpSheet.Activate
    rst.MoveFirst
            
    With objExpSheet
    If rst.RecordCount > 0 Then
        '.["D5"].Value = CStr(rst![Last Name])
        .[A7].Value = CStr(Nz(rst![Last Name], ""))
        .[E7].Value = CStr(Nz(rst![First Name], ""))
        .[J7].Value = CStr(Nz(rst![MA], ""))
        .[N7].Value = CStr(Nz(rst![Soc Sec #], ""))
        .[A9].Value = CStr(Nz(rst![M Address #1], ""))
        .[A10].Value = CStr(Nz(rst![M City], ""))
        .[E10].Value = CStr(Nz(rst![MS], ""))
        .[G10].Value = CStr(Nz(rst![M Zip], ""))
        .[K9].Value = CStr(Nz(rst![Birth], ""))
        .[K11].Value = CStr(Nz(rst![Location], ""))
    End If
    objXLBook.Save
    rst.Close
    End With
    Set objExpSheet = objXLBook.Worksheets("Front")
    objExpSheet.Activate

    Set cnn = Nothing
    Set objXLApp = Nothing
    Set objXLBook = Nothing
    Set objExpSheet = Nothing
End Sub
How does saving it into a table help you trace what's going on? You write over the table every time, correct?
 
Last edited:
sorry, I'm not in office this week - will try the procedure given above next week. thanks for your help!
 
You may or may not find the following useful. It's a procedure that exports data to be archived to an Excel worksheet but then removes the exported records from the Access tables. It's from Access Watch.
To join Access Watch: waw@office-watch.com to leave AW see bottom line of this issue.
Access Watch (AW to its friends) is copyright � 2006 Peter Deegan and Helen Feddema. All rights reserved. ISSN 1442-827X
Editor-in-chief: Peter Deegan. Editor: Helen Feddema.
The zip file containing this article, in Word format, plus the supporting file(s), may be downloaded from the Access Archon page of my Web site. It is accarch143.zip, which is the last entry in the table of Access Archon columns for Access Watch.
Code:
Public Sub ArchiveData(dteStart As Date, dteEnd As Date)
On Error GoTo ErrorHandler
   Dim appExcel As Excel.Application
   Dim intReturn As Integer
   Dim lngCount As Long
   Dim n As Long
   Dim rng As Excel.Range
   Dim rngStart As Excel.Range
   Dim strDBPath As String
   Dim strPrompt As String
   Dim strQuery As String
   Dim strSaveName As String
   Dim strSheet As String
   Dim strSheetTitle As String
   Dim strSQL As String
   Dim strTemplate As String
   Dim strTemplateFile As String
   Dim strTemplatePath As String
   Dim strTitle As String
   Dim wkb As Excel.Workbook
   Dim wks As Excel.Worksheet

   strQuery = "qryArchive"
   Set dbs = CurrentDb
   strSQL = "SELECT * FROM tblOrders WHERE " _
      & "[ShippedDate] Between #" & dteStart & "# And #" & dteEnd & "#;"
   Debug.Print "SQL for " & strQuery & ": " & strSQL
   lngCount = CreateAndTestQuery(strQuery, strSQL)
   Debug.Print "No. of items found: " & lngCount

   If lngCount = 0 Then
      strPrompt = "No orders found for this date range; canceling archiving"
      strTitle = "Canceling"
      MsgBox strPrompt, vbOKOnly + vbCritical, strTitle
      GoTo ErrorHandlerExit
   Else
      strPrompt = lngCount & " orders found in this date range; archive them?"
      strTitle = "Archiving"
      intReturn = MsgBox(strPrompt, vbYesNo + vbQuestion, strTitle)
      If intReturn = vbNo Then
         GoTo ErrorHandlerExit
      End If
   End If

   'Create new worksheet from template and export data to it
   strDBPath = Application.CurrentProject.Path & "\"
   Debug.Print "Current database path: " & strDBPath
   strTemplate = "Orders Archive.xlt"
   strTemplateFile = strDBPath & strTemplate
   If TestFileExists(strTemplateFile) = False Then
      strTitle = "Template not found"
      strPrompt = "Excel template 'Orders Archive.xlt'" _
         & " not found in " & strDBPath & ";" & vbCrLf _
         & "please put template in this folder and try again"
      MsgBox strPrompt, vbCritical + vbOKOnly, strTitle
      GoTo ErrorHandlerExit
   Else
      Debug.Print "Excel template used: " & strTemplateFile
   End If

   Set appExcel = GetObject(, "Excel.Application")
   Set rst = dbs.OpenRecordset("qryRecordsToArchive")
   Set wkb = appExcel.Workbooks.Add(strTemplateFile)
   Set wks = wkb.Sheets(1)
   wks.Activate
   appExcel.Visible = True

   'Write date range to title cell
   Set rng = wks.Range("A1")
   strSheetTitle = "Archived Orders for " & Format(dteStart, "d-mmm-yyyy") _
      & " to " & Format(dteEnd, "d-mmm-yyyy")
   Debug.Print "Sheet title: " & strSheetTitle
   rng.Value = strSheetTitle

   'Go to first data cell
   Set rngStart = wks.Range("A4")
   Set rng = wks.Range("A4")

   'Reset lngcount to number of records in query
   rst.MoveLast
   rst.MoveFirst
   lngCount = rst.RecordCount

   For n = 1 To lngCount
      'Write data from recordset to worksheet
      rng.Value = Nz(rst![OrderID])
      Set rng = rng.Offset(columnoffset:=1)
      rng.Value = Nz(rst![Customer])
      Set rng = rng.Offset(columnoffset:=1)
      rng.Value = Nz(rst![Employee])
      Set rng = rng.Offset(columnoffset:=1)
      rng.Value = Nz(rst![OrderDate])
      Set rng = rng.Offset(columnoffset:=1)
      rng.Value = Nz(rst![RequiredDate])
      Set rng = rng.Offset(columnoffset:=1)
      rng.Value = Nz(rst![ShippedDate])
      Set rng = rng.Offset(columnoffset:=1)
      rng.Value = Nz(rst![Shipper])
      Set rng = rng.Offset(columnoffset:=1)
      rng.Value = Nz(rst![Freight])
      Set rng = rng.Offset(columnoffset:=1)
      rng.Value = Nz(rst![ShipName])
      Set rng = rng.Offset(columnoffset:=1)
      rng.Value = Nz(rst![ShipAddress])
      Set rng = rng.Offset(columnoffset:=1)
      rng.Value = Nz(rst![ShipCity])
      Set rng = rng.Offset(columnoffset:=1)
      rng.Value = Nz(rst![ShipRegion])
      Set rng = rng.Offset(columnoffset:=1)
      rng.Value = Nz(rst![ShipPostalCode])
      Set rng = rng.Offset(columnoffset:=1)
      rng.Value = Nz(rst![ShipCountry])
      Set rng = rng.Offset(columnoffset:=1)
      rng.Value = Nz(rst![Product])
      Set rng = rng.Offset(columnoffset:=1)
      rng.Value = Nz(rst![UnitPrice])
      Set rng = rng.Offset(columnoffset:=1)
      rng.Value = Nz(rst![Quantity])
      Set rng = rng.Offset(columnoffset:=1)
      rng.Value = Nz(rst![Discount])

      'Go to next row
      rst.MoveNext
      Set rng = rngStart.Offset(rowoffset:=n)

   Next n

   'Save and close filled-in worksheet, using workbook save name
   'with date range
   strSaveName = strDBPath & strSheetTitle & ".xls"
   Debug.Print "Time sheet save name: " & strSaveName

   ChDir strDBPath

On Error Resume Next
   'If there already is a saved worksheet with this name, delete it
   Kill strSaveName

On Error GoTo ErrorHandler
   wkb.SaveAs FileName:=strSaveName, FileFormat:=xlNormal
   wkb.Close
   rst.Close

   appExcel.Visible = False
   Set appExcel = Nothing
   strTitle = "Workbook created"
   strPrompt = "Archive workbook '" & strSheetTitle & "'" & vbCrLf _
      & "created in " & strDBPath
   MsgBox strPrompt, vbOKOnly + vbInformation, strTitle

  'Delete archived records, processing "many" table first
   strSQL = "DELETE tblOrderDetails.*, tblOrders.ShippedDate " _
      & "FROM tblOrderDetails INNER JOIN qryArchive " _
      & "ON tblOrderDetails.OrderID = qryArchive.OrderID;"
   DoCmd.RunSQL strSQL
   strSQL = "DELETE tblOrders.* FROM tblOrders WHERE " _
      & "[ShippedDate] Between #" & dteStart & "# And #" & dteEnd & "#;"
   DoCmd.RunSQL strSQL

   strTitle = "Records cleared"
   strPrompt = "Archived records from " & Format(dteStart, "d-mmm-yyyy") _
      & " to " & Format(dteEnd, "d-mmm-yyyy") & " cleared from tables"
   MsgBox strPrompt, vbOKOnly + vbInformation, strTitle

ErrorHandlerExit:
   Exit Sub
ErrorHandler:
   'Excel is not running; open Excel with CreateObject
   If Err.Number = 429 Then
      Set appExcel = CreateObject("Excel.Application")
      Resume Next
   Else
      MsgBox "Error No: " & Err.Number & "; Description: "
      Resume ErrorHandlerExit
   End If
End Sub
 
Hi! O.k. this will take time to check as vba is not quite my strength and I am facing a very busy time at the moment.
Thanks for helping!!!!!!!!!!
Stefan
 

Users who are viewing this thread

Back
Top Bottom