PC Review Forums Newsgroups Microsoft Access Microsoft Access VBA Modules VBA & DAO problems - not making the connection to the database

Reply

VBA & DAO problems - not making the connection to the database

 
Thread Tools Rate Thread
Old 10-11-2003, 03:45 AM   #1
EMP
Guest
 
Posts: n/a
Default VBA & DAO problems - not making the connection to the database


I have given a sample of my code. It is not making
connection to the database. I get a recordcount of -1
which is weird. What am I doing wrong? Can anyone help
me out. Thanks!



Dim strSQL As String

Dim objADOConnect As New ADODB.Connection 'connection
Dim objRecSet As New ADODB.Recordset 'recordset
Dim objCommand As New ADODB.Command 'command

strSQL = "SELECT [index_id],[folder_name],[parent_id]
FROM Folder_Info" & _
" WHERE [parent_id] = '0'" & _
" ORDER BY [folder_name]"


With objADOConnect
.ConnectionString = "DBQ=speakers.mdb;" & _
"DRIVER={Microsoft Access Driver
(*.mdb)};" & _
"DefaultDir=C:\My Documents\Eve
Marie\Projects\Speaker_Specialist\Data;"
'.ConnectionString = "Provider =
Microsoft.Jet.OLEDB.4.0; Data Source= " & _
"C:\My Documents\Eve
Marie\Projects\Speaker_Specialist\Data\speakers.mdb"
.Open
End With

With objCommand
.CommandText = strSQL
.CommandType = adCmdText
.ActiveConnection = objADOConnect
Set objRecSet = .Execute ****This is the problem area
End With

  Reply With Quote
Old 10-11-2003, 07:51 AM   #2
Juan M. Afan de Ribera
Guest
 
Posts: n/a
Default Re: VBA & DAO problems - not making the connection to the database

That is because the recordset you get with the command .execute method
returns the default recordset, which cursor type property is
adOpenForwardOnly and which lock type is adLockReadOnly, and it needs to be
another kind of cursor type and lock type for recordcount to be available.
You better use the recordset .Open method. For example:

Dim rst As New ADODB.Recordset

With rst
.ActiveConnection = CurrentProject.Connection
.Source = "customers"
.CursorType = adOpenKeyset
.LockType = adLockReadOnly
.Open
End With

Debug.Print rst.RecordCount

rst.Close
Set rst = Nothing

I don't really know if it is a better solution for it, because I'm not an
expert programming in ADO, but this could be a way to do it.

HTH

--
Saludos desde Barcelona
Juan M. Afan de Ribera
<MVP Ms Access>
http://www.juanmafan.tk
http://www.clikear.com/webs4/juanmafan


"EMP" <kperatopoulos1899@wideopenwest.com> escribió en el mensaje
news:07c301c3a734$be458170$a001280a@phx.gbl...
> I have given a sample of my code. It is not making
> connection to the database. I get a recordcount of -1
> which is weird. What am I doing wrong? Can anyone help
> me out. Thanks!
>
>
>
> Dim strSQL As String
>
> Dim objADOConnect As New ADODB.Connection 'connection
> Dim objRecSet As New ADODB.Recordset 'recordset
> Dim objCommand As New ADODB.Command 'command
>
> strSQL = "SELECT [index_id],[folder_name],[parent_id]
> FROM Folder_Info" & _
> " WHERE [parent_id] = '0'" & _
> " ORDER BY [folder_name]"
>
>
> With objADOConnect
> .ConnectionString = "DBQ=speakers.mdb;" & _
> "DRIVER={Microsoft Access Driver
> (*.mdb)};" & _
> "DefaultDir=C:\My Documents\Eve
> Marie\Projects\Speaker_Specialist\Data;"
> '.ConnectionString = "Provider =
> Microsoft.Jet.OLEDB.4.0; Data Source= " & _
> "C:\My Documents\Eve
> Marie\Projects\Speaker_Specialist\Data\speakers.mdb"
> .Open
> End With
>
> With objCommand
> .CommandText = strSQL
> .CommandType = adCmdText
> .ActiveConnection = objADOConnect
> Set objRecSet = .Execute ****This is the problem area
> End With
>



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off