I use Excel 2003 SP3 and I currently have a macro in Excel that runs an access query and writes the records into Excel, which works perfectly fine.
However, as you can see in the codes below, the sql query in embedded into the sub as strings. I can't help but feel that this is a very amateurish way (correct me if I'm wrong), since I would have to write it in access first to make sure it works, and port it over to excel.
I would appreciate any suggestions for improvements in any parts of my code, as well as whether there is a way to run an access query directly from access without porting it over to excel.
thank you for your time.
However, as you can see in the codes below, the sql query in embedded into the sub as strings. I can't help but feel that this is a very amateurish way (correct me if I'm wrong), since I would have to write it in access first to make sure it works, and port it over to excel.
I would appreciate any suggestions for improvements in any parts of my code, as well as whether there is a way to run an access query directly from access without porting it over to excel.
thank you for your time.
Code:
Public Sub gef_query()
Dim connectionString As String
Dim connection As ADODB.connection
Dim rst As ADODB.Recordset
Dim introw As Integer
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Range("DBPath") & ";" & _
"Persist Security Info=False;"
Set connection = New ADODB.connection
connection.Open connectionString
Set rst = New ADODB.Recordset
'switch to set cumulative data from selected date to current date
If (Range("date_switch") = "True") Then
'selected date to current date
eq = ">="
Else
'selected date only
eq = "="
End If
introw = 4
With rst
.ActiveConnection = connection
'SQL
connectionString = "SELECT GCX.run_date AS [Run Date], ISS.issuer_name AS [Issuer Name], ISS.country_res " & _
"AS [Country of Domicile], PDT.product_type AS [Product Type], IIF(GEN.description Is Null,Null," & _
"GEN.description) AS [Product Class], SUP.share_os AS [Shares OS], " & _
"sum(IIF(GCX.transit In('" & Range("SBE") & "'),GCX.posn_unit,Null)) AS [SBE], " & _
"sum(IIF(GCX.transit In('" & Range("SIL") & "'),GCX.posn_unit,Null)) AS [SIL], " & _
"sum(IIF(GCX.transit In('" & Range("BNSL") & "'),GCX.posn_unit,Null)) AS [BNSL], " & _
"sum(IIF(GCX.transit In(" & Range("SCEL") & "),GCX.posn_unit,Null)) AS [SCEL], " & _
"sum(IIF(GCX.TRANSIT Not In('95075','38885','00375','01285','21485'),GCX.POSN_UNIT,Null)) AS [Elsewhere] " & _
"FROM (((((Linked_Tickers AS TIC INNER JOIN sag_issuer AS ISS ON TIC.F1 = ISS.BLOOMBERG_TICKER_ID) " & _
"INNER JOIN sag_master_security_desc AS MSD ON ISS.ISSUER_ID = MSD.ISSUER_ID) INNER JOIN " & _
"sag_gc_extract_data AS GCX ON MSD.MSD_ID = GCX.MSD_ID) INNER JOIN sag_product AS PDT ON MSD.PRODUCT_ID = " & _
"PDT.PRODUCT_ID) INNER JOIN sag_msd_supp AS SUP ON MSD.MSD_ID = SUP.MSD_ID) LEFT JOIN sag_generic_table " & _
"AS GEN ON SUP.CLASS_ID = GEN.PRODUCT_ID " & _
"WHERE ((GCX.RUN_DATE)" & eq & " #" & Range("RunDate") & "#) AND ((GEN.STATUS)='A') AND " & _
"((GEN.TABLE_ID)='share_class') AND ((PDT.PRODUCT_ID) In (1) AND ((SUP.share_os) Is Not Null)) " & _
"GROUP BY GCX.run_date, ISS.issuer_name, ISS.country_res, PDT.product_type, GEN.description, SUP.share_os;"
rst.Source = connectionString
'open record and write to excel sheet
.Open
Do Until .EOF
introw = introw + 1
Sheet3.Cells(introw, 1) = .Fields("Run Date")
Sheet3.Cells(introw, 2) = .Fields("Issuer Name")
Sheet3.Cells(introw, 3) = .Fields("Country of Domicile")
Sheet3.Cells(introw, 4) = .Fields("Product Type")
Sheet3.Cells(introw, 5) = .Fields("Product Class")
Sheet3.Cells(introw, 6) = .Fields("Shares OS")
Sheet3.Cells(introw, 7) = .Fields("SBE")
Sheet3.Cells(introw, 8) = .Fields("SIL")
Sheet3.Cells(introw, 9) = .Fields("BNSL")
Sheet3.Cells(introw, 10) = .Fields("SCEL")
Sheet3.Cells(introw, 11) = .Fields("Elsewhere")
.MoveNext
Loop
.Close
End With
connection.Close
'go to summary sheet
Sheets("GEF Holdings Summary").Select