Richard
Fri Apr 06 13:43:16 CDT 2007
"XP" <XP@discussions.microsoft.com> wrote in message
news:550C8ABC-0B03-43C4-8B7A-32FA7F53AD89@microsoft.com...
> Using Office 2003 and Windows XP:
>
> I have the following code which is functioning well, now I have two
> questions:
>
> 1) Is it necessary to specify a CursorLocation ? - If so what value should
> be used?
>
> 2) I need to return a RecordSet from the DB, could someone please post
> example code on how to do this?
>
> sFullNameSource = "C:\NorthWind.mdb"
> Set cn = CreateObject("ADODB.Connection")
> Set rs = CreateObject("ADODB.Recordset")
> cn.CursorLocation = 1 '<<<Is this necessary???
> cn.Provider = "Microsoft.Jet.OLEDB.4.0;"
> cn.Open "Data Source=" & sFullNameSource & ";"
> If cn.State = 1 Then Msgbox "Connected..."
> sSQL = "Select * From Customers;"
> rs.Open ???????????????????????????
>
> Thanks much in advance...
The only time I use the cursorLocation property is when I need to use the
Execute method of the Command object to return a Recordset, because I need
to enable paging (a property of the Command object) and I also want to use a
cursor that allows forward and backup movement (so I can retrieve
RecordCount and still enumerate the Recordset). In that case (rare) I set
cursorLocation to 3 (adUseClient). Documentation indicates cursorLocation of
1 means adUseNone, which is obsolete. The default (2 which is asUseServer)
is fine. I would say to not set a value.
Below is code similar to what I use to query Access database.
=================
strPath = "C:\Scripts\Customers"
strConnect = "DRIVER=Microsoft Access Driver (*.mdb);" _
& "FIL=MS Access;DriverID=25;DefaultDir=" & strPath _
& ";DBQ=" & strPath & "\Customers.mdb;"
Set adoRecordset = CreateObject("ADODB.Recordset")
adoRecordset.ActiveConnection = strConnect
adoRecordset.Source = "SELECT CustomerID, ContactName, Email, Phone " _
& "FROM Customers"
adoRecordset.Open
Do Until adoRecordset.EOF
strCustomerID = adoRecordset.Fields("CustomerID").Value
strContact = adoRecordset.Fields("ContactName").Value
strEmail = adoRecordset.Fields("Email").Value
strPhone = adoRecordset.Fields("Phone").Value
Wscript.Echo strCustomerID & ";" & strContact _
& ";" & strEmail & ";" & strPhone
adoRecordset.MoveNext
Loop
adoRecordset.Close
Set adoRecordset = Nothing
============
I have also used:
strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& strPath & "\Customers.mdb"
There are many ways to do these things. The method above skips the
Connection object. If you use a Connection object, then you assign the
Connection object to the ActiveConnection property of the Recordset object.
I either assign the query to the Source property of a Recordset object, or
to the CommandText property of a Command object (after which the Execute
method of the Command object is assigned to a Recordset object). So many
options can be confusing. The snippet I posted seems simplest.
--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab -
http://www.rlmueller.net
--