Question Problem using access query as external data source for excel pivot table (1 Viewer)

frankt68

Registered User.
Local time
Today, 15:54
Joined
Mar 14, 2012
Messages
90
I have a problem that is driving me crazy. They replaced my computer at work and I was forced to start using Office 365 and Windows 10. In Windows 7 and Office 2010 everything worked as it should be.

However, in Office 365, I have a problem linking Excel to external data from Access.

I have an Access query, and I would like to use the data from this query in the Excel pivot table. The problem is that all data from the query is not displayed in the pivot table.

This is a picture of data from a query:
Access.jpg
This is a picture of the data from the pivot table:
Excel.jpg

As you can see, there is only one record for each month of the year in the pivot table, but there should be three, as in the query.

Can anyone please tell me how to connect an excel rotation table with an access query so that all the data is shown in the pivot table?
 

Ranman256

Well-known member
Local time
Today, 09:54
Joined
Apr 9, 2015
Messages
4,339
you could either:
use access then export the data and run the pivot.
or
in Excel, instead of an internal link, you could do a live 'download' of data via ado connection.

I use ADO. No DSN to setup.
BE SURE ADO is put in VBE References: alt-F11, tools, references, Microsoft ActiveX Data Objects x.x Library

connect to the db when the workbook opens

Code:
Private Sub Workbook_Open()
  CONNECTDB()
end sub

save data to table:
RunActionQry "Insert into table (clientID,Phone) values (123, '534-555-1234')"

get data from db to spreadsheet:
GetLateRecords()


In VBE , insert MODULE and paste the code below into it.
Code:
public goConn as ADODB.Connection
public gDb


'-----------------
sub GetLateRecords()
'-----------------
dim rst
dim sSql as string
 
sSql = "select * from table where [late]=true"
Set rst = getRst(sSql)

range("A1").select
ActiveCell.CopyFromRecordset rst
set rst = nothing
end subs


'-----------------
Public Function getRst(ByVal pvQry) As ADODB.recordset
'-----------------
Dim rst As ADODB.recordset

On Error GoTo errGetRst

If goConn Is Nothing Then ConnectDB

Set rst = CreateObject("ADODB.Recordset")
With rst
    Set .ActiveConnection = goConn
    .CursorLocation = adUseClient
    .Open pvQry
End With
Set getRst = rst

Exit Function
errGetRst:
MsgBox Err.Description, , "getRst():" & Err
End Function


'-----------------
sub ConnectDB()
'-----------------
'BE SURE ADO is put in VBE References:  alt-F11, tools, references.

gDB = "\\server\folder\myDb.accdb"

sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & gDB
OLEDB:EngineType=4"
Set goConn = New ADODB.Connection
goConn.Open sConnect
end sub


'-----------------
Public Function RunActionQry(pvQry, Optional ByVal pbIsSql As Boolean)
'-----------------
On Error GoTo errRun

  ' Assign to ADO Command object
Set goCmd = New ADODB.Command
With goCmd
  .ActiveConnection = goConn
  .CommandText = pvQry
  
  If pbIsSql Then
    .CommandType = adCmdText
  Else
    .CommandType = adCmdStoredProc
  End If
  
  .Execute
End With
Exit Function

errRun:
RunActionQry = Err
End Function
 

frankt68

Registered User.
Local time
Today, 15:54
Joined
Mar 14, 2012
Messages
90
you could either:
use access then export the data and run the pivot.

Thisis not a convenient option for me.

or
in Excel, instead of an internal link, you could do a live 'download' of data via ado connection.
I don't have much experience with Access or Excel, so I don't really know what is ADO. However, I've imported data from Access to excel via
Insert >> PivotTable >>Use an external data source.
Then I chose the access database and appropriate query. But as you can see, the data didn't import correctly.

code]
Private Sub Workbook_Open()
CONNECTDB()
end sub
[/code]
Where should I put this code?



In VBE , insert MODULE and paste the code below into it.
VBE = visual basic for excel?
 

Users who are viewing this thread

Top Bottom