SSIS set result set from data flow to variable - sql-server-2008

Before I give myself some sort of stress related heart attack.
Would anyone know how to complete the seemingly simply task of setting the result set of one data flow task (the result will be either a 0 or 1) and assigning that value to a variable.
I've created the variable ok.
The result set comes from an XML file with multiple elements. The flag (0 or 1) is the result from one of those elements, so I also need to know how to get the result set to be just that flag.
If anyone could help I would really really appreciate it.
Update : I eventually read the result (0,1) back to SQL Server into a flag table. Then used a Execute SQL script to read it back from SQL Server and to a variable. Not sure if this is the best way to have done it but it seems to have done the trick.

You can use a Data Flow Script component to transfer a data flow column value to an SSIS variable. However, you must follow certain rules when working with the Data Flow Script component and SSIS variables.
SSIS doesn't allow you to assign values to SSIS variables in the script procedure that processes rows. But there are pre- and post-execute procedures where you can handle the assignment.
In your Script component, add the SSIS variable to the ReadWriteVariables property. Edit the script and declare a variable in the ScriptMain class. Use the PreExecute procedure to initialize the variable. Use the ProcessInputRow procedure to assign the input -buffer column value to the script variable. And, use the PostExecute task to assign the value from the script variable to the SSIS variable.
Here's an example VB script component. It has an SSIS variable (MyOutVariable) that will get the output of the script variable (MyVar). The MyVar variable gets it's value from the MyNumber column in the data flow.
Public Class ScriptMain
Inherits UserComponent
Dim MyVar As Integer
Public Overrides Sub PreExecute()
MyBase.PreExecute()
'initialize variable local to data flow
MyVar = 0
End Sub
Public Overrides Sub PostExecute()
MyBase.PostExecute()
' output variable value to SSIS variable
Me.Variables.MyOutVariable = MyVar
End Sub
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
' logic to get value
MyVar = Row.MyNumber
End Sub
End Class

Related

Error Trying to run Procedure from Another Form

