Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / March 2007

Tip: Looking for answers? Try searching our database.

Run-time error Method 'Open of Object' _Recordset' failed

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
bobm - 14 Mar 2007 09:43 GMT
Hello,

I get the subject error message when I run this query from Excel VBE.  I am
trying to populate a combo box in Excel by connecting to an Access database.

The connection string and sql for this as follows. Appreciate all advice
given.

   Sql = "SELECT DISTINCT tblUserData.[Team Leader] " & _
         "FROM tblUserData " & _
         "WHERE (((tblUserData.[Team Leader]) Is Not Null) AND
((tblUserData.Position) In (SELECT tblUserData.Position " & _
         "FROM tblUserData " & _
         "WHERE (((tblUserData.Name)=""" & strName & """));)) AND
((tblUserData.Team) In (SELECT tblUserData.Team " & _
         "FROM tblUserData " & _
         "WHERE (((tblUserData.Name)=""" & strName & """));))) " & _
         "ORDER BY tblUserData.[Team Leader];"
   
   Debug.Print Sql
   
   Set cn1 = CreateObject("ADODB.Connection")
   cn1.Provider = "Microsoft.Jet.OLEDB.4.0"
   cn1.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" &
AdminFilePath & " ;Jet OLEDB:Database Password=" & AdminPwd & ";"
   Set rs1 = CreateObject("ADODB.recordset")
   rs1.Open Sql, cn1
   
   Do Until rs1.EOF
       Me.ComboBox5.AddItem rs1.Fields("Team Leader")
       rs1.MoveNext
   Loop
   rs1.Close
   cn1.Close
   Set cn1 = Nothing
   Set rrs1 = Nothing

The debug.print of the the Sql is...
SELECT DISTINCT tblUserData.[Team Leader] FROM tblUserData WHERE
(((tblUserData.[Team Leader]) Is Not Null) AND ((tblUserData.Position) In
(SELECT tblUserData.Position FROM tblUserData WHERE
(((tblUserData.Name)="John"));)) AND ((tblUserData.Team) In (SELECT
tblUserData.Team FROM tblUserData WHERE (((tblUserData.Name)="John"));)))
ORDER BY tblUserData.[Team Leader];

and this works ok when I run from the SQL window in Access.

Bob
onedaywhen - 14 Mar 2007 10:10 GMT
> The debug.print of the the Sql is...
> SELECT DISTINCT tblUserData.[Team Leader] FROM tblUserData WHERE
[quoted text clipped - 5 lines]
>
> and this works ok when I run from the SQL window in Access.

Try removing the semicolons from the body of the SQL statement e.g.

replace

)="John"));))

with

)="John"))))

Jamie.

--
bobm - 14 Mar 2007 11:31 GMT
thanks for the response but same error when I removed the semicolon from the
body of the sql.

SELECT DISTINCT tblUserData.[Team Leader]
FROM tblUserData
WHERE (((tblUserData.[Team Leader]) Is Not Null)
AND ((tblUserData.Position) In (SELECT tblUserData.Position FROM tblUserData
WHERE (((tblUserData.Name)="John Brown"))))
AND ((tblUserData.Team) In (SELECT tblUserData.Team FROM tblUserData
WHERE (((tblUserData.Name)="John Brown")))))
ORDER BY tblUserData.[Team Leader];

If I remove the nested sql it runs ok.

> Try removing the semicolons from the body of the SQL statement e.g.
>
[quoted text clipped - 7 lines]
>
> Jamie.
Stefan Hoffmann - 14 Mar 2007 11:47 GMT
hi Bob,

> thanks for the response but same error when I removed the semicolon from the
> body of the sql.
> If I remove the nested sql it runs ok.
Have you tested it as a query in Access? Your query may be too complex
for Jet.

Why don't you create a query in Access?

mfG
--> stefan <--
bobm - 14 Mar 2007 16:03 GMT
> Have you tested it as a query in Access? Your query may be too complex
> for Jet.
>
> Why don't you create a query in Access?

thanks for the reply. Yes the query does run in access if i copy and paste
the debug.print sql to the access sql window. ok so perhaps nested queries
are too complex for jet or is there a better way to write the sql that will
be supported by jet?

i have not defined the query in access because i need to populate a combo
box depending on who the user is (strName in the where statement). is there a
way a can do this from access instead - pass the strName variable to an
access query?

regards, bob
Stefan Hoffmann - 14 Mar 2007 16:15 GMT
hi Bob,

> thanks for the reply. Yes the query does run in access if i copy and paste
> the debug.print sql to the access sql window. ok so perhaps nested queries
> are too complex for jet or is there a better way to write the sql that will
> be supported by jet?
Yes, try an INNER JOIN instead of your sub-queries, e.g.

  SELECT DISTINCT U.[Team Leader]
  FROM tblUserData U
  INNER JOIN tblUserData P ON P.Position = U.Position
  INNER JOIN tblUserData T ON T.Team = U.Team
  WHERE NOT IsNull(U.[Team Leader])
  AND U.Name =""
  AND T.Name =""

A also use [Name] as it is a reserved word.

mfG
--> stefan <--
bobm - 15 Mar 2007 03:25 GMT
stefan, thanks for your help ...appreciate it.

got the same error with your sql. after individually writing tbe queries
found that position is a reserved word so had to square bracket it. now all
queries work.

cheers, bob
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.