473,790 Members | 2,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parameter query in VBA

Hi -

I'm trying to run a query as part of a VBA procedure, and what I want
it to do is to grab the value for a parameter (the current month) from
another part of the procedure. I clearly am missing something,
however. Here's what I have:

Dim db As Database
Dim rs As Recordset
Dim rs2 As Recordset
Dim rs3 As Recordset
Dim strCurrentMonth As String
Dim strMsg As String
Dim qryPar As QueryDef

a bunch of stuff...

Set qryPar = db.QueryDefs("q ryMonthlyAccrua lUpdate")
qryPar.Paramete rs("Month") = rs3!Month
DoCmd.OpenQuery "qryMonthlyAccr ualUpdate", acViewNormal

The value of rs3!Month should be whatever month's data they have just
entered (this is a database for tracking sick/annual accrual). The
query is designed to then calculate how many hours they have accrued
for that month and add it to a table so that their current balances
can be calculated. What is happening, however, is that I'm getting an
error that the rows can't be appended because of an access key
violation - I think this is because there isn't a value for the
month. I have both "month" and "ID" set as primary keys in the table.

Any help would be greatly appreciated. Also, if this is the wrong
forum for this message, I apologize.

TIA,
Heather

Mar 5 '07 #1
8 20767
On Mar 5, 6:39 pm, "hbean" <bean...@gmail. comwrote:
Hi -

I'm trying to run a query as part of a VBA procedure, and what I want
it to do is to grab the value for a parameter (the current month) from
another part of the procedure. I clearly am missing something,
however. Here's what I have:

Dim db As Database
Dim rs As Recordset
Dim rs2 As Recordset
Dim rs3 As Recordset
Dim strCurrentMonth As String
Dim strMsg As String
Dim qryPar As QueryDef

a bunch of stuff...

Set qryPar = db.QueryDefs("q ryMonthlyAccrua lUpdate")
qryPar.Paramete rs("Month") = rs3!Month
DoCmd.OpenQuery "qryMonthlyAccr ualUpdate", acViewNormal

The value of rs3!Month should be whatever month's data they have just
entered (this is a database for tracking sick/annual accrual). The
query is designed to then calculate how many hours they have accrued
for that month and add it to a table so that their current balances
can be calculated. What is happening, however, is that I'm getting an
error that the rows can't be appended because of an access key
violation - I think this is because there isn't a value for the
month. I have both "month" and "ID" set as primary keys in the table.

Any help would be greatly appreciated. Also, if this is the wrong
forum for this message, I apologize.

TIA,
Heather
If there are no records for Month (which is a reserved word - function
- and may cause even more problems) then you can't update. A key
violation can also mean that you are attempting to create duplicate
values in a field that is set to No Duplicates.

Mar 6 '07 #2
The error occurs because the OpenQuery does not relate to the specific
instance of the QueryDef you assigned the parameter to.

The QueryDef object has an OpenRecordset method, so you can use the approach
you attempted with OpenRecordset. It does not have an OpenQuery method, so
you will need to take an alternative approach.

Some alternatives:
a) Open a form. Assign the value to the form, e.g.:
Forms!Form1!txt Month = rs!Month
and in the Criteria of the form, use:
Forms!Form1!txt Month
as the parameter.

b) Assign the SQL of the entire query, e.g.:
Const strcStub = "SELECT * FROM Table1 WHERE ([Month] = "
Const strcTail = ") ORDER BY SomeField;"
Currentdb.Query Defs("qryMonthl yAccrualUpdate" ).SQL = strcStub &
rs3!Month & strcTail

