473,508 Members | 2,400 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.OpenRecordset("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 6132
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, ProjectDescription
FROM Contracts
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 (Contracts.Client=" & Forms!SearchForm!ClientQ & " And " & Forms!SearchForm!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.JobType
  2. Contracts.Client
  3. Contracts.JobType
Jun 19 '07 #2
Kirstin
9 New Member
Besides a few syntax errors, what are the Data Types for the following Fields?
  1. Contracts.JobType
  2. Contracts.Client
  3. Contracts.JobType
Contracts.JobType and Contracts.Client 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, ProjectDescription
FROM Contracts
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 (Contracts.Client = Forms!SearchForm!ClientQ AND Forms!SearchForm!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("Search")

Search.Parameters(0) = Forms!SearchForm!JobTypeQ
Search.Parameters(1) = Forms!SearchForm!ClientQ
Set rs = Search.OpenRecordset

Oh and why can't I use the code:

"Forms!SearchForm!ClientQ & "=" N/A" "?

It works(well actually
"Forms!SearchForm!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("Search")

Search.Parameters(0) = Forms!SearchForm!JobTypeQ
Search.Parameters(1) = Forms!SearchForm!ClientQ
Set rs = Search.OpenRecordset

Oh and why can't I use the code:

"Forms!SearchForm!ClientQ & "=" N/A" "?

It works(well actually
"Forms!SearchForm!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
17856
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...
8
4762
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...
2
4476
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 =...
4
4453
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...
0
5364
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...
5
19898
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...
10
3693
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...
22
10254
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...
7
11044
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...
0
4517
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...
0
7223
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,...
1
7034
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7488
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...
0
5623
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,...
1
5045
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...
0
4702
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1544
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 ...
0
412
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...

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.