473,662 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VBA DAO OpenRecordset Error

9 New Member
Hi,

Can anyone help me with this error?

I have this code: Set rs = db.OpenRecordse t("Search")
where search is a parameterised query. When I try to run it I get this error: "Two few parameters, expected 2." I guess I have to put something else inside the brackets but I don't know what.


What I am trying to do is export the result of the query to word when a user clicks a command button. I have been at it all morning and made some progress but this latest error is doing my head in. Any help would be greatly appreciated!

Edit: Also 1 other question. What number do fields start at? I mean in this kind of code: Set fld = rs.Fields(?). I don't know if this second question makes sense but I can't think of any other way to ask it. Just ignore it if it doesn't!
TIA

Kirstin
Jun 19 '07 #1
6 6136
Kirstin
9 New Member
I found this answer to the first one here.
Thankfully, that has been bugging me for ages now :)

Edit(again): now my query doesn't work at all (it was before I just couldn't export it using VBA how I wanted).
I have this now: "SELECT ProjectTitle, ProjectDescript ion
FROM Contracts
WHERE (Contracts.JobT ype=" & Forms!SearchFor m!JobTypeQ & " And Contracts.Clien t=" & Forms!SearchFor m!ClientQ & ") Or (Contracts.JobT ype=" & Forms!SearchFor m!JobTypeQ & " And " & Forms!SearchFor m!ClientQ & "=" N/A") Or (Contracts.Clie nt=" & Forms!SearchFor m!ClientQ & " And " & Forms!SearchFor m!JobTypeQ & "=" N/A");"

Can anyone see any problems with that?
Besides a few syntax errors, what are the Data Types for the following Fields?
  1. Contracts.JobTy pe
  2. Contracts.Clien t
  3. Contracts.JobTy pe
Jun 19 '07 #2
Kirstin
9 New Member
Besides a few syntax errors, what are the Data Types for the following Fields?
  1. Contracts.JobTy pe
  2. Contracts.Clien t
  3. Contracts.JobTy pe
Contracts.JobTy pe and Contracts.Clien t are Text

Also if it helps JobTypeQ and ClientQ are lookup comboboxes with predefined values.

It was working fine before when it was like this:
"SELECT ProjectTitle, ProjectDescript ion
FROM Contracts
WHERE (Contracts.JobT ype = Forms!SearchFor m!JobTypeQ AND Contracts.Clien t = Forms!SearchFor m!ClientQ) OR (Contracts.JobT ype = Forms!SearchFor m!JobTypeQ AND Forms!SearchFor m!ClientQ = " N/A") OR (Contracts.Clie nt = Forms!SearchFor m!ClientQ AND Forms!SearchFor m!JobTypeQ =" N/A");"
but then I couldn't use it in VBA only reports etc. Infuriating!

Also I am a bit confused because of the way you answered me, was it okay to post this as a new reply or should I have edited my last one? I don't know who am talking to either, spooky!
Jun 19 '07 #3
MMcCarthy
14,534 Recognized Expert Moderator MVP
Hi Kirsten
I'm not sure who added the edit to your second post however, there are a few rules to follow. For text fields you have to enclose them in single quotes.

Also you cannot use this statement in a query.

Expand|Select|Wrap|Line Numbers
  1. Forms!SearchForm!ClientQ & "=" N/A"
If you explain the logic behind this statement I will try to help further.

I've encluded a simplified version of the query below just to show you how the single quotes work

Expand|Select|Wrap|Line Numbers
  1. "SELECT ProjectTitle, ProjectDescription
  2. FROM Contracts
  3. WHERE (JobType='" & Forms!SearchForm!JobTypeQ & "' And Client='" & Forms!SearchForm!ClientQ & "')"
  4.  
Just as a note for future reference dates would be enclosed with #
Jun 19 '07 #4
Kirstin
9 New Member
Thanks,

I eventually managed to solve it using this vba code:

Dim Search As QueryDef

...
Set Search = db.QueryDefs("S earch")

