quering access report from excel vba

Omer104

New Member
Joined
Oct 5, 2015
Messages
42
Office Version
  1. 365
Hi guys, can someone help i am trying to do vba/access link for the first time, and its quite hard!

I am trying to access a report in access (which has a password) called R_UCITS-AIFM_Summary
via an excel VBA, my first issue is normally input two dates, start and end date (normally the same) then press run (in access) but i am trying to do it all through vba excel,

Can anyone kindly shed some light for me?
I keep getting Run time error,Automation error and unspecfied error rolled into one message which is how bad its all going!:eeek::eeek:

here is my code so far

Sub getdatafromaccess()


Dim DBFullname As String
Dim Connect As String, Source As String
Dim Connection As ADODB.Connection
Dim Recorder As ADODB.Recordset
Dim Col As Integer
Dim pword As String


Cells.Clear


pword = "ABC"


DBFullname = "Z:\Portfolio Analysis\Exposure Monitoring\PAS_ACCESS.accdb"


Set Connection = New ADODB.Connection
Connect = "Provider=Microsoft.ACE.OLEDB.12.0;"
Connect = Connect & "Data source=" & DBFullname & ";" & "Jet OLEDB:Database Password" = pword
Connection.Open ConnectionString:=Connect


Set Recordset = New ADODB.Recordset
With Recordset


Source = "Select * from R_UCITS-AIFM_Summary"
.Open Source = Source, Connection, ActiveConnection:=Connection


For co = 0 To Recordset.Fields.Count - 1
Range("A1").Offset(0, Col).Value = Recordset.Fields(Col).Name
Next


Range("A1").Offset(1, 0).CopyFromRecordset Recordset
End With
Set Recordset = Nothing
Connection.Close
Set Connection = Nothing


End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
On a sheet , put the two dates in 2 cells.
B1=start date, B2=end date

alter the SQL,:

Code:
if range("B1").value ="" then
    sSql= "select* from table"
else
     sSql= "select* from table where [datefld] between #" & range("b1").value & "# and #" &  range("b2").value & "#"
end if
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top