Results 1 to 4 of 4

Thread: Can somebody please help me with my code?

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Posts
    11

    Can somebody please help me with my code?

    I am creating an excel macro on the number of analysts (by group) that has logged into our database for the current day. However when I open up my excel sheet, I would like to see the macro keep the existing data (previous day) and display the data of the current date in the next column. My output consists of group and date (number of analysts from each group logging into the database).
    Below is how the output looks. Right now i can only get my macro to look at the current day.
    Group November 30 (current date)
    PC 8
    MD 17
    SYS 1

    I have not been able to figure out how to keep the existing data and display the next day's data onto the next column such as the output below:

    Group November 30 Dec 1st Dec 2nd Dec 3rd
    PC 8 12 0 15
    MD 17 5 12 2
    SYS 1 9 14 3

    Can somebody please help me? I believe that I may need to run a loop through my query set. I'm fairly new to VB coding and welcome any help I can get. Thanks.
    This is the code i have so far:


    Sub UsageReport()
    '
    ' NewReport Macro
    ' Macro recorded 8/20/2004
    '

    Dim conn As ADODB.Connection
    Dim ConnectionString As String
    Set conn = New ADODB.Connection

    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset
    Dim rs1 As ADODB.Recordset
    Set rs1 = New ADODB.Recordset
    Dim SqlString As String
    Dim xl As Excel.Application
    Dim xlw As Excel.Workbook
    Dim xls As Excel.Worksheets
    Dim intRows As Integer



    conn.CursorLocation = adUseClient
    ConnectionString = "DRIVER=ODBC;Data Source=csathenacms;NA=172.27.101.179, 5000;Database=CMS;UID=cmsreports;PWD=reports"
    conn.Open ConnectionString 'Open connection

    'test to see if connection works

    If conn.State = adStateOpen Then
    MsgBox "You are now connected to the database"
    ElseIf conn.State = adStateClose Then
    MsgBox "You are not connected to the database"
    End If


    'Open recordset query
    rs.Open "SELECT A.GROUP2, Count(B.USERID) as Dte" & Chr(13) & "" & Chr(10) & _
    "FROM cms.dbo.sys_user A, CMS.DBO.USAGELOG B" & Chr(13) & "" & Chr(10) & _
    "WHERE B.USERID = A.USER_ID AND ((B.LOGIN_DT=Current_date()) AND (B.ISACTIVE=1))" & Chr(13) & "" & Chr(10) & _
    "GROUP BY A.GROUP2" & Chr(13) & "" & Chr(10) & _
    "ORDER BY A.GROUP2", conn, adOpenStatic, adLockOptimistic


    Set xl = Excel.Application

    Range("A2").Select
    ActiveCell.FormulaR1C1 = _
    "USAGE LOG REPORT" 'Title of Report
    With ActiveCell.Characters(Start:=1, Length:=53).Font
    .Name = "Comic Sans MS"
    .Size = 13
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .ColorIndex = xlAutomatic
    End With
    'Range("A2").Select
    With Selection
    .HorizontalAlignment = xlGeneral
    .VerticalAlignment = xlBottom
    .WrapText = True
    .Orientation = 0
    .AddIndent = False
    .ShrinkToFit = False
    .MergeCells = False
    End With


    ' MsgBox "Open Workbook"
    xl.Worksheets("Usage").Select 'set parameters for each column A-J
    Columns("A:A").Select
    Selection.ColumnWidth = 25#
    Selection.WrapText = True
    Range("A4").Select 'name of column
    ActiveCell.FormulaR1C1 = "Group"


    'Open existing worksheet
    'MsgBox "Open Worksheet"
    With xls
    xl.Range("A5").CopyFromRecordset rs



    Set xlw = Nothing
    Set xl = Nothing


    End With

    conn.Close
    Set conn = Nothing



    End Sub

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Can somebody please help me with my code?

    Instead of trying to copy data over to the next columns, insert a new column
    at the begining and shift the columns to the right by one. If you do this
    before you insert the new days data, you will be making room for it and
    preserving previous data.

    Code:
    ActiveWorkbook.Sheets("Usage").Range("A:A").EntireColumn.Insert
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Posts
    11

    Re: Can somebody please help me with my code?

    Thanks for you for your help RobDog, however that's not really quite what i'm looking for . When i placed that code in my macro, its shifted my entire output from the query one column to the right.
    I'm trying to maintain a history of analyst logging into our database each day
    so right now my code reflects data from the current day:

    Group Dec 14
    PC 8
    MD 17
    SYS 1

    so for the date of december 14th, the above number of analysts logged in. Now if i want to open up my excel sheet the next day, I would like to see Dec 14th's data along with December 15th's data. I would appreciate any help....i've been trying to figure out how to create a loop against my query but i'm soo lost

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Can somebody please help me with my code?

    Ok, I went through the code again and its the query that is only returning the
    current days data. We need to change it so the rs will contain the current
    days data plus what ever number of days in the past you need.
    Ex. does current date and the five previous days.

    Code:
    'Open recordset query
    Dim sSQL As String
    
    sSQL = "SELECT A.GROUP2, Count(B.USERID) as Dte"
    sSQL = sSQL & "FROM cms.dbo.sys_user A, CMS.DBO.USAGELOG B"
    sSQL = sSQL & "WHERE B.USERID = A.USER_ID AND "
    sSQL = sSQL & "((B.LOGIN_DT<=Current_date())"
    sSQL = sSQL & " AND (B.LOGIN_DT>=DATEADD('d', -5, Current_date())))"
    sSQL = sSQL & " AND (B.ISACTIVE=1)"
    sSQL = sSQL & "GROUP BY A.GROUP2"
    sSQL = sSQL & "ORDER BY A.GROUP2"
    
    rs.Open sSQL, conn, adOpenStatic, adLockOptimistic
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width