I'm trying to call a Sub from another Form, but I get:
Error 2465: Application defined error or object defined error
I already know that to call a procedure from another form I can use the syntax:
Form(Name).Procedure
But, in this case I want to refer to this form from a variable (Type Form).
Code from form Parametro_Cadastro:
Dim formCaller As Form
Set formCaller = Application.Forms("Cadastro_Aluno").Form
If (Not formCaller Is Nothing) Then
'
formCaller.SetDadosParametroCadastro Me.txtKey.Value, dadoAlterado
Set formCaller = Nothing
End If
Code from form Cadastro_Aluno:
Sub SetDadosParametroCadastro(ByVal KeyValue As String, ByVal DataValue As String)
'
'...
End Sub
The code in Parametro_Cadastro gives Error 2465 on the line:
formCaller.SetDadosParametroCadastro Me.txtKey.Value, dadoAlterado`
...even before the Sub SetDadosParametroCadastroin the form Cadastro_Alunos to be called.
In the Immediate Window, the variable formCaller is already signed as Form\Form_Cadastro_Alunos as expected.
What can I do in this case?
EDIT
The only way it worked was to assign the variable formCaller to Access.Forms(formName) and not through Application.Forms(formName) or simply Forms(formName), so the code now is like below:
Code from form Parametro_Cadastro:
Dim formCaller As Form
Set formCaller = Access.Forms("Cadastro_Aluno")
If (Not formCaller Is Nothing) Then
'
formCaller.SetDadosParametroCadastro Me.txtKey.Value, dadoAlterado
Set formCaller = Nothing
End If
When I check the variable formCaller in the Imediate Window, I also get (as before) Forms\Form_Cadastro_Alunos.
The interesting thing is this only change make the code work as expected. Maybe the Forms member to be called is from Access Object instead of Applications?
Although you can call a procedure from a form, it's generally avoided and is considered bad practice.
Any Sub or Function that need to be shared should be placed in a separate, shared module.
Some excellent advice from Stewart # Bytes.com:
Form code modules are essentially 'private' in scope. Functions and subroutines defined in them are normally unavailable outside of the form concerned - although it is actually possible to call the functions and subroutines defined as Public in scope within a form code module from outside of that module.
Use of the keyword Public is not just a matter of suddenly adding one as a change of style. It reflects that you wish the subroutine, function or variable to be available outside of the scope of the code module concerned...
The code modules which can be created or opened from the Modules tab are the usual locations of public subroutines and functions. Named modules provide a means of grouping tasks by logical function, and if used consistently can be an aid to maintainability.
I use many several named modules in my applications, separating custom linked table maintenance from username parsing, e-mail handling and so on. I also include a "General" module which contains general bespoke functions that I find useful and re-use across multiple databases (for example, to return the current financial year or the current quarter within it).
All subroutines and functions defined using the Public keyword in the publicly-accessible code modules are available outside of the module concerned, whereas those defined as Private cannot be used outside of the scope of the code module in which they reside.
"If you must..."
If, for some reason, you must call a public sub from a form, the syntax is:
Call Forms!FormName.SubName
Note that the sub being called must have the Public keyword specified.
More Information:
MSDN : Understanding Scope and Visibility
Bytes.com : How and where to use Private or Public sub or function?

How to loop through full result set using foreach container in SSIS

I encounter an issue when trying to use SSIS to retrieve a full result set from excel file.
I got two variables
Here is the dashboard in control flow
#CriteriaResult returns the ADO Recordset
#CriteriaID is the variable I try to loop through under the object I just returned
I encounter the error:The type of the value being assigned to variable "User::CriteriaID" differs from the current variable type. Variables may not change type during execution. Variable types are strict, except for variables of type Object.
My data is a column: 1,2,3A,3B,3C...
Online reference told me to use script task to change the convert object(#CriteriaResult) so I can use the data inside of it.
My VBA code is:
Public Sub Main()
Dim dt As Data.DataTable
Dim ds As Data.DataSet = CType(Dts.Variables("User::CriteriaID").Value, DataSet)
dt = ds.Tables(0)
Dts.TaskResult = ScriptResults.Success
End Sub
May I know how should I revise my code to make it works?
You can try this approach to iterate with an ADO enumerator:
1. Import to recordset with Data Flow Task
2. Iterate in ForEach with ADO Enumarator to recordset object variable
3. Each iteration saves record value to a variable
This is the package screenshot

MS Access Vb How to build a text string to reference a dynamic column in and Vb SQL query and then execute that text string using the Eval function

In the following example where POOPTrst is a a DAO recordset (systems object) referencing the Vb SQL query or statement.  this code returns "Run-time error 2482" and/or "Microsoft Access cannot find the name 'POOPTrst' you entered in the expression"  (note:  the POOPTVal variable holds a date).
POOPTWkDmd = Eval("POOPTrst" & "!" & POOPTVal)
In the following example where POOPTrst is a a DAO recordset referencing the Vb SQL query.  this code returns "Run-time error 3256" and/or "Item not found in this collection"  (note: with or without parenthesis around the variable POOPTVal)
POOPTWkDmd(POOPTCounterInt) = POOPTrst!Eval(POOPTVal)
If I remove the Eval function and the POOPT date variable and type literal characters into the code I get the proper/expected return value and/or response (note:  the problem is that 1/5/2009 is a dynamic value that is calculated at run time
POOPTWkDmd(POOPTCounterInt) = POOPTrst![1/5/2009]   
Use this syntax: rs("Fieldname") instead of rs!Fieldname
POOPTVal = "1/5/2009"
POOPTWkDmd = POOPTrst(POOPTVal)
for more explanation see this answer: https://stackoverflow.com/a/34969410/3820271
One issue is that Eval() doesn't know anything about VBA variables or objects such as recordsets. If you want to use those, build a string containing their values and give Eval() that string.
However, I'm not sure Eval() is what you should use here. It seems you want to reference the value of a field in your POOPTrst recordset, with a variable to hold that field name. If that is correct, use the variable with the recordset's Fields collection: POOPTrst.Fields(POOPTVal).Value

SSIS - attempting to assign a variable in postexecute not working?

I am building an SSIS package consisting of some dataflow tasks that contain script components. In one of these script components I'm trying to assign a value to a read/write variable in the post execute sub as follows:
Public Overrides Sub PostExecute()
Me.ReadWriteVariables("User::pEndDate").Value() = proEndDate
MyBase.PostExecute()
End Sub
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
...
Try
...
proEndDate = Row.EndDate
MessageBox.Show("Assigning the project end date from the row: " & proEndDate.ToString)
proEndDate has the correct value in it per the message box, but the variable is never being updated. Does anyone know why?
"The variable is never being updated". I take it this means when you look at the Variable's window while the package is running, the value never changes from its original.
This is correct and the value will never change but your code is also correct.
The Variables window shows the Design-time value of all of your variables. The Run-time values are accessible through the Debug window. In your Local's window, you will see the current value. I wrote a more detailed answer over here
Why doesn't the Script Task code assign any value to ReadWriteVariables?

How to make a VB6 function return a value when you call it from another form?

I have a form called Form1. In Form1, I have the following code -
Dim details As clsDetails
Set details = getDetials(1) ' This fails. It doesn't assign a value.
The getDetails function is declared in a separate module as follows-
Public Function getDetials(detailNumber As Integer) As clsDetails
Dim details As clsDetails
Select Case detailNumber
Case "1"
Debug.Print "Inside case1"
Set details = getDetail1()
Debug.Print details.comment ' This prints correctly.
End Select
Set getDetails = details
End Function
However, when I execute the above code, somehow, the details variable in Form1 doesn't get set, even though the getDetails function is called and prints the details correctly inside it. How to rectify this?
Do you have the Option Explicit keyword defined?
It looks like you might have a typo. Your function is called getDetials, but the variable you're setting the result to is getDetails, so the return value is not getting set.
I fixed the typo and everything works as expected on my end. Using the Option Explicit keyword will catch these types of errors.
I think this may be a case of not having Option Explicit On
Check you have Option Explicit at the top of your form (and in fact everywhere)
Your type mistake declaring the function getDetials but then setting an object called getDetails to the newly created class is actually creating a new object and the function return is not being set at all.
Insert Option Explicit and you will see that it won't compile!
When you have added Option Explicit everywhere got to Tools>Options>Editor tab and tick the box that says Require variable declaration - that will make sure it is added every time you add a new code file to your project.