how to keep same connection for a new snowflake worksheet - snowflake-cloud-data-platform

In snowfake how can you open a new worksheet with the same connectons for role, datawarehouse and schema? Everytime I open a new worksheet it removes all my settings and have to select them again

you need to set the default_warehouse and default_namespace user parameters to the values you want to be used when you create a new sheet.
These defaults will be used for every new sheet. As far as I'm aware, there is no way to automatically create a sheet with the same context as a previous sheet - in case that is what you are trying to achieve.

Related

VBA clear just pivot table cache, but leaving pivot table structure

How can I clear pivot table cache with VBA, but not destroy pivot table structure? My pivot table is connected to external data source. The SQL source determines which user should see which portion of the data. The source populates the table as the table is refreshed. I want to save the Excel file and distribute it with clean pivot table (no data inside).
As a result I want to get exactly this:
I have experimented around this code with no success. There is no such thing like PivotCaches.Clear in VBA.
Sub PT_cache_clear()
For Each pc In ActiveWorkbook.PivotCaches
pc.Clear
Next pc
End Sub
The only good solution I found is to refresh the table with a user which has access to SQL server source but is not allowed to see any single record of the data.
The idea:
ActiveSheet.PivotTables("PivotTable1").SaveData = False
seems not to lead to desired results.
The way I do it is to refresh with a query that will return the table structure but with 0 records. So if selecting from a view something like:
select top 0 *
from vw_MyPivotData
If using a stored procedure, you can send a parameter that ensures that no records will be returned such as a filter that you know doesn't exist in the data or a special parameter devised for the purpose of returning no records.
You cannot just clear the PivotCache without affecting the pivot table, they are inexorably linked. You can trick the PivotCache into loading and empty result set with the same structure/schema. Use the PivotTable.ChangeConnection function to switch the connection before closing the document.
If you have two external connections defined in your excel file. One that returns the correct data and another that returns the same structure but no rows. You can switch the connection to the no rows version and flush the cache that way. If there are any differences between the structure/schema of the connection resultset then excel will throw an error message.
Change the connection on demand, just before you distribute your file.
Sub PT_cache_clear()
'change connection'
Dim con as vartype
Set con = ActiveWorkbook.Connections("MyNoResultConnection")
Worksheets(1).PivotTables(1).ChangeConnection (con)
'refresh the pivot table cache'
Worksheets(1).PivotTables(1).PivotCache.Refresh
'clear the cache of any orphaned items'
Dim pc As PivotCache
Dim ws As Worksheet
With ActiveWorkbook
For Each pc In .PivotCaches
pc.MissingItemsLimit = xlMissingItemsNone
Next pc
End With
End Sub
Change to the good connection every time the sheet opens
Private Sub Workbook_Open()
Dim con as vartype
Set con = ActiveWorkbook.Connections("MyGoodResultConnection")
Worksheets(1).PivotTables(1).ChangeConnection (con)
Worksheets(1).PivotTables(1).PivotCache.Refresh
End Sub
You should only need to setup the MyNoResultConnection connection on your local PC since users will not be calling PT_cache_clear().
Update:
Instead of always changing the pivot table connection every time you could set it conditionally by getting the current connection name from the PivotCache.WorkbookConnection property and comparing the names.
Worksheets(1).PivotTables(1).PivotCache.WorkbookConnection
Note:
It is not possible to just flush or empty the pivot cache. You can call a refresh method or set it to refresh automatically when the file opens. You can clear the cache of orphaned items.
PivotTable.ChangeConnection and PivotCache.WorkbookConnection can only be used with external data sources.
Setting PivotTable.SaveData to false will not clear the pivot cache. This stops the data source from being saved it does not affect the PivotCache. The PivotCache lies between the data source and pivot table. When using external data sources this property will have no effect and for OLAP data sources it is always false.
See posts here and here for getting the current state of your PivotCache.
Alternative ways to implement this:
Create the connection object and pivot table with a VBA macro when the workbook opens. Delete the connection, pivot table before the workbook closes. The cache will then be automatically deleted when the workbook is saved. This approach can be difficult depending on how your external datasource is setup. It may require your to store username and passwords inside your VBA code.

Create Graphs automatically out of the excel generated via SSIS?

