Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 24th, 2006, 08:46 PM
Authorized User
 
Join Date: Feb 2006
Location: Sacramento, CA, USA.
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default ADO Recordset

I am familiar with recordsets in DAO, but I would like to start using ADO. My first attempt is not good. Here is my code, which is coming straight out of an Access 2002 book with my questions.

Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset

Set rst.ActiveConnection = CurrentProject.AccessConnection

The book says it should be "CurrentProject.ActiveConnection", but when I type in CurrentProject ActiveConnection does not come up as a choice. So I am not sure if my choice is causing the problems.

rst.CursorType = adOpenStatic
rst.LockType = adLockOptimistic
rst.Open "Main Table"

When I try and run this I get a runtime error with the above line highlighted.
Invalid SQL statement; Expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT' or 'UPDATE'.


If Not rst.BOF And Not rst.EOF Then
    Response = MsgBox("Not in there", vbDefaultButton1, "yes")
    Exit Sub
Else
    rst.MoveFirst
    Do While rst.EOF <> True 'Continue if last find succeeded.
        If IsNull(CCNnum) = False Then
        Response = MsgBox("There is already a record with this CCN number", vbInformation, "CCN Found")
        End If
    Loop
End If

rst.Close
cnn.Close

Any help would be very much appreciated.

Scott
__________________
ScottP
Reply With Quote
  #2 (permalink)  
Old February 24th, 2006, 09:35 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Hi Scott,

"CurrentProject.ActiveConnection" is an errata in your book. It doesn’t exist in the Access object model.

CurrentProject.AccessConnection is fine but you should usually use simply CurrentProject.Connection with a jet (.mdb) database. CurrentProject.AccessConnection and CurrentProject.Connection return the same ADO connection in a .adp database (the connection used by Access for the currently open database). In a jet database, however, they return two different conncetions. CurrentProject.Connection allows you to use more ADO features in jet, like ADOX, and code that needs index support, like the recodset Seek method.

So I’d use:

Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
Set rst.ActiveConnection = CurrentProject.Connection (or CurrentProject.AccessConnection)

rst.CursorType = adOpenStatic
rst.LockType = adLockOptimistic
rst.Open "MainTable", , , , adCmdTable

Your error is caused by the white space character in your table name. The recordset Open method generates an internal SQL statement, which in your case would have a FROM clause of:

FROM Main Table

This is an invalid SQL FROM clause (no white space allowed). Using “MainTable” will generate a valid internal FROM clause (never use spaces in Access object names!). adCmdTable is optional. It tells the internally generated SQL statement to evaluate your command text as a table and return all of its columns. Specifying adCmdTable when your Source argument is a table (as opposed to say a SQL statement) simply improves performance.

HTH,

Bob

Reply With Quote
  #3 (permalink)  
Old February 24th, 2006, 11:52 PM
Authorized User
 
Join Date: Feb 2006
Location: Sacramento, CA, USA.
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Bob, your help is much appreciated. I will fix my code on Monday.

Scott

ScottP
Reply With Quote
  #4 (permalink)  
Old February 27th, 2006, 04:44 PM
Authorized User
 
Join Date: Feb 2006
Location: Sacramento, CA, USA.
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Bob, the code works great. Thanks again for your help.

Scott
Reply With Quote
  #5 (permalink)  
Old February 27th, 2006, 06:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

You're welcome Scott. Enjoy learning ADO!

Bob

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing ADO Recordset ByVal robzyc Access VBA 2 April 23rd, 2007 09:07 AM
Clone DAO Recordset into ADO Recordset kamrans74 VB How-To 0 March 6th, 2007 11:57 AM
ADO recordset to table kanoorani VB Databases Basics 3 May 16th, 2006 02:59 AM
Ouptut from object Recordset (ADO) AnneOlga Excel VBA 1 March 30th, 2006 06:17 AM
Convert ADO recordset to DAO recordset andrew_taft Access 1 May 5th, 2004 02:31 PM



All times are GMT -4. The time now is 11:41 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.