Connecting Tech Pros Worldwide Help | Site Map

Queries in VBA,ADO

Familiar Sight
 
Join Date: Apr 2007
Location: Sydney, Australia
Posts: 168
#1: May 8 '07
Hi,

I am trying to figure out how to write queries using ADO
For example if I want to Select (Field) From (Table) Where
(Field) = (TextBox in Form) and just open that query as a recordset.
I am getting the following error:

RunTime error 3709
The Connection Cannot be used to perform this operation. It is either closed or invalid in this context.
This is my code:
-----------------------------------------
Option Compare Database
Option Explicit
Public rst As New ADODB.Recordset
Public cnn As ADODB.Connection
------------------------------------------------------
</ADO>
Set cnn = CurrentProject.Connection
rst.Open "Select tblJobDetails.RcptID From tblJobDetails Where tblJobDetails.TFN = Forms!frmJobDetailsMain.tbTFN"
If (rst.EOF And rst.BOF) Then MsgBox "The Recordset is Blank" Else MsgBox "The Recordset isn't Blank"
Set rst = Nothing
Set cnn = Nothing
</ADO>
tbTFN is a text box containing the value i would like the query to filter by


Thanking you kindly, This forum has been a great help to me.
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,865
#2: May 8 '07

re: Queries in VBA,ADO


Try this ...

Expand|Select|Wrap|Line Numbers
  1. rst.Open "Select tblJobDetails.RcptID From tblJobDetails Where tblJobDetails.TFN = [Forms]![frmJobDetailsMain]![tbTFN]"
  2.  
Familiar Sight
 
Join Date: Apr 2007
Location: Sydney, Australia
Posts: 168
#3: May 8 '07

re: Queries in VBA,ADO


Quote:

Originally Posted by mmccarthy

Try this ...

Expand|Select|Wrap|Line Numbers
  1. rst.Open "Select tblJobDetails.RcptID From tblJobDetails Where tblJobDetails.TFN = [Forms]![frmJobDetailsMain]![tbTFN]"
  2.  

I Tried it, but I'm getting the same error, it the error as per my first post is reffering to the current ADODB connection "cnn" being unable to open sql statements as a recordset, maybe I'm missing a reference in the access library or something. I'm not having any problems with opening external Query objects via ADO as recordsets, but even those give problems when I create an external Select Query and set the Criteria field as a text box from a form.
Familiar Sight
 
Join Date: Apr 2007
Location: Sydney, Australia
Posts: 168
#4: May 8 '07

re: Queries in VBA,ADO


Yeah, I didnt do something to intelligent, Here is a corrected version of what I was doing before I hadn't Defined set the connection "cnn" when I was declaring the recordset. but now its giving me a different error, now its saying No Value Given for one or more given Parameters, the runtime error code is 2147217904.
[Code/]
rst.Open "Select tblJobDetails.RcptID From tblJobDetails Where tblJobDetails.TFN = Forms!frmJobDetailsMain!tbTFN", cnn, adOpenKeyset, adLockOptimistic [/code]

Thanks
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,865
#5: May 8 '07

re: Queries in VBA,ADO


Quote:

Originally Posted by iheartvba

I Tried it, but I'm getting the same error, it the error as per my first post is reffering to the current ADODB connection "cnn" being unable to open sql statements as a recordset, maybe I'm missing a reference in the access library or something. I'm not having any problems with opening external Query objects via ADO as recordsets, but even those give problems when I create an external Select Query and set the Criteria field as a text box from a form.

You've used </ADO> to define your code but how is it actually defined in the module. In other words can you post the opening and closing sections of this procedure.
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,865
#6: May 8 '07

re: Queries in VBA,ADO


Quote:

Originally Posted by iheartvba

Yeah, I didnt do something to intelligent, Here is a corrected version of what I was doing before I hadn't Defined set the connection "cnn" when I was declaring the recordset. but now its giving me a different error, now its saying No Value Given for one or more given Parameters, the runtime error code is 2147217904.
[Code/]
rst.Open "Select tblJobDetails.RcptID From tblJobDetails Where tblJobDetails.TFN = Forms!frmJobDetailsMain!tbTFN", cnn, adOpenKeyset, adLockOptimistic [/code]

Thanks

Put the square brackets in as per my previous example
[Code/]
rst.Open "Select tblJobDetails.RcptID From tblJobDetails Where tblJobDetails.TFN = [Forms]![frmJobDetailsMain]![tbTFN]", cnn, adOpenKeyset, adLockOptimistic [/code]

Also make sure the form you are referencing is open.
Familiar Sight
 
Join Date: Apr 2007
Location: Sydney, Australia
Posts: 168
#7: May 8 '07

re: Queries in VBA,ADO


Ok I seem to have fixed it, heres what it looks like
[/code] rst.Open "Select tblJobDetails.RcptID From tblJobDetails Where tblJobDetails.TFN = '& Forms!frmJobDetailsMain!tbTFN &'", cnn, adOpenKeyset, adLockOptimistic [/code]

PS sorry about the poor tagging practices, I have been meaning to learn how to do it properly.


Thanks Guys
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,865
#8: May 8 '07

re: Queries in VBA,ADO


Quote:

Originally Posted by iheartvba

Ok I seem to have fixed it, heres what it looks like

Expand|Select|Wrap|Line Numbers
  1.  rst.Open "Select tblJobDetails.RcptID From tblJobDetails Where tblJobDetails.TFN = '& Forms!frmJobDetailsMain!tbTFN &'", cnn, adOpenKeyset, adLockOptimistic 
PS sorry about the poor tagging practices, I have been meaning to learn how to do it properly.


Thanks Guys

Leave the backslash off the first code tag.

Just out of curiousity where is this code?
Reply


Similar Microsoft Access / VBA bytes