Llenar DataGrid
Publicado por Aurora (10 intervenciones) el 06/11/2007 18:35:19
Hola! buen dia, necesito llenar un datagrid en visual basic 6.0, mi base de datos esta en sql...
Algo me hace falta pero no se que es...
Mi codigo es el siguiente...
tengo un modulo llamado conexion en donde tengo declaradas las variables que necesito para conectarme a mi base de datos
Public cnBase As ADODB.Connection
Public Cmd As Command
Public sql As String
Public Rec As Recordset
y una funcion llamada abrir la cual mando a llamar cuando lo necesito que es esta
Public Function Abrir(Servidor As String, Bd As String) As Boolean
On Error GoTo Errores
Set cnBase = New Connection
cnBase.ConnectionTimeout = 0
cnBase.CommandTimeout = 1200
cnBase.Open "Provider = SQLOLEDB.1; Integrated Security=SSPI; Persist Security = False; Initial Catalog = " & Bd & "; Data Source = " & Servidor & "; OLE DB Services = -1"
'cnBase.Open "Provider = SQLOLEDB.1; Integrated Security=SSPI; Persist Security = False; Initial Catalog = " & Bd & "; Data Source = BackOffice; OLE DB Services = -1"
cnBase.CursorLocation = adUseClient
Set Cmd = New Command
Set Cmd.ActiveConnection = cnBase
Cmd.CommandText = sql
Cmd.CommandType = adCmdText
Cmd.CommandTimeout = 1200
Set Rec = New Recordset
With Rec
.ActiveConnection = cnBase
.CursorLocation = adUseClient
.CursorType = adOpenForwardOnly
.LockType = adLockReadOnly
End With
Abrir = True
Errores:
If Err.Number <> 0 Then Errores.Errores
End Function
Ya con todo esto hago mi consulta
sql = "Select dbo.vendet.Cliente, dbo.clientes.nomcli, " & _
"dbo.clientes.destipcli, sum(dbo.vendet.cant_vend - dbo.vendet.cant_dev) " & _
"as Piezas, sum(dbo.vendet.costounit) as Venta " & _
"From dbo.clientes inner join dbo.vendet " & _
"on dbo.clientes.codcli = dbo.vendet.cliente " & _
"Where dbo.vendet.fechavtasinhora >= '" & Format(DTPFecha_Ini, "yyyy-mm-dd") & "' " & _
"and dbo.vendet.fechavtasinhora >= '" & Format(DTPFecha_Fin, "yyyy-mm-dd") & "' " & _
"group by dbo.vendet.cliente, dbo.clientes.destipcli, dbo.clientes.nomcli " & _
"order by dbo.clientes.nomcli"
Cmd.CommandText = sql
Cmd.Execute
Set DataGrid1.DataSource = sql
pero cuando quiero cargar el datagrid me marca error, el error es "Error 424, Se requiere un objeto" y si tengo mi datagrid con ese nombre...
les puse todo el codigo de la conexion porque supongo que algo ahi esta mal pero no se que es...
espero puedan ayudarme...
gracias...
Algo me hace falta pero no se que es...
Mi codigo es el siguiente...
tengo un modulo llamado conexion en donde tengo declaradas las variables que necesito para conectarme a mi base de datos
Public cnBase As ADODB.Connection
Public Cmd As Command
Public sql As String
Public Rec As Recordset
y una funcion llamada abrir la cual mando a llamar cuando lo necesito que es esta
Public Function Abrir(Servidor As String, Bd As String) As Boolean
On Error GoTo Errores
Set cnBase = New Connection
cnBase.ConnectionTimeout = 0
cnBase.CommandTimeout = 1200
cnBase.Open "Provider = SQLOLEDB.1; Integrated Security=SSPI; Persist Security = False; Initial Catalog = " & Bd & "; Data Source = " & Servidor & "; OLE DB Services = -1"
'cnBase.Open "Provider = SQLOLEDB.1; Integrated Security=SSPI; Persist Security = False; Initial Catalog = " & Bd & "; Data Source = BackOffice; OLE DB Services = -1"
cnBase.CursorLocation = adUseClient
Set Cmd = New Command
Set Cmd.ActiveConnection = cnBase
Cmd.CommandText = sql
Cmd.CommandType = adCmdText
Cmd.CommandTimeout = 1200
Set Rec = New Recordset
With Rec
.ActiveConnection = cnBase
.CursorLocation = adUseClient
.CursorType = adOpenForwardOnly
.LockType = adLockReadOnly
End With
Abrir = True
Errores:
If Err.Number <> 0 Then Errores.Errores
End Function
Ya con todo esto hago mi consulta
sql = "Select dbo.vendet.Cliente, dbo.clientes.nomcli, " & _
"dbo.clientes.destipcli, sum(dbo.vendet.cant_vend - dbo.vendet.cant_dev) " & _
"as Piezas, sum(dbo.vendet.costounit) as Venta " & _
"From dbo.clientes inner join dbo.vendet " & _
"on dbo.clientes.codcli = dbo.vendet.cliente " & _
"Where dbo.vendet.fechavtasinhora >= '" & Format(DTPFecha_Ini, "yyyy-mm-dd") & "' " & _
"and dbo.vendet.fechavtasinhora >= '" & Format(DTPFecha_Fin, "yyyy-mm-dd") & "' " & _
"group by dbo.vendet.cliente, dbo.clientes.destipcli, dbo.clientes.nomcli " & _
"order by dbo.clientes.nomcli"
Cmd.CommandText = sql
Cmd.Execute
Set DataGrid1.DataSource = sql
pero cuando quiero cargar el datagrid me marca error, el error es "Error 424, Se requiere un objeto" y si tengo mi datagrid con ese nombre...
les puse todo el codigo de la conexion porque supongo que algo ahi esta mal pero no se que es...
espero puedan ayudarme...
gracias...
Valora esta pregunta


0