PDA

Click to See Complete Forum and Search --> : Can somebody please help me with my code?


bibig1978
Dec 13th, 2004, 01:11 PM
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

RobDog888
Dec 13th, 2004, 10:13 PM
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.
ActiveWorkbook.Sheets("Usage").Range("A:A").EntireColumn.Insert

bibig1978
Dec 14th, 2004, 09:39 AM
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

RobDog888
Dec 14th, 2004, 08:19 PM
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.
'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