473,396 Members | 1,963 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.

Help constructing a SQL String

38
I developed a query in query window and copied the sql statement as follows in my code...
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT tblCategory.CatName, " & _
  2.                 "tblDepartment.DeptName, " & _
  3.                 "tblProject.ProjName, " & _
  4.                 "tblResource.ResName, " & _
  5.                 "tblResProj.StartDate, " & _
  6.                 "tblResProj.EndDate, " & _
  7.                 "blResProj.TotalHrs, " & _
  8.                 "tblResProj.Hours, " & _
  9.                 "tblResProj.TypeHrs " & _
  10. "FROM (tblDepartment INNER JOIN tblResource ON " & _
  11.       "tblDepartment.DeptId = tblResource.DeptId) " & _
  12. "INNER JOIN ((tblCategory INNER JOIN tblProject ON " & _
  13.              "tblCategory.CatId = tblProject.CatId) " & _
  14. "INNER JOIN tblResProj ON tblProject.ProjId = tblResProj.ProjId) ON " & _
  15.                          "tblResource.ResId = tblResProj.ResId " & _
  16. "WHERE tblResProj.TypeHrs = 'F';"
  17.  
  18. rsInput.Open strSQL, cnConnect
When I open the recordset I get an error message: No Value given for one or more required parameters. I should add that the query works in the query window. Also, is there a direct way to open a query in VBA so I can step through the recordset?

Thank you in advance.
Salzan
Feb 19 '08 #1
13 1706
Scott Price
1,384 Expert 1GB
Your code should look something like this:

Expand|Select|Wrap|Line Numbers
  1. Dim db As Database
  2. Dim rs As Recordset
  3.  
  4. Set db = CurrentDb()
  5.  
  6. Set rs = db.OpenRecordset(strSQL)
  7.  
  8.  
I haven't gone to the trouble of copying in your strSQL variable statement, which you can do in your database.

Regards,
Scott
Feb 19 '08 #2
salzan
38
This I how I'm doing it.
Expand|Select|Wrap|Line Numbers
  1. Dim cnConnect As ADODB.Connection
  2. Dim rsInput As ADODB.Recordset
  3. Dim strConnect As String
  4. strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  5.              "Data Source=" & _
  6.              CurrentProject.Path & _
  7.              "\" & _
  8.              CurrentProject.Name & _
  9.              ";"
  10. Set cnConnect = New ADODB.Connection
  11. cnConnect.Open strConnect
  12.  
  13. Set rsInput = New ADODB.Recordset
  14. With rsInput
  15.     .CursorType = adOpenDynamic
  16.     .CursorLocation = adUseServer
  17.     .LockType = adLockOptimistic
  18. End With
  19.  
  20. strSQL = "SELECT tblCategory.CatName, " & _
  21. "tblDepartment.DeptName, " & _
  22. "tblProject.ProjName, " & _
  23. "tblResource.ResName, " & _
  24. "tblResProj.StartDate, " & _
  25. "tblResProj.EndDate, " & _
  26. "blResProj.TotalHrs, " & _
  27. "tblResProj.Hours, " & _
  28. "tblResProj.TypeHrs " & _
  29. "FROM (tblDepartment INNER JOIN tblResource ON " & _
  30. "tblDepartment.DeptId = tblResource.DeptId) " & _
  31. "INNER JOIN ((tblCategory INNER JOIN tblProject ON " & _
  32. "tblCategory.CatId = tblProject.CatId) " & _
  33. "INNER JOIN tblResProj ON tblProject.ProjId = tblResProj.ProjId) ON " & _
  34. "tblResource.ResId = tblResProj.ResId " & _
  35. "WHERE tblResProj.TypeHrs = 'F';"
  36.  
  37. rsInput.Open strSQL, cnConnect
Feb 19 '08 #3
salzan
38
I also tried it your way and I got another error message: 3061 too many paramaters. Expected 1.
Feb 19 '08 #4
Scott Price
1,384 Expert 1GB
Hi Salzan,

I'm sorry that I missed you were using an ADODB connection. When using the OpenRecordset() method you can specify either a connection object or a database object to use.

You can try using this:

Expand|Select|Wrap|Line Numbers
  1. Set rsInput = cnConnect.OpenRecordset(strSQL)
However, I think you are running into a different problem. I have no way of testing the connection code (and am not an expert on this area of VBA!) so if the above doesn't work, I'll call in some of our other experts who deal with this more.

Regards,
Scott
Feb 19 '08 #5
MMcCarthy
14,534 Expert Mod 8TB
Salsan

The connection looks fine. Have you tried running your query in the query window to see if any results are returned?
Feb 19 '08 #6
MMcCarthy
14,534 Expert Mod 8TB
Salsan

The connection looks fine. Have you tried running your query in the query window to see if any results are returned?
Sorry, just reread your initial post.
Feb 19 '08 #7
salzan
38
The query works as I constructed it in query window.
Feb 19 '08 #8
FishVal
2,653 Expert 2GB
Hi, salzan.

