473,394 Members | 1,902 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,394 software developers and data experts.

runsql command in acces VB return "Run time error 2342"

2
I'm executing the following procedure within Access mdb. The query that I'm trying to run works nolrmally when executed as Query.

Do anybody has any idea of reason of the error?.

Thanks a lot



Private Sub Form_BeforeInsert(Cancel As Integer)
Dim Istruz As Variant
Dim MaxOfVersione As Integer
Istruz = "SELECT Max(ControlloVersioniBDG.Versione) AS MaxOfVersione" _
& " FROM ControlloVersioniBDG" _
& " GROUP BY ControlloVersioniBDG.BDGType, ControlloVersioniBDG.Anno" _
& " HAVING (ControlloVersioniBDG.BDGType=[Forms]![F_BDG]![TipoBDG])" _
& " AND (ControlloVersioniBDG.Anno=[Forms]![F_BDG]![DetControlloVersioni])"

DoCmd.RunSQL Istruz
[Forms]![NewControlloVersioniBDG]![Versione].DefaultValue = MaxOfVersione + 1

End Sub
Aug 13 '09 #1
5 6231
ChipR
1,287 Expert 1GB
You Dim MaxOfVersione, but you never set it equal to anything. The "AS MaxOfVersione" in your query just creates a column with that title, and you don't do anything with it. You might want to save the query and then use the DMax() function, or the DLookup() function.
Aug 13 '09 #2
Stewart Ross
2,545 Expert Mod 2GB
Hi. Queries referring to form textboxes in their WHERE or HAVING clauses can cause run-time errors when run within the VBA environment. In particular, you may receive a 'too few parameters - expected 1' or somesuch error, which I think is what error 2342 is.

To resolve this, pass the value of the form control in the SQL statement, not the name of the control. If your form control holds a string value you need to pass its value inside single quotes, as shown in the HAVING clauses below:

Expand|Select|Wrap|Line Numbers
  1.  
  2. & " HAVING (ControlloVersioniBDG.BDGType = " _
  3. & "'" & [Forms]![F_BDG]![TipoBDG])  & "'"_
  4. & " AND (ControlloVersioniBDG.Anno = '" & [Forms]![F_BDG]![DetControlloVersioni] & ')"
  5.  
If the values are numbers and not strings you will not need the single quotes before and after the values.

-Stewart
Aug 13 '09 #3
ChipR
1,287 Expert 1GB
The error is actually due to using RunSQL for a query that is not an action query. RunSQL on a SELECT statement, nothing is going to happen. If you really want to do a SELECT, you could use DoCmd.OpenQuery, but I don't think that is the goal in this case.
Aug 13 '09 #4
stpgmn
2
Thankyou to all of you.

I realize that DoCmd.RunSQL doesn't works for SELECT query.

I change my procedure as follow using DoCmd.OpenQuery method and it works.
However, the query I execute return exactly what I expect, the question now is: how I can refer to 'MaxOfVersione' field returnd by the query. I have to put the query's result value (plus 1), to a field in a form.

Thanks again.

regards.
=========
Private Sub Form_BeforeInsert(Cancel As Integer)
Dim MaxOfVesrione As Integer


DoCmd.OpenQuery "FindMaxOfVersione", , acReadOnly
[Forms]![NewControlloVersioniBDG]![Versione].Value = [FindMaxOfVersione]![MaxOfVersione] + 1

DoCmd.Close acQuery, "FindMaxOfVersione"

End Sub
Aug 13 '09 #5
ChipR
1,287 Expert 1GB
My first post (Post #2) addresses this. There is no field returned by the query, so you need to save the query and use a function to grab the value.
Aug 13 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Francesc Guim Bernat | last post by:
Dear colleagues, i'm getting in troubles using one XML library with Visual Studio .NET and Xerces with Xalan. When i execute the code i get the next run time error: "Run-Time Check Failure #2...
0
by: Colleyville Alan | last post by:
My app is giving me this error. Run-time error 3211: The database engine could not lock table 'Sorted_Template' because it is already in use by another person or process. When I run the app...
2
by: pramodrepaka | last post by:
I am trying to print a form with the help of below code form1.printform but the above code gives me an error as "printer error"with an error no 482 printer is connected to lan and it is...
3
by: DontB3 | last post by:
Hi, I'm new in this forum, and i hope someone can help. I'm creating an automatic application that transfer a database from Access -> DBF -> Oracle. When My App try to execute Insert SQL...
0
by: mohamed82 | last post by:
Hi all. in my project using the inet controls for down loading zip file from site at the time error will occur for "unable to complete ur request" i think it will happening for the download more than...
1
by: mukeshgautam | last post by:
Hi Friends, I am transferring a access table into excel file, while transferring I am getting run time error '2391', and I am unable to resolve it, can any body help me why I am getting this...
5
by: mufc4life | last post by:
Hi, actually I'm creating a database for some coursework at school. It's basically about a booking system and how I book hardware in and out of the school for students who need the hardware. I've...
2
by: steve c | last post by:
Dim rs As New ADODB.Recordset Dim oCnn As ADODB.Connection Set oCnn = New ADODB.Connection 'Windows Integrated Security Login 'oCnn.ConnectionString = "provider=sqloledb;data source=intel;initial...
2
by: Laneyshia | last post by:
Hello Again. I have been proceeding through my code, thanks to you guys, and although this particular line may not be needed it is giving me this error that does not make sense. When asked 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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.