
ADO PARAMETERIZED QUERIES AND DATA REPORT - project.zip (0/1)
Just thought I would drop a note to those struggling with
parameterized queries using VB6, the DataConnectioDesigner and
DataReports. Below is some sample code that works, the table I used
has few fields and this is simple at best. I use the
ConnectionDesigner and just insert the good old "?" into the variable
you want to pass in the query.
Here is the form code:
Dim rs As Recordset
Private Sub Command1_Click()
Dim rs As Recordset, count As Integer 'just fooling around here for
debugging
DataEnvironment1.Commands.Item("command1").Parameters(0).Value = 4
Set rs = DataEnvironment1.Commands.Item("command1").Execute()
DataEnvironment1.rsCommand1.Open
count = DataEnvironment1.rsCommand1.RecordCount
DataEnvironment1.rsCommand1.Close
End Sub
Private Sub Command2_Click()
DataReport1.Show 'show the report
End Sub
Private Sub Form_Load()
List1.AddItem 1 'load simple listbox with values
List1.AddItem 2
List1.AddItem 3
List1.AddItem 4
End Sub
Private Sub List1_Click()
rsValue = Val(List1.Text) 'set the value of the public variable
'used to set the parameter
End Sub
' The only thing in a module is the rsValue declared as a Public
Variable
Here is the DataReport's Initialize Event:
Private Sub DataReport_Initialize()
Dim rs As Recordset, count As Integer
On Error GoTo err:
DataEnvironment1.Commands.Item("command1").Parameters(0).Value =
rsValue
Set rs = DataEnvironment1.Commands.Item("command1").Execute()
DataEnvironment1.rsCommand1.Open
Exit Sub
err:
DataEnvironment1.Commands.Item("command1").Parameters(0).Value =
rsValue
Set rs = DataEnvironment1.Commands.Item("command1").Execute()
DataEnvironment1.rsCommand1.Close
End Sub
Here is the SQL Query I use:
SELECT Incident_ID, Section, Acuity_Level, Incident_Type FROM
Incidents WHERE (Acuity_Level = ?)
' Again this is just barebones to help folks struggling with this, as
I was... Henry