473,418 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

open Query from Code : Getting Error

Hi,

I have looked at the example called "Open Parameter queries from code"
from the site http://www.mvps.org/access/queries/qry0003.htm

I made up a test which I though looked pretty close (which I will
paste below)

I have put it on the BeforeUpdate event of a form I am using, but
whenever it is called, I get "Run-time error '13':, Type mismatch
Could anybody give me a suggestion of what I did wrong? Probably a
typo, but I really can't spot it :(
Cheers

Max.

Private Sub Form_BeforeUpdate(Cancel As Integer)
' This is a function which will NOT ALLOW data to be entered if it has
conflicting values
' We have a query which is designed to see if to date/times and a room
passed to it would
' conflict with any of the current bookings. If it does, then it has
to NOT allow this
' entry to be entered into the database. A nice message with as much
information as possible
' should be returned... it "This entry for Room ###xx would conflict
with the booking of XXX"
Dim dbs As Database ' The Current Database
Dim rs As Recordset ' Where the query results will be held
Dim qdQry As QueryDef ' The query which will return the conflicts

Set dbs = CurrentDb()
Set qdQry = dbs.QueryDefs("QryTemp")
' MsgBox ("aborts after this statement")
Set rs = qdQry.OpenRecordset() ' Have rs recieve the query
results

MsgBox ("Before Update test message")
End Sub
Nov 12 '05 #1
4 11329
TC
The example is for parameter queries. You have not set any parameters! So if
QryTemp is a parameter query, "pretty close" will not be close enough.

Post the SQL text of your query, and clarify exactly what line of code the
error occurred on. Forget all the comments & stuff, they won't help us find
the problem :-)

HTH,
TC
"Max Harvey" <go****@DELTAL.org> wrote in message
news:7a**************************@posting.google.c om...
Hi,

I have looked at the example called "Open Parameter queries from code"
from the site http://www.mvps.org/access/queries/qry0003.htm

I made up a test which I though looked pretty close (which I will
paste below)

I have put it on the BeforeUpdate event of a form I am using, but
whenever it is called, I get "Run-time error '13':, Type mismatch
Could anybody give me a suggestion of what I did wrong? Probably a
typo, but I really can't spot it :(
Cheers

Max.

Private Sub Form_BeforeUpdate(Cancel As Integer)
' This is a function which will NOT ALLOW data to be entered if it has
conflicting values
' We have a query which is designed to see if to date/times and a room
passed to it would
' conflict with any of the current bookings. If it does, then it has
to NOT allow this
' entry to be entered into the database. A nice message with as much
information as possible
' should be returned... it "This entry for Room ###xx would conflict
with the booking of XXX"
Dim dbs As Database ' The Current Database
Dim rs As Recordset ' Where the query results will be held
Dim qdQry As QueryDef ' The query which will return the conflicts

Set dbs = CurrentDb()
Set qdQry = dbs.QueryDefs("QryTemp")
' MsgBox ("aborts after this statement")
Set rs = qdQry.OpenRecordset() ' Have rs recieve the query
results

MsgBox ("Before Update test message")
End Sub

Nov 12 '05 #2
"TC" <a@b.c.d> wrote in message news:<1069646795.823362@teuthos>...
The example is for parameter queries. You have not set any parameters! So if
QryTemp is a parameter query, "pretty close" will not be close enough.

Post the SQL text of your query, and clarify exactly what line of code the
error occurred on. Forget all the comments & stuff, they won't help us find
the problem :-)

OK, I have ditched my attempt, and tried the one pretty much straight
from the web page..
Now I have a query called QryTemp,
its code is as follows

-----

SELECT tblBooking.BookingID, tblBooking.RoomID, tblBooking.ClientID
FROM tblBooking
WHERE (((tblBooking.BookingID)=[Please Enter Code:]));

-----

As for the code I am using (comments removed)

-----

Private Sub Form_BeforeUpdate(Cancel As Integer)

Dim db As Database 'current database
Dim rs As Recordset 'holds query resultset
Dim qdfParmQry As QueryDef 'the actual query object
Set db = CurrentDb()
Set qdfParmQry = db.QueryDefs("QryTemp")
qdfParmQry("Please Enter Code:") = 3
Set rs = qdfParmQry.OpenRecordset()

End Sub

-----

And I even checked to ensure that BookingID = 3 is a valid record, and
displays a result when the query is run by itself :)

As for where it crashes, it crashes on the "Set rs =
qdfParmQry.OpenRecordset()" line (Thats the line highlighted in Yellow
when I go to debug, with "Run-time error '13': Type mismatch
Any assistance would be great.

NB:. This may be of importance... while in the code editor[1], I had
to go to Tools-->References and select "Microsoft DAO 3.6 Object
Library" for it to even reconise "database" as a type. Is it possible
there are more references I need to select which aren't currently
selected[2]?

