Help constructing a SQL String

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • salzan
    New Member
    • Feb 2008
    • 38

    Help constructing a SQL String

    I developed a query in query window and copied the sql statement as follows in my code...
    Code:
    strSQL = "SELECT tblCategory.CatName, " & _
                    "tblDepartment.DeptName, " & _
                    "tblProject.ProjName, " & _
                    "tblResource.ResName, " & _
                    "tblResProj.StartDate, " & _
                    "tblResProj.EndDate, " & _
                    "blResProj.TotalHrs, " & _
                    "tblResProj.Hours, " & _
                    "tblResProj.TypeHrs " & _
    "FROM (tblDepartment INNER JOIN tblResource ON " & _
          "tblDepartment.DeptId = tblResource.DeptId) " & _
    "INNER JOIN ((tblCategory INNER JOIN tblProject ON " & _
                 "tblCategory.CatId = tblProject.CatId) " & _
    "INNER JOIN tblResProj ON tblProject.ProjId = tblResProj.ProjId) ON " & _
                             "tblResource.ResId = tblResProj.ResId " & _
    "WHERE tblResProj.TypeHrs = 'F';"
    
    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
  • Scott Price
    Recognized Expert Top Contributor
    • Jul 2007
    • 1384

    #2
    Your code should look something like this:

    Code:
    Dim db As Database
    Dim rs As Recordset
    
    Set db = CurrentDb()
    
    Set rs = db.OpenRecordset(strSQL)
    I haven't gone to the trouble of copying in your strSQL variable statement, which you can do in your database.

    Regards,
    Scott

    Comment

    • salzan
      New Member
      • Feb 2008
      • 38

      #3
      This I how I'm doing it.
      [CODE=vb]
      Dim cnConnect As ADODB.Connectio n
      Dim rsInput As ADODB.Recordset
      Dim strConnect As String
      strConnect = "Provider=Micro soft.Jet.OLEDB. 4.0;" & _
      "Data Source=" & _
      CurrentProject. Path & _
      "\" & _
      CurrentProject. Name & _
      ";"
      Set cnConnect = New ADODB.Connectio n
      cnConnect.Open strConnect

      Set rsInput = New ADODB.Recordset
      With rsInput
      .CursorType = adOpenDynamic
      .CursorLocation = adUseServer
      .LockType = adLockOptimisti c
      End With

      strSQL = "SELECT tblCategory.Cat Name, " & _
      "tblDepartment. DeptName, " & _
      "tblProject.Pro jName, " & _
      "tblResource.Re sName, " & _
      "tblResProj.Sta rtDate, " & _
      "tblResProj.End Date, " & _
      "blResProj.Tota lHrs, " & _
      "tblResProj.Hou rs, " & _
      "tblResProj.Typ eHrs " & _
      "FROM (tblDepartment INNER JOIN tblResource ON " & _
      "tblDepartment. DeptId = tblResource.Dep tId) " & _
      "INNER JOIN ((tblCategory INNER JOIN tblProject ON " & _
      "tblCategory.Ca tId = tblProject.CatI d) " & _
      "INNER JOIN tblResProj ON tblProject.Proj Id = tblResProj.Proj Id) ON " & _
      "tblResource.Re sId = tblResProj.ResI d " & _
      "WHERE tblResProj.Type Hrs = 'F';"

      rsInput.Open strSQL, cnConnect[/CODE]
      Last edited by Scott Price; Feb 19 '08, 02:30 AM. Reason: code tags

      Comment

      • salzan
        New Member
        • Feb 2008
        • 38

        #4
        I also tried it your way and I got another error message: 3061 too many paramaters. Expected 1.

        Comment

        • Scott Price
          Recognized Expert Top Contributor
          • Jul 2007
          • 1384

          #5
          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:

          Code:
          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

          Comment

          • MMcCarthy
            Recognized Expert MVP
            • Aug 2006
            • 14387

            #6
            Salsan

            The connection looks fine. Have you tried running your query in the query window to see if any results are returned?

            Comment

            • MMcCarthy
              Recognized Expert MVP
              • Aug 2006
              • 14387

              #7
              Originally posted by msquared
              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.

              Comment

              • salzan
                New Member
                • Feb 2008
                • 38

                #8
                The query works as I constructed it in query window.

                Comment

                • FishVal
                  Recognized Expert Specialist
                  • Jun 2007
                  • 2656

                  #9
                  Hi, salzan.

                  I would like to suggest you not to open new ADODB.Connectio n 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.Connectio n may be rejected.

                  [CODE=vb]
                  Dim rsInput As ADODB.Recordset

                  Set rsInput = New ADODB.Recordset
                  With rsInput
                  .CursorType = adOpenDynamic
                  .CursorLocation = adUseServer
                  .LockType = adLockOptimisti c
                  End With

                  strSQL = "SELECT tblCategory.Cat Name, " & _
                  "tblDepartment. DeptName, " & _
                  "tblProject.Pro jName, " & _
                  "tblResource.Re sName, " & _
                  "tblResProj.Sta rtDate, " & _
                  "tblResProj.End Date, " & _
                  "blResProj.Tota lHrs, " & _
                  "tblResProj.Hou rs, " & _
                  "tblResProj.Typ eHrs " & _
                  "FROM (tblDepartment INNER JOIN tblResource ON " & _
                  "tblDepartment. DeptId = tblResource.Dep tId) " & _
                  "INNER JOIN ((tblCategory INNER JOIN tblProject ON " & _
                  "tblCategory.Ca tId = tblProject.CatI d) " & _
                  "INNER JOIN tblResProj ON tblProject.Proj Id = tblResProj.Proj Id) ON " & _
                  "tblResource.Re sId = tblResProj.ResI d " & _
                  "WHERE tblResProj.Type Hrs = 'F';"

                  rsInput.Open strSQL, CurrentProject. Connection[/CODE]

                  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.

                  Comment

                  • salzan
                    New Member
                    • Feb 2008
                    • 38

                    #10
                    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 :)

                    Comment

                    • salzan
                      New Member
                      • Feb 2008
                      • 38

                      #11
                      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.

                      Comment

                      • puppydogbuddy
                        Recognized Expert Top Contributor
                        • May 2007
                        • 1923

                        #12
                        Originally posted by salzan
                        The query works as I constructed it in query window.
                        I think your problem could be your connection string.
                        See this link:
                        404 - Page Not Found. Shown when a URL cannot be mapped to any kind of resource.

                        Comment

                        • puppydogbuddy
                          Recognized Expert Top Contributor
                          • May 2007
                          • 1923

                          #13
                          Sorry, did not refresh my browser. Good job Fish!

                          Comment

                          • FishVal
                            Recognized Expert Specialist
                            • Jun 2007
                            • 2656

                            #14
                            Not a problem.
                            Good luck and happy coding.

                            Best regards,
                            Fish

                            Comment

                            Working...