I need to create simple application using Visual basic 6.0 and I have a problem with the syntax as far as how to use exec method and where to place it.
I appreciate any help! thanks
First I set up:
Microsoft Common Controll 6.0
Microsoft Tabbed Dialog Control
Microsoft ADO Data Controls
Microsoft DataList Controls
Then I created connection object, command object and recordset object using VB code
Then I bound the control ( a ListView control ) to the recordset object.
But I did not get the result correctly ( ListView is Blank!!! )
The problem is : I need to use system stored procedure ( I'm using SQL server as back end ). So instead of using "select group_name from sp_helpgroup"
I need to use "exec sp_helpgroup" and get the group_name from here.
I don't know where and where I should place this Exec statement so that the group_name appear in the ListView drop down menu.
I've tested the code below with a select statement using different table, the code is working and I get the list I wanted. Anyone can help Me please?
Below is my simple code, any help is appreciated very much:
'create a connection object, command object and recordset object:
Option Explicit
Private WithEvents cnCCS As Connection
Private GetGroupList As Command
Private rsStoredGroupList As Recordset
-----------------------------------------------------------------------------------------------------------
Sub Form_Load()
Set cnCCS = New Connection
With cnCCS
.Provider = "MSDASQL.1;Persist Security Info=False;User ID=myname;Data Source=WEB"
.ConnectionString = cnCCS
.Open
End With
'Instantiate a command object
Set GetGroupList = New Command
'Setting the properties of Command Object
With GetGroupList
.ActiveConnection = cnCCS
.CommandText = "Exec sp_helpgroup" <-------- I also tried .CommandText ="sp_helpgroup" and did not work.
.Execute
End With
' Create an instant of the recordset object
Set rsStoredGroupList = New Recordset
'Using command object instead of recordset object property setting
Set rsStoredGroupList = GetGroupList.Execute
' Binding the control to the recordset object, in this case binding dbcboGroupList ( which is a ListView Control ) to the recordest
Set dbcboGroupList.RowSource = rsStoredGroupList
With dbcboGroupList
.ListField = "group_name"
.BoundColumn = "group_id"
.DataChanged = False
End With
End Sub
------------------------------------------------------------------------------------------------------------------------------------
Private Sub cnCCS_ConnectComplete(ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pConnection As ADODB.Connection)
MsgBox "Connection successful!"
End Sub
------------------------------------------------------------------------------------------------------------------------------------
' After finish cerating Connection , recordset and Command object, in Form_Unload event procedures destroy them
Private Sub Form_Unload(Cancel As Integer)
rsStoredGroupList.Close
Set rsStoredGroupList = Nothing
Set GetGroupList = Nothing
cnCCS.Close
Set cnCCS = Nothing
End Sub
--------------------------------------------------------------------------------------------------------------------------------------
I appreciate any help! thanks
First I set up:
Microsoft Common Controll 6.0
Microsoft Tabbed Dialog Control
Microsoft ADO Data Controls
Microsoft DataList Controls
Then I created connection object, command object and recordset object using VB code
Then I bound the control ( a ListView control ) to the recordset object.
But I did not get the result correctly ( ListView is Blank!!! )
The problem is : I need to use system stored procedure ( I'm using SQL server as back end ). So instead of using "select group_name from sp_helpgroup"
I need to use "exec sp_helpgroup" and get the group_name from here.
I don't know where and where I should place this Exec statement so that the group_name appear in the ListView drop down menu.
I've tested the code below with a select statement using different table, the code is working and I get the list I wanted. Anyone can help Me please?
Below is my simple code, any help is appreciated very much:
'create a connection object, command object and recordset object:
Option Explicit
Private WithEvents cnCCS As Connection
Private GetGroupList As Command
Private rsStoredGroupList As Recordset
-----------------------------------------------------------------------------------------------------------
Sub Form_Load()
Set cnCCS = New Connection
With cnCCS
.Provider = "MSDASQL.1;Persist Security Info=False;User ID=myname;Data Source=WEB"
.ConnectionString = cnCCS
.Open
End With
'Instantiate a command object
Set GetGroupList = New Command
'Setting the properties of Command Object
With GetGroupList
.ActiveConnection = cnCCS
.CommandText = "Exec sp_helpgroup" <-------- I also tried .CommandText ="sp_helpgroup" and did not work.
.Execute
End With
' Create an instant of the recordset object
Set rsStoredGroupList = New Recordset
'Using command object instead of recordset object property setting
Set rsStoredGroupList = GetGroupList.Execute
' Binding the control to the recordset object, in this case binding dbcboGroupList ( which is a ListView Control ) to the recordest
Set dbcboGroupList.RowSource = rsStoredGroupList
With dbcboGroupList
.ListField = "group_name"
.BoundColumn = "group_id"
.DataChanged = False
End With
End Sub
------------------------------------------------------------------------------------------------------------------------------------
Private Sub cnCCS_ConnectComplete(ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pConnection As ADODB.Connection)
MsgBox "Connection successful!"
End Sub
------------------------------------------------------------------------------------------------------------------------------------
' After finish cerating Connection , recordset and Command object, in Form_Unload event procedures destroy them
Private Sub Form_Unload(Cancel As Integer)
rsStoredGroupList.Close
Set rsStoredGroupList = Nothing
Set GetGroupList = Nothing
cnCCS.Close
Set cnCCS = Nothing
End Sub
--------------------------------------------------------------------------------------------------------------------------------------