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

Update query doesn't work

Hi All,

I was trying to update a field in a table, based on the results from a
query.
The table to be updated is tblCustomers and the query is qrySelect. This
query has two parameters, provided by a form.
I created an update query called qryUpdate, which has both tblCustomers and
qrySelect (linked by CustomerID).
And I try to update one of the fields in tblCustomers.
If I open the form, and I click on a button, I can open qrySelect and I get
the records I am interested in. Then I can go in qryUpdate and if I execute
it manually, everything works fine.
But in the moment I try to execute it from code, I get the error: "Too few
parameters.Expected 2"

Regards,
Nicolae

======================================

' Here is the code I am using:

Private Sub btnMailMerge_Click()
On Error GoTo Err_btnMailMerge_Click

Dim oApp As Object
Dim strDocName As String

Set db = CurrentDb

Set oApp = CreateObject("Word.Application")

Set qdf = db.QueryDefs("qryClearSelectedForMailout")
qdf.Execute dbFailOnError

Dim dtDate As Date
Dim sBrand As String

sBrand = [Forms]![frmCustomersByBrandAndDate]![cboBrand]
dtDate = [Forms]![frmCustomersByBrandAndDate]![DTPicker]

strQueryName = "qryCustByBrandAndEnterDate" ' this query works by itself
strUpdateQueryName = "qryUpdateCustByBrandAndEnterDate" ' this query works
by itself

DoCmd.OpenQuery strQueryName, acNormal, acEdit 'this one works ok

Set qdf = db.QueryDefs(strUpdateQueryName)
'qdf.Parameters("cboBrand") = sBrand ' if I provide these parameters, it
will come up with no results
'qdf.Parameters("DTPicker") = dtDate
qdf.Execute dbFailOnError ' *********************** This is the line
which raises the error about too few parameters

strDocName = "C:/Databases/Docs/MailMerge.doc"

oApp.Visible = True
oApp.Application.Documents.Open strDocName
Set oApp = Nothing

Exit_btnMailMerge_Click:
Exit Sub

Err_btnMailMerge_Click:
MsgBox Err.Description
Resume Exit_btnMailMerge_Click
End Sub
Nov 13 '05 #1
6 3231
You didn't Dim qdf as an object variable.
It's hard to tell about the rest of it.

- Jim

On Fri, 9 Jul 2004 15:11:24 +1000, "Nicolae Fieraru"
<no****@please.cxm> wrote:
Hi All,

I was trying to update a field in a table, based on the results from a
query.
The table to be updated is tblCustomers and the query is qrySelect. This
query has two parameters, provided by a form.
I created an update query called qryUpdate, which has both tblCustomers and
qrySelect (linked by CustomerID).
And I try to update one of the fields in tblCustomers.
If I open the form, and I click on a button, I can open qrySelect and I get
the records I am interested in. Then I can go in qryUpdate and if I execute
it manually, everything works fine.
But in the moment I try to execute it from code, I get the error: "Too few
parameters.Expected 2"

Regards,
Nicolae

======================================

' Here is the code I am using:

Private Sub btnMailMerge_Click()
On Error GoTo Err_btnMailMerge_Click

Dim oApp As Object
Dim strDocName As String

Set db = CurrentDb

Set oApp = CreateObject("Word.Application")

Set qdf = db.QueryDefs("qryClearSelectedForMailout")
qdf.Execute dbFailOnError

Dim dtDate As Date
Dim sBrand As String

sBrand = [Forms]![frmCustomersByBrandAndDate]![cboBrand]
dtDate = [Forms]![frmCustomersByBrandAndDate]![DTPicker]

strQueryName = "qryCustByBrandAndEnterDate" ' this query works by itself
strUpdateQueryName = "qryUpdateCustByBrandAndEnterDate" ' this query works
by itself

DoCmd.OpenQuery strQueryName, acNormal, acEdit 'this one works ok

