How to Run Parameter Query in VBA???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mr Key
    New Member
    • Aug 2010
    • 132

    How to Run Parameter Query in VBA???

    Hi all!!
    I have a simple database with 56-Queries that runs by passing parameters from specific forms. It works perfect!!!
    The problem is, I`m running out of space in the navigation pane, with 56 queries and more parameter queries to come.
    I would like to build parameter query in VBA so as to have user friendly database using the following code!
    Code:
    Private sub parameterquery()
    Dim stringSQL As DAO.Workspace   'Current workspace (for transaction).
      Dim db As DAO.Database    'Inside the transaction.
      Dim bInTrans As Boolean   'Flag that transaction is active.
      Dim stringSql11 As String      'Action query statements.
        Dim strMsg As String      'MsgBox message.
        'Step 1: Initialize database object inside a transaction.
      Set stringSQL = DBEngine(0)
      stringSQL.BeginTrans
      bInTrans = True
      Set db = stringSQL(0)
    stringSql11 = "Delete tblMytableName.Field1, tblMytableName.Field2, tblMytableName.Field3 FROM tblMytableName WHERE (((tblMytableName.Field1)=Me![FormsField1]));"
    db.Execute stringSql11, dbFailOnError
    stringSQL.CommitTrans
    End Sub
    when running this code, it gives RunTime error 3061 "Too Few Parameters Expected 1"
    Where am I doing Wrong???
    Please help!!!!!
    Last edited by Mr Key; Dec 23 '11, 01:41 PM. Reason: Layout editing
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32634

    #2
    Your SQL is invalid. It refers to Me, which only has a meaning within report or form object modules - not in SQL.

    Also, please read How to Debug SQL String so you know how to post such questions in future.

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      What are these 56 queries? I suspect they are similar with a slight change.

      Comment

      • Mr Key
        New Member
        • Aug 2010
        • 132

        #4
        Thanks Neo and Rabbit, the queries runs with respect to the forms, the key word Me refers to the current Fields in a forms that referenced by the query. May be it is not the right place to be thus why I have asked for your hint!!!
        Rabbit, these queries varies in structures and differ one another; I have Append queries, Delete Queries, Update queries and simple queries, but all of them runs with parameters taken from either FormsComboBox or formstextBox.
        The programs is running perfectly but I want to improve by adding more queries and thus am running out of space in the navigation pane and is very hard to spot the query in the pane.
        Please help!!!

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32634

          #5
          Originally posted by Mr Key
          Mr Key:
          May be it is not the right place to be thus why I have asked for your hint!!!
          Indeed. But when I gave you a hint (two in fact) you seem to have ignored it (them).

          Reading the linked article and following the instructions there will help to save you wasting so much time on things which are easily handled by following those instructions. From there, we might have something a little more sensible to work with than VBA code that creates what you think ought to be SQL.

          Comment

          • Mr Key
            New Member
            • Aug 2010
            • 132

            #6
            Ok thanks NeoPa!
            You always have constructive ideas but very hard to grasp!!!
            I have read the article as suggested but coldnt solve my problem.
            I have a parameter query which is working properly so far, I just want to change the design towards VBA from ordinary queries. Should I run my first SQL through VBA then I can go ahead debugging my SQL string!!!!
            You know, I can make it on my own should there be no choice behind but it might take longer than under your assistance or hint!

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32634

              #7
              The first thing I'd do, if I were you, would be to use the linked article to get the exact contents of the SQL string immediately prior to execution posted here in this thread. That way we are looking directly at the SQL and don't need to get ourselves confused with VBA things.

              Comment

              • Mihail
                Contributor
                • Apr 2011
                • 759

                #8
                Hi !
                Mr Key take a look here. Maybe this is also an answer for you:

                Comment

                • Mr Key
                  New Member
                  • Aug 2010
                  • 132

                  #9
                  Thanks Mihal and Neopa for your time!
                  I will work it out for two days and post the answer right here!

                  Comment

                  Working...