quering access report from excel vba

Omer104

New Member
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!:eek::eek:

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
 

ranman256

Well-known Member
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:

Some videos you may like

This Week's Hot Topics

  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • find many based on a certain criteria
    good evening, I hope someone can help me? I have a workbook sheet 2 contains lots of data.... I would like to be able to find anything on sheet...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
  • Text Format
    I have a sheet for user to keyin the data. The format of the data can be 451 / 1903, 0012 / 9908 or 00287 / 0099. The number after the "/" is...
  • Macro to copy values across rows and transposing them and add the user id
    [FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]Hi,[/COLOR][/SIZE][/FONT] [FONT=Times New...
Top