Results 1 to 10 of 10

Thread: sql results in excel

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Location
    Earth, I think!!
    Posts
    249

    sql results in excel

    The following code shows that if I run the micro command in the first sheet (sheet1) of my workbook then the results of the query they will be presented in sheet1.
    What changes should I make in the code in order to are presented the results in an other sheet e.g. Sheet2.



    Sub Refresh()
    'Declare variables
    Dim OraSession As Object
    Dim OraDatabase As Object
    Dim EmpDynaset As Object
    Dim flds() As Object
    Dim fldcount As Integer
    Dim userentry As String
    Dim dtentryFROM As Date
    Dim dtentryTO As Date
    Dim strdtfrom As String
    Dim strdtto As String


    'dtentryTO = Format(dd / mm / yyyy)
    strdtfrom = InputBox("From Date:")
    strdtto = InputBox("To Date:")
    userentry = InputBox("Please give User")

    Set OraSession = CreateObject("OracleInProcServer.XOraSession")

    Set OraDatabase = OraSession.OpenDatabase("mydb", "username/pass", 0&)

    Set EmpDynaset = OraDatabase.CreateDynaset("SELECT ROWNUM, tbl1, tbl2 FROM data WHERE date>= TO_DATE ('" + strdtfrom + "', 'DD/MM/RRRR') AND date< TO_DATE ('" + strdtto + "', 'DD/MM/RRRR') AND user = " + userentry + " ORDER BY date, ROWNUM", 0&)

    'Set objSht = objWkb.Worksheets(class)

    'Worksheets(class).
    Range("A2:C2000").Select



    'Range("A2:C2000").Select

    Selection.ClearContents


    'Declare and create an object for each column.

    'This will reduce objects references and speed

    'up your application.

    fldcount = EmpDynaset.Fields.Count

    ReDim flds(0 To fldcount - 1)

    For colnum = 0 To fldcount - 1

    Set flds(colnum) = EmpDynaset.Fields(colnum)

    Next



    'Insert Column Headings

    'For Colnum = 0 To EmpDynaset.Fields.Count - 1

    'ActiveSheet.Cells(1, Colnum + 1) = flds(Colnum).Name

    'Next


    'Display Data

    For Rownum = 2 To EmpDynaset.RecordCount + 1

    For colnum = 0 To fldcount - 1

    ActiveSheet.Cells(Rownum, colnum + 1) = flds(colnum).Value

    Next

    EmpDynaset.MoveNext

    Next


    Range("A2:A2").Select

    End Sub

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Location
    Earth, I think!!
    Posts
    249
    any help????

  3. #3
    Member
    Join Date
    Mar 2002
    Location
    Grimsby (up the Mariners!)
    Posts
    45

    Arrow Replace your ActiveSheet references with you're own objects

    You can reference any worksheet in any workbook as an object.

    e.g. Reference a named sheet that already exists...

    Code:
       Dim ws as Worksheet
       Set ws = ActiveWorkbook.Sheets("Sheet2")
    e.g. A new sheet in the current workbook...

    Code:
       Dim ws as Worksheet
       Set ws = ActiveWorkbook.Sheets.Add
       ws.Name = "New Sheet"
    e.g. A new sheet in a new workbook...
    Code:
       Dim ws as Worksheet
       Set ws = Application.Workbooks.Add.Sheets.Add

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Location
    Earth, I think!!
    Posts
    249
    i put this

    Dim XL As Excel.Application
    Dim Wkb As Excel.Workbook
    Dim ws As Worksheet
    Set ws = ActiveWorkbook.Sheets("sheet2")

    and i get data again on sheet1??

  5. #5
    Member
    Join Date
    Mar 2002
    Location
    Grimsby (up the Mariners!)
    Posts
    45

    Replace your Activesheet refs

    Did you replace your ActiveSheet references with ws??

  6. #6
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253
    How about this?

    VB Code:
    1. Private Function GenerateReport(oData As ADODB.Recordset, Name As String) As String
    2.  
    3.     On Error GoTo ERR_GenerateReport
    4.    
    5.     Dim xlApp As Object
    6.     Dim xlWb As Object
    7.     Dim xlWs As Object
    8.     Dim sFile As String
    9.     Dim sDirectory As String
    10.     Dim lCtr As Long
    11.    
    12.     '**************************************************************
    13.     '* Make the report directory if it doesn't already exist . . .
    14.     '**************************************************************
    15.     sDirectory = mReportRootDirectory & "\" & Replace(Name, Chr(32), "")
    16.     If Dir(sDirectory, vbDirectory) = vbNullString Then
    17.         MkDir sDirectory
    18.         SetAccess "Everyone", sDirectory, 0&
    19.         SetAccess "SQLExec", sDirectory, GENERIC_READ Or GENERIC_EXECUTE Or DELETE Or GENERIC_WRITE
    20.         SetAccess Environ$("USERNAME"), sDirectory, GENERIC_READ Or GENERIC_EXECUTE Or DELETE Or GENERIC_WRITE Or GENERIC_ALL
    21.         App.LogEvent "Created Directory '" & sDirectory & "'", vbLogEventTypeInformation
    22.     End If
    23.    
    24.     '*****************************************************************************************
    25.     '* Get the XL objects. These are late bound because Microsoft change versions often . . .
    26.     '*****************************************************************************************
    27.     Set xlApp = CreateObject("Excel.Application")
    28.     Set xlWb = xlApp.Workbooks.Add
    29.     Set xlWs = xlWb.Worksheets("Sheet1")
    30.  
    31.     '********************************
    32.     '* Add the column headings . . .
    33.     '********************************
    34.     For lCtr = 0 To oData.Fields.Count - 1
    35.         xlWs.Cells(1, lCtr + 1) = oData.Fields(lCtr).Name
    36.     Next
    37.    
    38.     '*****************************
    39.     '* Copy data and format . . .
    40.     '*****************************
    41.     xlWs.Cells(2, 1).CopyFromRecordset oData
    42.     xlApp.Selection.CurrentRegion.Columns.Autofit
    43.    
    44.     '********************************
    45.     '* Save the file, and quit . . .
    46.     '********************************
    47.     sDirectory = Right$(sDirectory, Len(sDirectory) - 3) 'strip of leading drive letter e.g 'c:\'
    48.     sDirectory = "\\" & Environ$("COMPUTERNAME") & "\" & sDirectory
    49.     sFile = sDirectory & "\" & Format$(Now, "yyyymmddhhnnss") & Replace(Name, Chr(32), "")
    50.    
    51.     xlWb.SaveAs sFile
    52.     xlApp.quit
    53.    
    54.     Set xlWs = Nothing
    55.     Set xlWb = Nothing
    56.     Set xlApp = Nothing
    57.    
    58.     GenerateReport = sFile
    59.    
    60.     Exit Function
    61.    
    62. ERR_GenerateReport:
    63.  
    64.     Set xlWs = Nothing
    65.     Set xlWb = Nothing
    66.     Set xlApp = Nothing
    67.    
    68.     GenerateReport = vbNullString
    69.    
    70. End Function

  7. #7
    New Member
    Join Date
    Jul 2003
    Posts
    1
    try this

    Range("sheet2!A1:C1000").select

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Location
    Earth, I think!!
    Posts
    249
    i replace the ws...
    my code now is :

    Private Sub OKclass_Click()
    Dim OraSession As Object
    Dim OraDatabase As Object
    Dim EmpDynaset As Object
    Dim flds() As Object
    Dim fldcount As Integer
    Dim userentry As String
    Dim dtentryFROM As Date
    Dim dtentryTO As Date
    Dim strdtfrom As String
    Dim strdtto As String
    'Dim XL As Excel.Application
    'Dim Wkb As Excel.Workbook
    Dim ws As Worksheet
    Set ws = ActiveWorkbook.Sheets("class")
    'dtentryTO = Format(dd / mm / yyyy)
    strdtfrom = InputBox("From Date:")
    strdtto = InputBox("To Date:")
    userentry = InputBox("Please give User_Id")

    Set OraSession = CreateObject("OracleInProcServer.XOraSession")

    Set OraDatabase = OraSession.OpenDatabase("mydb", "username/pass", 0&)

    Set EmpDynaset = OraDatabase.CreateDynaset("SELECT ROWNUM,tbl1,tbl2 Data WHERE date >= TO_DATE ('" + strdtfrom + "', 'DD/MM/RRRR') AND date < TO_DATE ('" + strdtto + "', 'DD/MM/RRRR') AND user = " + userentry + " ORDER BY date, ROWNUM", 0&)

    'Range("sheet2!A1:C1000").Select

    Range("A2:C2000").Select

    Selection.ClearContents


    'Declare and create an object for each column.

    'This will reduce objects references and speed

    'up your application.

    fldcount = EmpDynaset.Fields.Count

    ReDim flds(0 To fldcount - 1)

    For colnum = 0 To fldcount - 1

    Set flds(colnum) = EmpDynaset.Fields(colnum)

    Next



    'Insert Column Headings

    'For Colnum = 0 To EmpDynaset.Fields.Count - 1

    'ActiveSheet.Cells(1, Colnum + 1) = flds(Colnum).Name

    'Next
    'Display Data

    For Rownum = 2 To EmpDynaset.RecordCount + 1

    For colnum = 0 To fldcount - 1

    ActiveSheet.Cells(Rownum, colnum + 1) = flds(colnum).Value

    Next

    EmpDynaset.MoveNext

    Next
    Range("A2:A2").Select
    End Sub


    and the "Range("sheet2!A1:C1000").Select"
    don' work stops with en error msg!!

  9. #9
    Member
    Join Date
    Mar 2002
    Location
    Grimsby (up the Mariners!)
    Posts
    45

    Look at the post above

    alazarou,

    look at yrwyddfa's post above because it clearly shows how to do it. These are the important steps...

    This code below gets the Excel objects and creates a reference to Sheet1 called xlWs. If your code is running inside Excel you don't need the first two lines. If you want your records on sheet2 then replace "Sheet1" with "Sheet2".

    Code:
           
    '*****************************************************************************************
        '* Get the XL objects. These are late bound because Microsoft change versions often . . .
        '*****************************************************************************************
        Set xlApp = CreateObject("Excel.Application")
        Set xlWb = xlApp.Workbooks.Add
        Set xlWs = xlWb.Worksheets("Sheet1")
    The next section adds the column headings to the first row of the worksheet- I notice that you have some very similar code in your post. There is no need to copy the headings into an array before putting them on the worksheet. Notice how xlWs is at the start of the statement. Change 'oData' to the name of your dynaset object.
    Code:
        '********************************
        '* Add the column headings . . .
        '********************************
        For lCtr = 0 To oData.Fields.Count - 1
            xlWs.Cells(1, lCtr + 1) = oData.Fields(lCtr).Name
        Next
    The next section gets the data using the mucho efficient CopyFromRecordset method - using "xlWs" again. Then the Autofit method is called to set all the column widths to match the data. Change xlApp to Application if you are working in Excel VBA. You will also need to change oData again.
    Code:
        '*****************************
        '* Copy data and format . . .
        '*****************************
        xlWs.Cells(2, 1).CopyFromRecordset oData
        xlApp.Selection.CurrentRegion.Columns.Autofit
    It should be as simple as that. Does this help any??

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Location
    Earth, I think!!
    Posts
    249
    ok this was very helpful !!

    thank you all guys!!!

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