Simplify My Code (1 Viewer)

graviz

Registered User.
Local time
Today, 11:15
Joined
Aug 4, 2009
Messages
167
I've been getting better at VBA over the past couple years however I'd like to try and learn how to simplify/condense my code. The code below allows me to output a query "02_08_Overall_Latest_Week" to a specific tab and cell. The "Call Init_Paths" is just where I have my paths stored so I can use global vars Is there a better was of doing what I have been doing or is there a way I can condense my code below. I'd like to keep my "For Loop" in there as some queries I need the headings and some I do not want them. If there's a better way I'm open. Thanks!

Public Function Output_Overall()
Call Init_Paths
Dim cnn As ADODB.Connection
Dim MyRecordset As New ADODB.Recordset
Dim MySQL As String
Dim MySheetPath As String
Dim Xl As Excel.Application
Dim XlBook As Excel.Workbook
Dim XlSheet As Excel.Worksheet
Set cnn = CurrentProject.Connection
MyRecordset.ActiveConnection = cnn
MySQL = "SELECT * From "
MySQL = MySQL & "02_08_Overall_Latest_Week"
MyRecordset.Open MySQL
MySheetPath = Staging_Path
Set Xl = CreateObject("Excel.Application")
Set XlBook = GetObject(MySheetPath)
Xl.Visible = True
XlBook.Windows(1).Visible = True
Set XlSheet = XlBook.Worksheets("Overall")
For iCol = 1 To MyRecordset.Fields.Count
XlSheet.Cells(1, iCol).Value = MyRecordset.Fields(iCol - 1).Name
Next
XlSheet.Range("A2").CopyFromRecordset MyRecordset
MyRecordset.Close
Set cnn = Nothing
Set Xl = Nothing
XlBook.Save
XlBook.Close
Set XlBook = Nothing
Set XlSheet = Nothing
End Function
 

MarkK

bit cruncher
Local time
Today, 10:15
Joined
Mar 17, 2004
Messages
8,186
If you want to start discussing code in detail on here, I recommend you start with learning how to use the code tags. When you use code tags your indents are preserved and it makes your code considerably more readable. Note that understanding someone else's code is not a trivial matter even under optimum conditions.

To use code tags, highlight your code and hit the number sign, "#", or octothorpe on the toolbar in the edit window. It yields a result like . . .
Code:
Private Property Get Request() As MSXML2.XMLHTTP60
'   This is the request sent to Google
    If m_request Is Nothing Then
        Set m_request = New MSXML2.XMLHTTP60
   
        m_request.Open "GET", "http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address=" & FormattedAddress        ' & "&bounds=49,-123.3|49.5,-121"
        m_request.Send
    
        Do While m_request.readyState <> 4
            DoEvents
        Loop
    End If
    Set Request = m_request
End Property
The main problem I see though, is that you have a table called "02_08_Overall_Latest_Week" hard-coded into the routine, and even if you might want to pass that in as a parameter, it suggests that you are not storing your data in it's most raw form. "02_08" is data, not structure. That information should be data in a field, not the name of an object.
hth
 

Users who are viewing this thread

Top Bottom