SQL in VBA for Access

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Flo100
    New Member
    • Jun 2007
    • 31

    SQL in VBA for Access

    Hi All,

    I have a simple query that I want to execute in VBA in Access. My form has a button and the following code exists in the click event of the button:

    Code:
    Dim db As DAO.Database
    Dim strSQL As String
    Set db = CurrentDb
       
    strSQL = "SELECT * FROM Project info Tbl;"
    db.Execute strSQL
    DoCmd.Close acForm, Me.Name
    Set db = Nothing

    But it gives me a Run Time Error 3131: Syntax error in FROM Clause.

    Can somebody help me on this. Very urgent. Many thanks.
  • LacrosseB0ss
    New Member
    • Oct 2006
    • 112

    #2
    Your table name can't have spaces. SQL looks at the statement this way:

    FROM - get ready for a table
    Project - ok, this is the table I want
    **standby for additional functions or where clause**

    Info - look for the info function. WTF!?!?!? I can't find it. Throw an error.

    My advice would be to change the table name to Project_Info_Ta ble or something.

    Hope this helps
    - LB

    Comment

    • JKing
      Recognized Expert Top Contributor
      • Jun 2007
      • 1206

      #3
      Hi, another alternative to changing your table name is to enclose the table name in square brackets. Square brackets are used in sql to handle spaces in field and table names.

      [code=vb]
      strSQL = "SELECT * FROM [Project info Tbl];"
      [/code]

      Comment

      • Flo100
        New Member
        • Jun 2007
        • 31

        #4
        I changed the name of the table and used it in the following 2 ways:

        First:

        strSQL = "SELECT * FROM Project_info_Tb l;"
        db.Execute strSQL

        Error: Cannot execute SELECT Query

        Second:

        strSQL = "SELECT * FROM Project_info_Tb l;"
        DoCmd.RunSQL strSQL
        DoCmd.Close acForm, Me.Name
        Set db = Nothing

        Error: A RunSQL action requires an argument consisting of an SQL statement

        Comment

        • Flo100
          New Member
          • Jun 2007
          • 31

          #5
          Originally posted by JKing
          Hi, another alternative to changing your table name is to enclose the table name in square brackets. Square brackets are used in sql to handle spaces in field and table names.

          [code=vb]
          strSQL = "SELECT * FROM [Project info Tbl];"
          [/code]

          I tried with the square brackets:

          strSQL = "SELECT * FROM [Project info Tbl];"
          DoCmd.RunSQL strSQL
          DoCmd.Close acForm, Me.Name
          Set db = Nothing

          But, it gives me the following error....

          Error: A RunSQL action requires an argument consisting of an SQL statement

          Comment

          • JKing
            Recognized Expert Top Contributor
            • Jun 2007
            • 1206

            #6
            RunSQL and Execute I believe can only be used with action queries.
            There are two things you can do to acheive your results

            1) Create and save a query that uses your SQL
            [code=vb]
            DoCmd.OpenQuery ("qryMyQuery ")
            DoCmd.Close acForm, Me.Name
            [/code]

            2) Create and save a query that you can modify on the fly
            [code=vb]
            Dim db as DAO.Database
            Dim qdf As DAO.QueryDef
            Dim strSQL As String

            Set db = CurrentDb
            Set qdf = db.QueryDefs("q ryMyQuery")

            strSQL = "SELECT * FROM [Project Name tbl];"
            qdf.SQL = strSQL

            DoCmd.OpenQuery ("qryMyQuery ")
            DoCmd.Close acForm, Me.Name

            Set db = nothing
            Set qdf = nothing
            [/code]

            Let me know how you make out with this.

            Comment

            • Flo100
              New Member
              • Jun 2007
              • 31

              #7
              Originally posted by JKing
              RunSQL and Execute I believe can only be used with action queries.
              There are two things you can do to acheive your results

              1) Create and save a query that uses your SQL
              [code=vb]
              DoCmd.OpenQuery ("qryMyQuery ")
              DoCmd.Close acForm, Me.Name
              [/code]

              2) Create and save a query that you can modify on the fly
              [code=vb]
              Dim db as DAO.Database
              Dim qdf As DAO.QueryDef
              Dim strSQL As String

              Set db = CurrentDb
              Set qdf = db.QueryDefs("q ryMyQuery")

              strSQL = "SELECT * FROM [Project Name tbl];"
              qdf.SQL = strSQL

              DoCmd.OpenQuery ("qryMyQuery ")
              DoCmd.Close acForm, Me.Name

              Set db = nothing
              Set qdf = nothing
              [/code]

              Let me know how you make out with this.

              I tried it too. But it is giving me the following error:

              "Run time error 3265 item not found in this collection".

              I am using Access 2002 and the databse is Access 200 file format.
              Does it have to do anything with the version of Access?

              Comment

              • Flo100
                New Member
                • Jun 2007
                • 31

                #8
                Originally posted by Flo100
                I tried it too. But it is giving me the following error:

                "Run time error 3265 item not found in this collection".

                I am using Access 2002 and the databse is Access 200 file format.
                Does it have to do anything with the version of Access?

                Correction: the database is in Access 2000 file format

                Comment

                • JKing
                  Recognized Expert Top Contributor
                  • Jun 2007
                  • 1206

                  #9
                  Did you replace qryMyQuery with the name of your query?

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32661

                    #10
                    There are a lot of right answers in here (I loved LB's explanation - sweet), but I'm afraid the question is unclear. Bugs were found (quite validly) but trying to understand what you're trying to achieve is not so easy.
                    Your SQL (after fixes) is a SELECT query, which means it will provide information.
                    Where do you need this information?
                    In your code or on the screen for the operator?
                    This makes a world of difference to the question.

                    Comment

                    • Flo100
                      New Member
                      • Jun 2007
                      • 31

                      #11
                      Originally posted by NeoPa
                      There are a lot of right answers in here (I loved LB's explanation - sweet), but I'm afraid the question is unclear. Bugs were found (quite validly) but trying to understand what you're trying to achieve is not so easy.
                      Your SQL (after fixes) is a SELECT query, which means it will provide information.
                      Where do you need this information?
                      In your code or on the screen for the operator?
                      This makes a world of difference to the question.
                      I need the answer(result of my query) on the screen. My task is to write an event for a command button which creates a query and opens the result for the user in datasheet view.

                      I hope this is clear. Just one more question please. What are the diference between Access 200, 2002 and 2003?

                      Comment

                      • NeoPa
                        Recognized Expert Moderator MVP
                        • Oct 2006
                        • 32661

                        #12
                        Originally posted by Flo100
                        I need the answer(result of my query) on the screen. My task is to write an event for a command button which creates a query and opens the result for the user in datasheet view.
                        In that case JKing is on the right lines.
                        Follow those instructions and let us know how you get on.

                        Comment

                        • NeoPa
                          Recognized Expert Moderator MVP
                          • Oct 2006
                          • 32661

                          #13
                          Originally posted by Flo100
                          I hope this is clear. Just one more question please. What are the differences between Access 2000, 2002 and 2003?
                          Just a small question to throw in then (LOL)
                          Check out this link (Converting Access versions)

                          Comment

                          • Flo100
                            New Member
                            • Jun 2007
                            • 31

                            #14
                            Originally posted by NeoPa
                            Just a small question to throw in then (LOL)
                            Check out this link (Converting Access versions)

                            I got my task done by saving the query and opening it using

                            Code:
                            CurrentDb.QueryDefs("qryAllIndex").SQL = strSQL
                            DoCmd.OpenQuery "qryAllIndex"
                            where "QueryAllIn dex" is name of a query and strSQL is the string that contains the query.

                            Thank you everybody..for your help.

                            Comment

                            • NeoPa
                              Recognized Expert Moderator MVP
                              • Oct 2006
                              • 32661

                              #15
                              Good for you. Pleased you got it sorted :)

                              Comment

                              Working...