Senhores adotamos o VBA em nosso ambiente de produção, mas a documentação a respeito não é escassa porem nada é oficial, e o que é oficial tem poucos exemplos de utilização, tentei de diversas formas mas sem sucesso usar uma função que facilitasse as consultas a banco de dados.
retornando assim um recordset, para ser utilizado da melhor forma.
Porem na linha indicada recebo o erro uso null invalido
Segue o codigo
' This function returns a recordset which can hold multiple values.
Public Function GetEmployees() As ADODB.Recordset
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=K:\GCON\BASE DE DADOS\MATMED-ADM.accdb"
cn.Open
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = cn
cmd.CommandType = adCmdText
cmd.CommandText = "SELECT * FROM MATMED;"
cmd.Execute
Set GetEmployees = cn.Execute("select * from MATMED;")
End Function
Private Sub cmdGetRecordset_Click()
Dim strFirstName As String
Dim strLastName As String
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
' Get the recordset object.
Set rst = GetEmployees()
' Use loop to iterate through the recordset row by row to read its content.
Do While Not rst.EOF
strFirstName = rst!COD
'strLastName = rst.Fields("DESC")
' Display in Immediate window.
Debug.Print strFirstName & " " & strLastName
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
End Sub
retornando assim um recordset, para ser utilizado da melhor forma.
Porem na linha indicada recebo o erro uso null invalido
Segue o codigo
' This function returns a recordset which can hold multiple values.
Public Function GetEmployees() As ADODB.Recordset
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=K:\GCON\BASE DE DADOS\MATMED-ADM.accdb"
cn.Open
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = cn
cmd.CommandType = adCmdText
cmd.CommandText = "SELECT * FROM MATMED;"
cmd.Execute
Set GetEmployees = cn.Execute("select * from MATMED;")
End Function
Private Sub cmdGetRecordset_Click()
Dim strFirstName As String
Dim strLastName As String
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
' Get the recordset object.
Set rst = GetEmployees()
' Use loop to iterate through the recordset row by row to read its content.
Do While Not rst.EOF
strFirstName = rst!COD
'strLastName = rst.Fields("DESC")
' Display in Immediate window.
Debug.Print strFirstName & " " & strLastName
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
End Sub