Set qdf = db.QueryDefs(strUpdateQueryName)
'qdf.Parameters("cboBrand") = sBrand ' if I provide these parameters, it
will come up with no results
'qdf.Parameters("DTPicker") = dtDate
qdf.Execute dbFailOnError ' *********************** This is the line
which raises the error about too few parameters

strDocName = "C:/Databases/Docs/MailMerge.doc"

oApp.Visible = True
oApp.Application.Documents.Open strDocName
Set oApp = Nothing

Exit_btnMailMerge_Click:
Exit Sub

Err_btnMailMerge_Click:
MsgBox Err.Description
Resume Exit_btnMailMerge_Click
End Sub


Nov 13 '05 #2
"Nicolae Fieraru" <no****@please.cxm> wrote in message news:<40********@duster.adelaide.on.net>...
Hi All,

I was trying to update a field in a table, based on the results from a
query.
The table to be updated is tblCustomers and the query is qrySelect. This
query has two parameters, provided by a form.
I created an update query called qryUpdate, which has both tblCustomers and
qrySelect (linked by CustomerID).
And I try to update one of the fields in tblCustomers.
If I open the form, and I click on a button, I can open qrySelect and I get
the records I am interested in. Then I can go in qryUpdate and if I execute
it manually, everything works fine.
But in the moment I try to execute it from code, I get the error: "Too few
parameters.Expected 2"

Regards,
Nicolae


Private Sub cmdTest_Click()
Dim db As Database
Dim qdf As QueryDef
Dim strUpdateQueryName As String

Set db = CurrentDb
strUpdateQueryName = "qryUpdateCustByBrandAndEnterDate"
Set qdf = db.QueryDefs(strUpdateQueryName)
qdf.Parameters("cboBrand") = cboBrand.Value
qdf.Parameters("DTPicker") = DTPicker.Value
qdf.Execute dbFailOnError
MsgBox("Done.")
End Sub

This code allowed updates for me with qryCustByBrandAndEnterDate.SQL =
"PARAMETERS DTPicker DateTime, cboBrand Text; UPDATE tblCustomers SET
tblCustomers.theDate = [DTPicker], tblCustomers.Brand = [cboBrand];"

Maybe having the same name for both controls and parameters was
causing the problem. Also, perhaps using the querydef.Execute to run
the query is not a good way to do it since I have never run into this
problem when using DoCmd.RunSQL strSQL or MyDB.Execute strSQL and the
values were sitting on the form.

James A. Fortune
Nov 13 '05 #3
Hi Jim,

Thank you very much for your comments. You are right about that. I forgot to
set Option Explicit. But that didn't solve the problem.

Regards,
Nicolae

"Jim Allensworth" <ji****@datacentricsolutions.com> wrote in message
news:40****************@news.west.earthlink.net...
You didn't Dim qdf as an object variable.


Nov 13 '05 #4
Hi James,

Thank you very much, you are a lifesaver :-)
Based on your suggestion, I solved the problem. I changed the parameters
names in the query. I am not sure yet exactly what was the problem, but the
fact that you told me the code you created worked for you helped me to solve
mine as well.
There was no need for the .Value
I don't think there is much difference between running a query or an sql
string. I suppose that when I run an existing query via qdf.Execute, this
command gets the query SQL string as well. Besides, I consider it is much
easier to use the query designer to modify the query, instead of creating
the query by hand (I have problems understanding the Left Join, etc :-) ).
I think that my mistake was that I was trying to use in the query value
straight from the form for parameters. There is no need for that, since I
can provide them when I call the query from VBA.

Best regards,
Nicolae
"James Fortune" <ja******@oakland.edu> wrote in message
news:a6**************************@posting.google.c om...
"Nicolae Fieraru" <no****@please.cxm> wrote in message news:<40********@duster.adelaide.on.net>...

Private Sub cmdTest_Click()
Dim db As Database
Dim qdf As QueryDef
Dim strUpdateQueryName As String

