Results 1 to 3 of 3
  1. #1
    graviz is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Sep 2009
    Posts
    48

    Help To Simplify My Code

    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

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,920
    Don't think it could be much simpler, although for such a short and simple SQL statement, probably don't need to build it incrementally, one liner will work.

    MyRecordset.Open "SELECT * FROM 02_08_Overall_Latest_Week;"

    What do you mean by wanting to keep the For loop for some queries and not others? This procedure specifies a single query.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725
    Use code tags [c o d e] and [/ c o d e] (remove the spaces) to keep your formatting and help the reader follow your structure.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Report Code is not allowing return to main code
    By rcwiley in forum Programming
    Replies: 2
    Last Post: 06-16-2013, 10:31 AM
  2. Replies: 7
    Last Post: 05-28-2013, 09:11 AM
  3. Replies: 1
    Last Post: 05-04-2013, 12:19 PM
  4. Need help to simplify this process
    By shanea.kr in forum Access
    Replies: 1
    Last Post: 07-10-2012, 01:40 PM
  5. Word code in Access - How to modify my current code
    By Alexandre Cote in forum Programming
    Replies: 0
    Last Post: 11-15-2010, 08:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums