PC Review


Reply
Thread Tools Rate Thread

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

 
 
EMP
Guest
Posts: n/a
 
      10th Nov 2003
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
 
 
 
 
Juan M. Afan de Ribera
Guest
Posts: n/a
 
      10th Nov 2003
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" <(E-Mail Removed)> escribió en el mensaje
news:07c301c3a734$be458170$(E-Mail Removed)...
> 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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database connection problems pwbre Microsoft Outlook BCM 1 13th Oct 2009 05:15 PM
Database Connection Problems Jonathan Wood Microsoft ADO .NET 2 15th Aug 2007 07:08 AM
Making a database connection global Terry Jolly Microsoft ASP .NET 35 15th May 2006 09:10 AM
making a database connection =?Utf-8?B?c3VuZWV0aGk=?= Microsoft ASP .NET 1 14th Sep 2005 01:03 AM
Basic connection to an ADP database - problems and more problems Chris Strug Microsoft Access ADP SQL Server 1 18th Jul 2003 03:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:30 PM.