c) Design a form (in datasheet view if you wish) instead of the query, and
assign its RecordSource (as above.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"hbean" <be*****@gmail. comwrote in message
news:11******** **************@ 30g2000cwc.goog legroups.com...
>
I'm trying to run a query as part of a VBA procedure, and what I want
it to do is to grab the value for a parameter (the current month) from
another part of the procedure. I clearly am missing something,
however. Here's what I have:

Dim db As Database
Dim rs As Recordset
Dim rs2 As Recordset
Dim rs3 As Recordset
Dim strCurrentMonth As String
Dim strMsg As String
Dim qryPar As QueryDef

a bunch of stuff...

Set qryPar = db.QueryDefs("q ryMonthlyAccrua lUpdate")
qryPar.Paramete rs("Month") = rs3!Month
DoCmd.OpenQuery "qryMonthlyAccr ualUpdate", acViewNormal

The value of rs3!Month should be whatever month's data they have just
entered (this is a database for tracking sick/annual accrual). The
query is designed to then calculate how many hours they have accrued
for that month and add it to a table so that their current balances
can be calculated. What is happening, however, is that I'm getting an
error that the rows can't be appended because of an access key
violation - I think this is because there isn't a value for the
month. I have both "month" and "ID" set as primary keys in the table.

Any help would be greatly appreciated. Also, if this is the wrong
forum for this message, I apologize.

TIA,
Heather
Mar 6 '07 #3
rkc
hbean wrote:
Dim db As Database
Dim rs As Recordset
Dim rs2 As Recordset
Dim rs3 As Recordset
Dim strCurrentMonth As String
Dim strMsg As String
Dim qryPar As QueryDef

a bunch of stuff...

Set qryPar = db.QueryDefs("q ryMonthlyAccrua lUpdate")
qryPar.Paramete rs("Month") = rs3!Month
DoCmd.OpenQuery "qryMonthlyAccr ualUpdate", acViewNormal

The value of rs3!Month should be whatever month's data they have just
entered (this is a database for tracking sick/annual accrual). The
query is designed to then calculate how many hours they have accrued
for that month and add it to a table so that their current balances
can be calculated. What is happening, however, is that I'm getting an
error that the rows can't be appended because of an access key
violation - I think this is because there isn't a value for the
month. I have both "month" and "ID" set as primary keys in the table.
You're using the QueryDef incorrectly if you're trying to run
a action query. Doesn't it prompt you for the parameter? That should
be a clue.

Set qryPar = db.QueryDefs("q ryMonthlyAccrua lUpdate")
qryPar.Paramete rs("Month") = rs3!Month
'----------------
qryPar.Execute "qryMonthlyAccr ualUpdate"
'----------------
Mar 6 '07 #4
On Mar 5, 5:42 pm, rkc <r...@rkcny.yab ba.dabba.do.com wrote:
>
Set qryPar = db.QueryDefs("q ryMonthlyAccrua lUpdate")
qryPar.Paramete rs("Month") = rs3!Month
'----------------
qryPar.Execute "qryMonthlyAccr ualUpdate"
'----------------
Yes, it was prompting me for the parameter (month), which I didn't
want it to do. I was wanting it to grab the month from the rs3!month
value, which isn't working. I tried the above, which doesn't seem to
work either. I think I will take one of the above suggestions and
have users enter months from a form so that I know which month they're
entering based on which button they've selected, and see how far I get
with that.

Thanks for the suggestions.

Heather

Mar 6 '07 #5
rkc
hbean wrote:
On Mar 5, 5:42 pm, rkc <r...@rkcny.yab ba.dabba.do.com wrote:
> Set qryPar = db.QueryDefs("q ryMonthlyAccrua lUpdate")
qryPar.Paramete rs("Month") = rs3!Month
'----------------
qryPar.Execute "qryMonthlyAccr ualUpdate"
'----------------

Yes, it was prompting me for the parameter (month), which I didn't
want it to do. I was wanting it to grab the month from the rs3!month
value, which isn't working. I tried the above, which doesn't seem to
work either. I think I will take one of the above suggestions and
have users enter months from a form so that I know which month they're
entering based on which button they've selected, and see how far I get
with that.
You can change your method and struggle with that or just fix what
you're doing now. I would check the results returned by rs3 to make
sure it's working first. I'd also think about naming the recordset
variables something that indicated what they were supposed to be returning.
Mar 6 '07 #6
I know that rs3!month is giving the correct month because I'm writing
that value to another table, and it's returning the correct value. Is
that what you mean? I can change the rs labels to something different
- that's not a big deal. What else would you suggest to try to fix
this? It's really causing me a lot of grief, and I'm really not
getting anywhere with my alternatives.

TIA,
Heather

Mar 7 '07 #7
rkc
hbean wrote:
I know that rs3!month is giving the correct month because I'm writing
that value to another table, and it's returning the correct value. Is
that what you mean? I can change the rs labels to something different
- that's not a big deal. What else would you suggest to try to fix
this? It's really causing me a lot of grief, and I'm really not
getting anywhere with my alternatives.
If I were you the first thing I would do is open up a test database
and write some code to prove to myself that you can execute an action
query using QueryDef.Execut e and a saved parameter query.

Then I would write some test code to prove to myself that my query
is actually doing what I think it is doing.
Mar 7 '07 #8
THANK YOU, THANK YOU, THANK YOU!

The QueryDef.Execut e did the trick! Many, many thanks! I've been
stuck for a week on this.

Heather

Mar 8 '07 #9

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

Similar topics

3
16947
by: WGW | last post by:
Though I am a novice to MS SQL server (2000 I believe), I can do almost! everything I need. Maybe not efficiently, but usefully. However, I have a problem -- a complex query problem... I can create a parameter query in a stored procedure, but how do I use the result set of a parameter query in a select query (in the same or another sp)? In short, if a select query contains a result table that is generated as a parameter query, how do I...
5
4303
by: MX1 | last post by:
Simpler way to ask question from my previous post. I wrote a query and it has a paramter field in it. I want to enter a date with the current year. If it I put in 6/30/2003, it works great. If I put in some kind of a variable based input for the parameter like '6/30/' & year(now()), it doesn't work. Is there any way to concatenate text and a variable at the input of a parameter query. THIS IS DRIVING ME NUTS. PLEASE HELP. :)
3
3129
by: thomas goodwin | last post by:
I have a query which asks for a parameter value to execute it. To see the results I have to: a) click on the query -- the "Enter Parameter Value" window pops up. b) enter the parameter value c) look at results in datasheet view If I want to run the query again, I must d) click to close the datasheet view e) click on the query f) enter the parameter value
3
1011
by: cassandra.flowers | last post by:
Hi, I was wondering if it is possible (Using access) to have a query parameter as a drop down box rather than a text box? e.g. typing as criteria for a query produces a box with a text box for you to enter the parameter. Can any body enlighten me on how to turn this text box into a drop down box? And whether it is possible?
4
5731
by: Andy Davis | last post by:
I have developed a number of reports that are based on parameter queries where the user enters criteria such as a date range and a sales rep say. I want to be able to show a graphical picture in the form of a bar chart based on the data within the report. Can I do this as when I have tried to insert a chart on the report it displays an error saying that it doeds not recognise the parameter criteria. Am I right to assume that maybe a chart...
1
2579
by: carrionk | last post by:
Hi, I have created a Subform which SourceObject is a parameter query. This is the Query: Qry Name:80IsscomProduct SELECT * FROM Isscomp28 WHERE Like ;
21
5773
by: Marc DVer | last post by:
I am trying to create a query that can be loaded as a querydef object but not having to assign values to the parameters if I don't want to. Normally when using a parameter query in VBA my code would go something like this: dim qry as dao.querydef set qry = currentdb.querydefs("myquery") qry.parameters("Par1") = "blah"
6
4484
by: tizmagik | last post by:
I am having a lot of difficulty generating a CrossTab Query based report. I have looked online for several tutorials and whatnot but I have not been able to really find what I'm looking for, nor have I been able to adapt other people's solutions/tips to fit what I need. If anyone could please help me with the following it would be really appreciated, thank you! I need to generate a Report (say: repCrossTab) that grabs it's data from the...
2
3267
by: billtubbs | last post by:
Hi I am getting the common problem of the "Enter Parameter Value" dialog box, which pops up every time I execute a query. I built the query using the Expression Builder in Access. If any of my expressions reference another column in the same query, it causes the dialog box to appear for each parameter before displaying the query output, even though no parameter values are required. For example, here are four expressions in my query: ...
20
5245
by: exipnakias | last post by:
Hello Guys. In a form I created a listbox which looks up the values of a table. I want: 1) ..to create a query where a parameter will be needed in order to be loaded. But I do not want to write the parameter, I want to select a row from the listbox and after clicking in a button, the query to automatically take the current value of listbox as the needed parameter and then to load the query. (The Inputbox for "Enter Parameter" will...
0
9512
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10419
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...
1
10147
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9987
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
9023
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
7531
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
5424
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5552
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4100
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

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.