473,396 Members | 2,087 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,396 software developers and data experts.

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("qryMonthlyAccrualUpdate")
qryPar.Parameters("Month") = rs3!Month
DoCmd.OpenQuery "qryMonthlyAccrualUpdate", 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 20746
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("qryMonthlyAccrualUpdate")
qryPar.Parameters("Month") = rs3!Month
DoCmd.OpenQuery "qryMonthlyAccrualUpdate", 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!txtMonth = rs!Month
and in the Criteria of the form, use:
Forms!Form1!txtMonth
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.QueryDefs("qryMonthlyAccrualUpdate").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.googlegro ups.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("qryMonthlyAccrualUpdate")
qryPar.Parameters("Month") = rs3!Month
DoCmd.OpenQuery "qryMonthlyAccrualUpdate", 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("qryMonthlyAccrualUpdate")
qryPar.Parameters("Month") = rs3!Month
DoCmd.OpenQuery "qryMonthlyAccrualUpdate", 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("qryMonthlyAccrualUpdate")
qryPar.Parameters("Month") = rs3!Month
'----------------
qryPar.Execute "qryMonthlyAccrualUpdate"
'----------------
Mar 6 '07 #4
On Mar 5, 5:42 pm, rkc <r...@rkcny.yabba.dabba.do.comwrote:
>
Set qryPar = db.QueryDefs("qryMonthlyAccrualUpdate")
qryPar.Parameters("Month") = rs3!Month
'----------------
qryPar.Execute "qryMonthlyAccrualUpdate"
'----------------
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.yabba.dabba.do.comwrote:
> Set qryPar = db.QueryDefs("qryMonthlyAccrualUpdate")
qryPar.Parameters("Month") = rs3!Month
'----------------
qryPar.Execute "qryMonthlyAccrualUpdate"
'----------------

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.Execute 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.Execute 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
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...
5
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...
3
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)...
3
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...
4
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...
1
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
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...
6
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...
2
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...
20
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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
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...

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.