I have the following requirement,
A SQL Server 2008 scheduled job that runs at 9 am in the morning. This job should send a spreadsheet with data in a workbook(workbook1) and the chart attached in the next workbook(workbook2). The data must have the values from a SQL table. The chart must reflect the values present in the workbook1. This sheet should be mailed across to n number of users. The n number of users are not even aware of the sql server and dont know anything about the username and password of the server. They must just have the spreadsheet with the two workbooks.
I have decided to do the below
1) Create a stored procedure that formats the data into a table 2) Invoke a SSIS , to copy the data into the workbook1 of excel 3) Create the Graph in the workbook2 of the same excel
The point 1 and 2, I have completed already.
After activity 1 and 2, I will have something like below in the excel
Workbook1:
Date | Column A | Column B | Column C |
10-7-14 | 0983883 | 09433344 | 4443333 |
11-7-14 | 0986444 | 06875544 | 4689073 |
I am not really able to do the activity 3. Activity 3 must have take in the values from the above table and create graphs in workbook2
I know it can be done with SSRS, but I would like to know other ways as we have only SSIS with us and SSRS is totally out of scope. For some reasons SSRS is restricted to use in our systems.
I have tried various ways to do this. (created a dynamic graph in workbook2 of the excel template for SSIS and let the values transferred to workbook1 renders the graph but it does not work sadly.). I am not able to create dynamic graphs with the empty excel templates that I feed to SSIS.
Excel is the problem here, it does not allows any pre-defined graphs in it. It always expects a value to create a graphs.
I can also go for a VBA macro option,(like creating a button and let the user click the button to generate graphs) but I am not really sure if it is a feasible one?
Excel experts please help !
I might like sound like an amateur but please pardon me as I am new to SSIS and none of my colleagues have an idea on how to do it and I could not get anything with the research I have done, which is a bit frustrating though.
As I don't know the specifics of your workbook structure you might need to adapt the following vba code (as the sub name suggests this should be placed in the Open event of the workbook):
Private Sub Workbook_Open()
Dim wb As Workbook
Dim ws As Worksheet
Dim ws2 As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim LastColumnLetter As String
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet1")
Set ws2 = wb.Worksheets("Sheet2")
Application.ScreenUpdating = False
LastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
LastColumn = ws.Cells(2, Columns.Count).End(xlToLeft).Column
LastColumnLetter = Chr(LastColumn + 64)
ws2.Activate
ws2.ChartObjects("Chart 1").Activate
ActiveChart.SetSourceData Source:=ws.Range("A1:" & LastColumnLetter & LastRow)
ws2.Range("A1").Select
Application.ScreenUpdating = True
End Sub
This code executes when the workbook is opened and first tries to find the range your data table is occupying and then updates the Data Source for the Chart. I have assumed that your data are in Sheet1 and start at cell A1 while your chart (Chart 1) is in Sheet2.
I do something similar for work but am using excel as a front end to SSAS but the concept is similar for sql server.
It sounds like you would find a pivot chart in excel useful. This is due to the fact that excels dynamically renders the chart based on the results of the pivot table.
Note that the pivot table allows for filtering so this means that the user can filter out parts of the data they do not need. Or if the user wants to alter how the chart looks, e.g. sales by month instead of sales by quarter it is extremely easy for them to do. So basically the presentaton of the results are being done dynamically in excel.
To create one based off external data you do the following:
Data --> from other sources
It is possible for excel to query sql server, though you probably want to limit the result set to a small read-only table for reporting.
Note that the data source information is saved into the file, with excel being able to use both user name and password hard coded in or the window's domain logins. Ideally you want to use the domain login over a general username and password
So the solution becomes more like the following:
SP or SSIS to populate a table for excel to query. This means excel is just writing simple selects and group bys
Using Data --> from other sources --> from SQL server
Create the stock pivot table, which will handle the presentation of the data
Give file to users. The file would have the defined datasource in it
They then just hit refresh which will requery the server for the results.
User is able to drag and drop fields, which updates the pivot table and redraws the excel chart
The only downside being that the user can only see a copy of the current dataset.

How do I connect to an unbound table on SQL Server from VS/VB?

I created a form in Visual Studio 2012 binding table 1 to a few of the fields in order to force the user to use specific data in both a textbox and a combobox. They select what they need out of those fields, and fill in a few others, and then click save, which will take the data to table 2. While I've created a dataset, bindingsource, tableadapter, and tableadaptermanager for table 2, it does not save to the table if I use...
Me.Validate()
Me.table2BindingSource.EndEdit()
Me.table2TableAdapter.Update(table2DataSet)
...like you can if you use the bound source that's autofilling the form. How do I wire the save to hit the proper table? Do I have to go the long way and open up a connection creating the sql statement, executing that business, etc.?
So I did have to code the connection sequence into the button click. I've included the code below for posterity and comment.
Using connection As New SqlClient.SqlConnection(My.Settings.MyConnectionString)
Using Command As New SqlClient.SqlCommand("INSERT INTO table2 (stuff1, stuff2, stuff3) VALUES (#stuff1, #stuff2, #stuff3)", connection)
Command.Parameters.AddWithValue("#stuff1", stuff1TextBox.Text)
Command.Parameters.AddWithValue("#stuff2r", stuff2ComboBox.Text)
Command.Parameters.AddWithValue("#stuff3", stuff3TextBox.Text)
connection.Open()
Command.ExecuteNonQuery()
Dim rowsAffected As Integer = Command.ExecuteNonQuery()
Console.WriteLine("RowsAffected: {0}", rowsAffected)
End Using
It worked well enough for me to be comfortable and submit it for user testing. Please feel free to pull it apart.