Search.Paramete rs(0) = Forms!SearchFor m!JobTypeQ
Search.Paramete rs(1) = Forms!SearchFor m!ClientQ
Set rs = Search.OpenReco rdset

Oh and why can't I use the code:

"Forms!SearchFo rm!ClientQ & "=" N/A" "?

It works(well actually
"Forms!SearchFo rm!ClientQ =" N/A") . The reason for it is so the user can choose to search for a certain job type for a certain client or a certain job type for all clients or all job types for a certain client. If that makes sense. I have a lookup up combobox from predefined tables of different jobtypes and clients that the user can't add to. I am pretty new to this and kinda making it up as I go along so maybe there is a better way.
Jun 20 '07 #5
MMcCarthy
14,534 Recognized Expert Moderator MVP
Thanks,

I eventually managed to solve it using this vba code:

Dim Search As QueryDef

...
Set Search = db.QueryDefs("S earch")

Search.Paramete rs(0) = Forms!SearchFor m!JobTypeQ
Search.Paramete rs(1) = Forms!SearchFor m!ClientQ
Set rs = Search.OpenReco rdset

Oh and why can't I use the code:

"Forms!SearchFo rm!ClientQ & "=" N/A" "?

It works(well actually
"Forms!SearchFo rm!ClientQ =" N/A") . The reason for it is so the user can choose to search for a certain job type for a certain client or a certain job type for all clients or all job types for a certain client. If that makes sense. I have a lookup up combobox from predefined tables of different jobtypes and clients that the user can't add to. I am pretty new to this and kinda making it up as I go along so maybe there is a better way.
You are using this code in a query and that statement makes no sense in a query. Can you actually post the full code of this procedure.
Jun 20 '07 #6
Kirstin
9 New Member
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Public WordApp As Word.Application
  4. Public doc As Word.Document
  5. Public sel As Word.Selection
  6. Public Sub WordEx()
  7.  
  8. Set WordApp = New Word.Application
  9. WordApp.Documents.Add
  10. Set doc = WordApp.ActiveDocument
  11. Set sel = WordApp.Selection
  12. WordApp.Visible = True
  13.  
  14. End Sub
  15.  
  16. Public Sub WordXPort()
  17. Dim db As DAO.Database
  18. Dim Search As QueryDef
  19. Dim rs As DAO.Recordset
  20. Dim Title As DAO.Field
  21. Dim Description As DAO.Field
  22.  
  23. Set db = CurrentDb()
  24. Set Search = db.QueryDefs("Search")
  25.  
  26. Search.Parameters(0) = Forms!SearchForm!JobTypeQ
  27. Search.Parameters(1) = Forms!SearchForm!ClientQ
  28.  
  29. Set rs = Search.OpenRecordset
  30.  
  31. If rs.RecordCount = 0 Then Exit Sub
  32.  
  33. Set Title = rs.Fields(0)
  34. Set Description = rs.Fields(1)
  35.  
  36. WordEx
  37.  
  38. Do Until rs.EOF
  39.  
  40. sel.TypeText Text:="Job Title - " & Title.Value & vbCrLf 
  41. sel.TypeText Text:="Job Description - " & Description.Value & vbCrLf 
  42.  
  43. rs.MoveNext
  44.  
  45. Loop
  46.  
  47. End Sub
  48.  
The bit inside the loop is just me testing, I will be editing that to display it how I want (hopefully in a table if that is possible).

My SQL is:
Expand|Select|Wrap|Line Numbers
  1. SELECT Contracts.ProjectTitle, Contracts.ProjectDescription, Contracts.ProjectCost
  2. FROM Contracts
  3. WHERE (Contracts.JobType=Forms!SearchForm!JobTypeQ And Contracts.Client=Forms!SearchForm!ClientQ) Or (Contracts.JobType=Forms!SearchForm!JobTypeQ And (Forms!SearchForm!ClientQ=" N/A" or "Select Client")) Or (Contracts.Client=Forms!SearchForm!ClientQ And (Forms!SearchForm!JobTypeQ=" N/A" or "Select Job Type"));
It all works now anyway :) But if there is a better way to do it then please let me know!
Jun 20 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
17867
by: Philippa | last post by:
I'm trying to access data in vba using the openrecordset command. The data in on a SQL Server 2000 database, and I have linked tables to that data. the Table I'm trying to access is one of these linked tables, and my codes is as follows: Set vRS = CurrentDb.OpenRecordset(tbl_DataCommentLog) when this line is executed I get the error: "Run-time error '3622':
8
4796
by: Russell Potter | last post by:
I'm trying to create a recordset using "currentDB.OpenRecordSet", using a query as the "source" string (the only parameter: so all the others are set to their defaults which, I believe, means the recordset returned will be a "table" one), but VB keeps giving me "type mismatch" errors. Should this problem be resolved by changing the "options" parameter so that a compatible recordset will be returned, thus eliminating the error? Thanks,
2
4480
by: MLH | last post by:
I copied the following code right out of MS Access 2.0 HELP under Index Property Setting example... Sub Button0_Click () Dim MyDB As Database, MyTable As Recordset Set MyDB = DBEngine.Workspaces(0).Databases(0) Set MyTable = MyDB.OpenRecordset("tblOrders", DB_OPEN_TABLE) MyTable.Index = "PrimaryKey" MyTable.Seek "=", 10050 MyTable.Close
4
4460
by: google | last post by:
I am trying to implement form that allows a user to update data in multiple records in a table based on criteria they enter in the form. Because I want to allow some user interaction on a record level, and provide some additional data validation, I'm trying to do it with code instead of an update query. Unfortunately, it's not letting me update the data: I'm getting an error saying the data is read only. The same query statement, when...
0
5377
by: mo. | last post by:
I need some help in accessing Jet database using DAO. I have just started to learn c# and am trying to rewrite a program I have in vb.net to c#. In VB.Net I can do this: Dim ws As DAO.Workspace Dim db As DAO.Database ws = DAODBEngine_definst.Workspaces(0) db = ws.OpenDatabase(DBFileName) Dim rs As DAO.Recordset
5
19915
by: Sunnyrain | last post by:
I am developing a program in Access 2000. I couldn't make OpenRecordset method work right. It's working when I opened a simple SQL query below in OpenRecordset. ..... Dim dbs As Database, rst As Recordset Set dbs = CurrentDb
10
3721
by: MLH | last post by:
Gentlemen: I am having one heck of a time taking a DAO walk through the records in an SQL dynaset. I'm trying to walk a set of records returned by a UNION query. I'm attempting to filter the records to those related to vehicle #60 ( = 60 ). If I explicitly specify 60 in the SQL ==everything works fine. Take a look: 100 PString = "SELECT & " & Chr$(&H22) & Space(1) & Chr$(&H22) & " & AS Recipient " 120 PString = PString & "FROM...
22
10275
by: MLH | last post by:
100 Dim db As Database, rst As Recordset 120 Set db = CurrentDb 140 PString = "SELECT qryBatchList.ReadyFor906, qryBatchList.BatchID FROM qryBatchList WHERE qryBatchList.BatchID=GetCurrentBatchID()" 160 Set rst = db.OpenRecordset(PString, dbOpenDynaset) At compile time, things are OK. But at run time, line #160 gives rise to an error saying some FN I've used for years is undefined. It almost seems like it pukes on some random
7
11049
by: mirandacascade | last post by:
The questions are toward the bottom of this post. Situation is this: 1) Access 97 2) Multi-user appplication 3) SQL Server 2000 4) Sporadically (i.e. less than 1% of the time) encounter the following error: 3218 Couldn't update; currently locked
0
4550
by: rguidry | last post by:
Hello All, I am currently using a dao connection string for an upsized Access App with a SQL Server backend. I get the following error when trying to use the dbOpenTable type. Run-time error '3219' Invalid operation I can use the dbOpenDynaset type but then my .Index = "PrimaryKey" and .Seek will generate run-time errors. I have tried different combinations of Types, Options, and LockEdits unsucessfully.
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8856
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1992
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.