Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2017
    Posts
    2

    Post Access VBA code efficiency required....

    dear mates,

    am new to MS ACCESS .
    can any one help me on the below code without any performance issues.



    Private Sub cmdchemicalcompareexport_Click()
    Dim saveloc As String
    strWorksheetPath As String
    xl As Object
    wb As Object
    Dim exportsheet As Object
    Dim index As Long
    Dim db As Database
    Set db = CurrentDb
    Dim rs As Recordset
    Dim rng As Object


    Set xl = CreateObject("Excel.Application")
    Set wb = xl.Workbooks.Add
    Set exportsheet = wb.Worksheets(1)
    exportsheet.Name = "Chemical Compare"
    xl.Application.Visible = True
    Set rs = db.OpenRecordset(Me.RecordSource)



    exportsheet.Cells(3, 1).Value = "Trade Name"
    exportsheet.Cells(4, 1).Value = "Supplier"
    exportsheet.Cells(5, 1).Value = "Category"
    exportsheet.Cells(6, 1).Value = "Physical Appearance"
    exportsheet.Cells(7, 1).Value = "Active Ingredient"
    exportsheet.Cells(8, 1).Value = "Regional Availability"
    exportsheet.Cells(9, 1).Value = "EPA#"
    exportsheet.Cells(10, 1).Value = "Comment"
    exportsheet.Range("A3:A10").Font.Bold = True



    Dim rownum As Long, colnum As Long
    Dim i As Long
    i = 0
    rownum = 3
    colnum = 2
    rs.MoveFirst


    For index = 1 To rs.Fields.Count
    If rs.Fields(i).Name <> "SDS" And rs.Fields(i).Name <> "CID" Then
    Do While Not rs.EOF
    exportsheet.Cells(rownum, colnum).Value = rs.Fields(i).Value
    rs.MoveNext
    colnum = colnum + 1


    Loop
    rownum = rownum + 1
    colnum = 2


    End If
    i = i + 1
    rs.MoveFirst
    Next
    Set rng = exportsheet.UsedRange
    rng.Borders.LineStyle = xlContinuous
    exportsheet.Cells.EntireColumn.AutoFit
    exportsheet.Cells.EntireColumn.HorizontalAlignment = xlLeft

    'build report heading
    exportsheet.Range("A1", "F1").Merge
    exportsheet.Range("A2", "F2").Merge
    exportsheet.Range("A1").HorizontalAlignment = xlCenter
    exportsheet.Range("A2").HorizontalAlignment = xlCenter
    exportsheet.Range("A1").Cells.Font.Bold = True
    exportsheet.Range("A2").Cells.Font.Bold = True
    exportsheet.Range("A1").Cells.Font.Name = "Cambria"
    exportsheet.Range("A2").Cells.Font.Name = "Cambria"
    exportsheet.Range("A1").Cells.Font.Size = 14
    exportsheet.Range("A2").Cells.Font.Size = 12
    exportsheet.Range("A1").Value = "Report Chemical Comparision "
    exportsheet.Range("A2").Value = Date & " " & Time


    End Sub
    Attached Files Attached Files

  2. #2
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    I would use the inbuilt access export function (DoCmd.TransferSpreadsheet) then apply your formatting.
    It will be much quicker than looping through the recordset.

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

Similar Threads

  1. Efficiency in code run time
    By rpeare in forum Programming
    Replies: 1
    Last Post: 04-12-2017, 12:01 PM
  2. Replies: 3
    Last Post: 03-08-2014, 06:01 PM
  3. Replies: 1
    Last Post: 08-23-2013, 10:15 AM
  4. vba code required for report
    By princeofdumph in forum Programming
    Replies: 1
    Last Post: 12-09-2011, 08:49 AM
  5. Lotsa code; need help with efficiency!!
    By usmcgrunt in forum Forms
    Replies: 5
    Last Post: 08-26-2011, 07:49 AM

Tags for this Thread

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