Set db = CurrentDb
strUpdateQueryName = "qryUpdateCustByBrandAndEnterDate"
Set qdf = db.QueryDefs(strUpdateQueryName)
qdf.Parameters("cboBrand") = cboBrand.Value
qdf.Parameters("DTPicker") = DTPicker.Value
qdf.Execute dbFailOnError
MsgBox("Done.")
End Sub

This code allowed updates for me with qryCustByBrandAndEnterDate.SQL =
"PARAMETERS DTPicker DateTime, cboBrand Text; UPDATE tblCustomers SET
tblCustomers.theDate = [DTPicker], tblCustomers.Brand = [cboBrand];"

Maybe having the same name for both controls and parameters was
causing the problem. Also, perhaps using the querydef.Execute to run
the query is not a good way to do it since I have never run into this
problem when using DoCmd.RunSQL strSQL or MyDB.Execute strSQL and the
values were sitting on the form.

James A. Fortune

Nov 13 '05 #5
Setting Option Explicit had nothing to do with my comment! However it
is a recommended thing to do.

I simply noted that you did not have the object variable dimmed in the
procedure. Perhaps it was a module level variable - which seems kind
of strange. At any rate it sounds like you have solved your problem -
glad to hear it.

- Jim

On Mon, 12 Jul 2004 10:01:18 +1000, "Nicolae Fieraru"
<no****@please.cxm> wrote:
Hi Jim,

Thank you very much for your comments. You are right about that. I forgot to
set Option Explicit. But that didn't solve the problem.

Regards,
Nicolae

"Jim Allensworth" <ji****@datacentricsolutions.com> wrote in message
news:40****************@news.west.earthlink.net.. .
You didn't Dim qdf as an object variable.



Nov 13 '05 #6
"Nicolae Fieraru" <no****@please.cxm> wrote in message news:<40******@duster.adelaide.on.net>...
Hi James,
...
There was no need for the .Value
It's good practice to put the .Value in so that if the default for the
combobox changes in a future version of Access the code will survive
the conversion process providing .Value still exists.
I don't think there is much difference between running a query or an sql
string. I suppose that when I run an existing query via qdf.Execute, this
command gets the query SQL string as well. Besides, I consider it is much
easier to use the query designer to modify the query, instead of creating
the query by hand (I have problems understanding the Left Join, etc :-) ).
I think that my mistake was that I was trying to use in the query value
straight from the form for parameters. There is no need for that, since I
can provide them when I call the query from VBA.


In theory there's no difference between theory and practice, but in
practice there is. --- Rebecca Riordan

I'm glad you found something that works for you.

James A. Fortune
Nov 13 '05 #7

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

Similar topics

6
by: David Shorthouse | last post by:
Hello folks, I have a problem with an update query on an asp not updating the table in an Access db. The code runs, I have no errors, but when I examine the table, nothing was updated. The query...
17
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
2
by: Mike Leahy | last post by:
Hello all, This question is related to updating tables - is there any way to calculate or update the values in a column in a table to the values in a field produced by a query result? An...
10
by: Randy Harris | last post by:
I imported records into a table, later found out that many of them had trailing spaces in one of the fields. If I'd caught it sooner, I could have trimmed the spaces before the import. This...
17
by: Benoit Martin | last post by:
I'm working on a project in VB.net connecting to a SQL Server 2000 database that I can't modify I created a dataset with a schema identical to the DB. When trying to update the DB from the dataset...
9
by: P3Eddie | last post by:
Hello all! I don't know if this can even be done, but I'm sure you will either help or suggest another avenue to accomplish the same. My problem may be a simple find duplicates / do something...
16
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
0
by: SuzK | last post by:
I am trying to calculate in VBA in Access 2002 a moving average and update a table with the calculations. Fields in my WeeklyData table are Week Ending (date) ItemNbr (double) Sales Dollars...
4
by: dougmeece | last post by:
Morning Everyone... I have a table that needs to be append to and also updated. All the fields in the table are populated with data from the text boxes and combo boxes on a form. The Date...
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: 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: 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
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
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
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.