Results 1 to 3 of 3

Thread: query result in variable or xla sheet

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Location
    Lucenec, Slovakia
    Posts
    154

    query result in variable or xla sheet

    Hi guys,

    I have created a simple xla which shoving me some data from our SQL server.
    Everything works fine, but I would like to set the destination from ordinary excel sheet range to somewhere else, xla sheet, variable, etc. At the moment the macro shoving me exchange rate of several currencies for a given date, but I am thinking about showing a chart for some time period (week, month, etc.).
    I would like to make the destination independent to excel sheet.

    Has anybody some idea?

    Thanks. Boris

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: query result in variable or xla sheet

    It all depends how the macro works, if you post it we can make informed suggestions.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Location
    Lucenec, Slovakia
    Posts
    154

    Re: query result in variable or xla sheet

    Quote Originally Posted by si_the_geek
    It all depends how the macro works, if you post it we can make informed suggestions.
    Here is:
    VB Code:
    1. Public Sub MonthView1_DateClick(ByVal dateclicked As Date)
    2. If ActiveWorkbook Is Nothing Then
    3. MsgBox "Open a workbook first!"
    4. Else
    5. UserForm1.Spreadsheet1.Range("A1:C5").Locked = False
    6. UserForm1.Spreadsheet1.Sheets(1).Protection.Enabled = False
    7. UserForm1.MonthView1.MultiSelect = False
    8. UserForm1.MonthView1.SetFocus
    9. Range("M60005:N60009").Clear
    10. Dim a As Range
    11.  
    12. ' error handling routine
    13. If err <> 0 Then
    14.         MsgBox "Error occured!", vbInformation
    15. End If
    16.  
    17.     With ActiveSheet.QueryTables.Add(Connection:= _
    18.         "ODBC;DRIVER={INTERSOLV 3.01 32-BIT INFORMIX};DB=thor@ifxserv;HOST=thorma;UID=bolcskei;PWD=mustang" _
    19.         , Destination:=Range("M60005"))
    20.         '.CommandText = Array("SELECT crate.crate_currency, crate.crate_rate FROM maxmast.crate crate WHERE (((crate.crate_currency='CZK') AND (crate.crate_lud='" & DateClicked & "')) OR ((crate.crate_currency='EUR') AND (crate.crate_lud='" & DateClicked & "')) OR ((crate.crate_currency='HUF') AND (crate.crate_lud='" & DateClicked & "')) OR ((crate.crate_currency='USD') AND (crate.crate_lud='" & DateClicked & "'))) ORDER BY crate.crate_currency")
    21.         .CommandText = Array( _
    22.         "SELECT crate.crate_currency, crate.crate_rate FROM maxmast.crate crate WHERE (crate.crate_currency='CZK') AND (crate.crate_lud='" & dateclicked & "') OR (crate.crate_currency='EUR') AND (crate.crate_lud=" _
    23.         , _
    24.         "'" & dateclicked & "') OR (crate.crate_currency='HUF') AND (crate.crate_lud='" & dateclicked & "') OR (crate.crate_currency='USD') AND (crate.crate_lud='" & dateclicked & "') ORDER BY crate.crate_currency" _
    25.         )
    26.         .Name = "Query from MAX"
    27.         .FieldNames = True
    28.         .RowNumbers = False
    29.         .FillAdjacentFormulas = False
    30.         .PreserveFormatting = False
    31.         .RefreshOnFileOpen = False
    32.         .BackgroundQuery = True
    33.         .RefreshStyle = xlInsertDeleteCells
    34.         .SavePassword = True
    35.         .SaveData = True
    36.         .AdjustColumnWidth = True
    37.         .RefreshPeriod = 0
    38.         .PreserveColumnInfo = True
    39.         .Refresh BackgroundQuery:=False
    40.     End With
    41.    
    42. ' error handling routine
    43. If err <> 0 Then
    44.         MsgBox "Error occured", vbInformation
    45. End If
    46.  
    47. If UserForm1.OptionButton1.Value = True Then
    48. If Range("N60006").Value = 0 Then
    49. UserForm1.Spreadsheet1.Range("B2").Value = "N/A"
    50. Else: UserForm1.Spreadsheet1.Range("B2").Value = 1 / Range("N60006").Value
    51. End If
    52. If Range("N60007").Value = 0 Then
    53. UserForm1.Spreadsheet1.Range("B3").Value = "N/A"
    54. Else: UserForm1.Spreadsheet1.Range("B3").Value = 1 / Range("N60007").Value
    55. End If
    56. If Range("N60008").Value = 0 Then
    57. UserForm1.Spreadsheet1.Range("B4").Value = "N/A"
    58. Else: UserForm1.Spreadsheet1.Range("B4").Value = 1 / Range("N60008").Value
    59. End If
    60. If Range("N60009").Value = 0 Then
    61. UserForm1.Spreadsheet1.Range("B5").Value = "N/A"
    62. Else: UserForm1.Spreadsheet1.Range("B5").Value = 1 / Range("N60009").Value
    63. End If
    64. UserForm1.Spreadsheet1.Range("B1").Value = dateclicked
    65.  
    66. Else
    67. If Range("N60006").Value = 0 Then
    68. UserForm1.Spreadsheet1.Range("C2").Value = "N/A"
    69. Else: UserForm1.Spreadsheet1.Range("C2").Value = 1 / Range("N60006").Value
    70. End If
    71. If Range("N60007").Value = 0 Then
    72. UserForm1.Spreadsheet1.Range("C3").Value = "N/A"
    73. Else: UserForm1.Spreadsheet1.Range("C3").Value = 1 / Range("N60007").Value
    74. End If
    75. If Range("N60008").Value = 0 Then
    76. UserForm1.Spreadsheet1.Range("C4").Value = "N/A"
    77. Else: UserForm1.Spreadsheet1.Range("C4").Value = 1 / Range("N60008").Value
    78. End If
    79. If Range("N60009").Value = 0 Then
    80. UserForm1.Spreadsheet1.Range("C5").Value = "N/A"
    81. Else: UserForm1.Spreadsheet1.Range("C5").Value = 1 / Range("N60009").Value
    82. End If
    83. UserForm1.Spreadsheet1.Range("C1").Value = dateclicked
    84. End If
    85.  
    86. Range("M60005:N60009").Clear
    87. UserForm1.Spreadsheet1.Range("A1:B5").Locked = True
    88. UserForm1.Spreadsheet1.Sheets(1).Protection.Enabled = True
    89. End If
    90. End Sub

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