[1] About reports as... Microsoft Visual Basic 6.3 Version 9108 VBA:
Retail 6.3.8863 Forms3: 2.01
[2] Currently selected referecnes are
- Visual Basic For Application
- Microsoft Access 10.0 Object Library
- Microsoft ActiveX Data Objects 2.1 Library
- OLE Automation
- Microsoft DAO 3.6 Object Library
Thnaks for any help
Max.
Nov 12 '05 #3
TC
Ok. Try changing:
Dim db As Database
Dim rs As Recordset
Dim qdfParmQry As QueryDef
to:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim qdfParmQry As DAO.QueryDef
Also, I'm not sure whether the Parameters collection is the default
collection of a querydef object, so also change:

qdfParmQry("Please Enter Code:") = 3

to:

qdfParmQry.Parameters("Please Enter Code:") = 3

HTH,
TC

"Max Harvey" <go****@DELTAL.org> wrote in message
news:7a**************************@posting.google.c om... "TC" <a@b.c.d> wrote in message news:<1069646795.823362@teuthos>...
The example is for parameter queries. You have not set any parameters! So if QryTemp is a parameter query, "pretty close" will not be close enough.

Post the SQL text of your query, and clarify exactly what line of code the error occurred on. Forget all the comments & stuff, they won't help us find the problem :-)

OK, I have ditched my attempt, and tried the one pretty much straight
from the web page..
Now I have a query called QryTemp,
its code is as follows

-----

SELECT tblBooking.BookingID, tblBooking.RoomID, tblBooking.ClientID
FROM tblBooking
WHERE (((tblBooking.BookingID)=[Please Enter Code:]));

-----

As for the code I am using (comments removed)

-----

Private Sub Form_BeforeUpdate(Cancel As Integer)

Dim db As Database 'current database
Dim rs As Recordset 'holds query resultset
Dim qdfParmQry As QueryDef 'the actual query object
Set db = CurrentDb()
Set qdfParmQry = db.QueryDefs("QryTemp")
qdfParmQry("Please Enter Code:") = 3
Set rs = qdfParmQry.OpenRecordset()

End Sub

-----

And I even checked to ensure that BookingID = 3 is a valid record, and
displays a result when the query is run by itself :)

As for where it crashes, it crashes on the "Set rs =
qdfParmQry.OpenRecordset()" line (Thats the line highlighted in Yellow
when I go to debug, with "Run-time error '13': Type mismatch
Any assistance would be great.

NB:. This may be of importance... while in the code editor[1], I had
to go to Tools-->References and select "Microsoft DAO 3.6 Object
Library" for it to even reconise "database" as a type. Is it possible
there are more references I need to select which aren't currently
selected[2]?

[1] About reports as... Microsoft Visual Basic 6.3 Version 9108 VBA:
Retail 6.3.8863 Forms3: 2.01
[2] Currently selected referecnes are
- Visual Basic For Application
- Microsoft Access 10.0 Object Library
- Microsoft ActiveX Data Objects 2.1 Library
- OLE Automation
- Microsoft DAO 3.6 Object Library
Thnaks for any help
Max.

Nov 12 '05 #4
"TC" <a@b.c.d> wrote in message news:<1069818088.506719@teuthos>...
Ok. Try changing:
Dim db As Database
Dim rs As Recordset
Dim qdfParmQry As QueryDef


to:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim qdfParmQry As DAO.QueryDef


Also, I'm not sure whether the Parameters collection is the default
collection of a querydef object, so also change:

qdfParmQry("Please Enter Code:") = 3

to:

qdfParmQry.Parameters("Please Enter Code:") = 3

HTH,
TC

Thanks TC, everybody,

With the above correctoins I have managed to make it work. :)

I was so glad when I found the web site with the instructions, and
when they didn't work, I thought I was doomed... you guys Rock.

All the best.

Max.
Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Colleyville Alan | last post by:
I ran into problems in an app in which I received a msg of "cannot lock the table, it is in use by another user". It turns out that I had opened a recordset with a command like set rstmyrecs =...
5
by: Ryan Hubbard | last post by:
Is it possible to get the recordset from an open query window? So you run the query. The window is open. Can vba retrieve this data?
0
by: dd_bdlm | last post by:
I have recently updated my database to include a postcode recognition function. And to perform calculations based on that recognised value. The query that makes all these calculations is very large...
15
by: Rob Nicholson | last post by:
I'm starting to worry a bit now. We're getting the above error when two users hit the same database/page on an ASP.NET application using ADO.NET, talking to a SQL 7 server. The error is perfectly...
3
by: Joanne | last post by:
I have searched through previous threads regarding this error. I went back through all my code to make sure I closed and set to nothing all open db's and rs's. However, I still have one computer...
10
by: jimmy | last post by:
Hi again, sorry for posting two questions so close together but im working on a school project which is due in soon and running into some difficulties implementing the database parts. I have the...
10
by: bluray | last post by:
Hello guys, thanks again for taking the time to help me out with my problems! This problem seems super simple in my head, however getting the coding to make it work is turing out to be not so...
2
n8kindt
by: n8kindt | last post by:
i'm stuck. i'm using access 2007 on a windows vista machine (i've tried running on xp too, though--same result) and this is the error i keep getting whenever i try running my main query: Cannot...
14
by: sarabonn | last post by:
hallo Iam trying to insert a row into a access database using visual c# and iam getting an error " cannot open the action query". here is my code private void button1_Click(object sender,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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,...
0
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...

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.