Excel communicate with SQL Server to identify user.

Excel queries SQL Server. Trying to query using current user name. I have an Excel file that queries a view in SQL Server. The view is set to return the results based on the user querying the view.
I setup a file on a network server for others to access, hoping that when they opened the file, it would show only the information relating to them.
But the information still only refers to myself. I want it to show information the user using the file, as if they were querying the view directly.
Here is the sample query for the view:
Select * from dbo.WorkEstimate where Estimator = SUSER_NAME()
But the Excel sheet only returns the information relating to myself. I think it may have something to do with the connectstring that always specifies my workstation name. Is there a way to change that to reflect the individual (or their workstation) that is using the file?
The SUSER_NAME() (transact-sql) in the SQL statment refers to the current user within the context of the currently executing SQL statement. In your case, it is the userID you specified in your connection string. The sql server uses that user specified in your connectionstring to run that statement and thus your SUSER_NAME() reflect that user.
You will need to specify the user that the Excel is running under (Windows environment) with the following Excel functions:
Application.Username is the name of the User set in Excel Tools>Options (under the User Name box)
Environ("Username") is the name you registered for Windows see Control Panel >System
So, your SQL statement will have have to be something like:
vsSQL = "SELECT * FROM dbo.WorkEstimate WHERE (Estimator = '" & Environ("Username") & "');"
Hope this helps.
You don't need to have Excel communicate with SQL Server to figure that out. Excel needs to communicate with the user's machine.
Use this code to discover all of the environment variables that pertain to the user logged in to the machine. Look through the results to figure out what you need to pass in to your query.
Add a module to your Excel file and paste in the following:
Sub ListAllEnvironmentVariables()
Dim ws As Worksheet
Dim rng As Range
Set ws = Sheets("Sheet1")
Set rng = ws.Range("A1")
Dim i As Integer
i = 1
Do Until Environ$(i) = ""
rng.Offset(i - 1).Value = Environ(i)
i = i + 1
Loop
End Sub
I'm using Sheet1 in my example, but you can change that and set ws to any blank sheet in your workbook to see how it works. Obviously don't use Sheet1 if you have data on the sheet.

Lookup Filter dropdown incomplete in programmatically altered SQL backend Access database

In this thread a user had problems with the lookup filter being missing in an acess database that had a SQL server backend. The problem was easily solved simply by checking on an option in the current database settings that allowed ODBC fields to also provide lookup filter dropdowns.
For those confused, the lookup filter is the excel like function in a datasheet view that allows you to click on the drop down of the field name and select individual values from that field for filters by a checkbox.
I, however, have a slightly different problem. The checkbox to allow ODBC field filter lookups is active in the settings, so that's not an issue. If I have a form that pulls data from a query, the lookup filters work fine, and are pre-populated with values in that field for filter selection. If that record source is changed in VBA, however, say for example, a SQL statement that exactly matches that query, the lookup filter no longer works. I tried creating a recordset and attaching it to the same form, creating a SQL statement and attaching it to the record source, and opening the form with arguments which are then used within the form's on load event to change the record source, all with the same result of no lookup filter.
Am I overlooking something?
After HansUp replied, I floundered about trying to figure out what he meant and ran into it rather accidently.
Since the form is opened by another form where the SQL statement is pregenerated, I simply assigned the form's underlying query to a QueryDef object and applied the pregenerated SQL statement to the object's SQL property. When the form was subsequently opened, the new SQL statement was used and all the lookup filters worked properly. I was quite pleased :D
Here's a quick run down of the code:
Dim db as Database
Dim qDef as QueryDef
Dim strSQL as String
'generate SQL statement and assign it to strSQL here'
Set qDef = db.QueryDefs("qryMyQuery")
qDef.SQL = strSQL
DoCmd.OpenForm "frmMyForm" 'frmMyForm is based of qryMyQuery'
Thanks, HansUp!

Resources