I would like to suggest you not to open new ADODB.Connection but use CurrentProject.Connection to get one already opened.
Really, when you modify code and before the module where you make changes is saved the database is in locked state and ADODB.Connection may be rejected.

Expand|Select|Wrap|Line Numbers
  1. Dim rsInput As ADODB.Recordset
  2.  
  3. Set rsInput = New ADODB.Recordset
  4. With rsInput
  5.     .CursorType = adOpenDynamic
  6.     .CursorLocation = adUseServer
  7.     .LockType = adLockOptimistic
  8. End With
  9.  
  10. strSQL = "SELECT tblCategory.CatName, " & _
  11. "tblDepartment.DeptName, " & _
  12. "tblProject.ProjName, " & _
  13. "tblResource.ResName, " & _
  14. "tblResProj.StartDate, " & _
  15. "tblResProj.EndDate, " & _
  16. "blResProj.TotalHrs, " & _
  17. "tblResProj.Hours, " & _
  18. "tblResProj.TypeHrs " & _
  19. "FROM (tblDepartment INNER JOIN tblResource ON " & _
  20. "tblDepartment.DeptId = tblResource.DeptId) " & _
  21. "INNER JOIN ((tblCategory INNER JOIN tblProject ON " & _
  22. "tblCategory.CatId = tblProject.CatId) " & _
  23. "INNER JOIN tblResProj ON tblProject.ProjId = tblResProj.ProjId) ON " & _
  24. "tblResource.ResId = tblResProj.ResId " & _
  25. "WHERE tblResProj.TypeHrs = 'F';"
  26.  
  27. rsInput.Open strSQL, CurrentProject.Connection
If that doesn't help try to open RecordSet with default CursorLocation property.
If after that you still get an error, then the problem is in SQL expression, no matter you've built it in query editor - get a runtime value of strSQL and try to execute it in query editor window.

Regards,
Fish.
Feb 19 '08 #9
salzan
38
I tried both of your suggestions and neither of them worked. this problem has mr bafold. Anybody out there that can help, I'm willing to ship a case of beer :)
Feb 19 '08 #10
salzan
38
IT WORKED. THe Problem was with the sql string. Your suggestion to get a runtime value for the strSQL and try it in a query window led me to the problem.

Thank you very much.
Feb 19 '08 #11
puppydogbuddy
1,923 Expert 1GB
The query works as I constructed it in query window.
I think your problem could be your connection string.
See this link:
http://www.connectionstrings.com/def...carrier=access
Feb 19 '08 #12
puppydogbuddy
1,923 Expert 1GB
Sorry, did not refresh my browser. Good job Fish!
Feb 19 '08 #13
FishVal
2,653 Expert 2GB
Not a problem.
Good luck and happy coding.

Best regards,
Fish
Feb 19 '08 #14

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

Similar topics

7
by: Ben Thomas | last post by:
Hi all, I'm having some trouble understanding the behavior of std::ostringstream. (I'm using Visual Studio .Net & STL port 4.5.3). I'll appreciate if someone can give me a little explanation of...
5
by: bjorgenson | last post by:
Here is my dilema: I want a form that includes an input box and submit button. A user will type in some data in the input box. When they click submit, I need set variables appended to this input...
5
by: grocery_stocker | last post by:
Why would someone go through the trouble of constructing an error handling function using variable-length argument lists? Why not just use something like printf()?
2
by: Jeremy | last post by:
I'm having some problems with using Server.Transfer and I am hoping somebody can help me. Page 1 has a datagrid, Page 2 has a form. If a user goes to page 1 and selects something out of the...
3
by: MattB | last post by:
Not sure if this is a good approach or not, but I'd like to hear some informed opinions. I've designed an ECommerce site that interfaces with my company's POS system. I'm trying to make it as...
31
by: Extremest | last post by:
I have a loop that is set to run as long as the arraylist is > 0. at the beginning of this loop I grab the first object and then remove it. I then go into another loop that checks to see if there...
8
by: Dip | last post by:
Hello Experts, Here is the code to flatten a PC hierarchy into a level based table. It works fine. SELECT t1.TASK_ID AS TASK_LV1, t2.TASK_ID AS TASK_LV2, t3.TASK_ID AS TASK_LV3, t4.TASK_ID AS...
6
by: Big George | last post by:
Hello, I develop in ASP.NET with VB.NET code. I need some help constructing a class: Worker. I'm designing the properties of this class. The class is filled reading tables in database. ...
8
by: CCLeasing | last post by:
I have a variable string, called formatstring The general format of the string is : - "somevariable + "some string text" + some variable2 + "some more text" .... " I want to use this variable...
4
by: perryschon | last post by:
Can someone please help me out with the Visual Basic source code needed that allows configuration and usage of comm ports in any PC? I am constructing an asynchronous serial communication RS-232...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.