Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2016
    Posts
    13

    SQL statement problem!

    Hi
    I want to use select query to show the history of transactions of each TapeID.
    I wrote the code in VBE as
    Code:
    Private Sub cmdHistory_Click()
        Dim SQL As String
        SQL = "SELECT tblTapeTransaction.Date,tblTapeTransaction.Time, tblTapeTransaction.AddOrSubtract " & _
                "FROM tblTapeTransaction WHERE (tblTapeTransaction.TapeID=[Forms]![frmTape]![TapeID]);"
        DoCmd.RunSQL SQL
    End Sub
    the code is error and VB error shows
    "Run-Time error '2342':
    A RunSQL action requires an argument consisting of an SQL statement."


    But I already have had an SQL and I can't find the error of my code.
    Anyone please advise.
    Thank you

  2. #2
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    you don't need to write any code. just make a query.
    (you cant use form paths in code like this, but you can in a query)
    The query builder will prevent errors.

  3. #3
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    A few other things:
    - Don't use reserved words like "SQL" as variables. This can cause ambiguity/confusion/errors. I often preface my variables with the prefix "my", i.e. "mySQL". Other people will use data type prefixes, i.e. "strSQL".
    - Running a Select query in VBA doesn't really do anything. If you want to return the results to the screen, you will want to assign the SQL code to a query object, and open that query object. Typically, you would use the RunSQL command on an Action Query that updates records.

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    "Date" and "Time" are also reserved words and shouldn't be used as object names (your field names).
    As stated, the RunSQL command (and Execute) only run Action Queries (Append, Update and Delete).


    Also, you need to concatenate the value from [Forms]![frmTape]![TapeID] to the SQL string.
    If "TapeID" is a number, thenit would look something like:
    Code:
    "FROM tblTapeTransaction WHERE (tblTapeTransaction.TapeID= " & [Forms]![frmTape]![TapeID] & ");"

  5. #5
    Join Date
    Apr 2016
    Posts
    13
    Thank you! I built a query named qryTapeHistory which has a criteria "=[Forms]![frmTape]![txtTapeID]" in the "TapeID" field
    on QBE grid and a command button that used the following code:
    Code:
    Private Sub cmdHistory_Click()
        DoCmd.OpenQuery "qryTapeHistory", , acReadOnly
    End Sub
    It works but I want to show the query by a dialog form view. The query that is shown is in a full view.
    How to?

  6. #6
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    It works but I want to show the query by a dialog form view. The query that is shown is in a full view.
    You are opening a Query, not a Form.

    If you want to mess around with different Form views, use the Query as the Data Source for a Form, and then open that Form.

  7. #7
    Join Date
    Apr 2016
    Posts
    13
    Hi JoeM,
    Yes. I have tried to build a form named "frmTapeHistory" that has "qryTapeHistory" as a data source.
    And I wrote
    Code:
    Private Sub cmdOpenFrmTapeHistory_Click()
        DoCmd.OpenForm "frmTapeHistory", , , , acFormReadOnly, acDialog
    End Sub
    to open the form. (the command button's on click event is in the main form "frmTape")
    But the form intentionally defaulted in Datasheet view is blank.
    I think the problem is the query "qryTapeHistory" which has the criteria
    "=[Forms]![frmTape]![txtTapeID]" in the "TapeID" field
    on QBE grid.
    I think that the criteria is wrong.
    The query does not link with the TapeID that I want to see.
    And I don't know how to write to link it.
    Please excuse me. I am a beginner.


  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Work through these tutorials: http://www.rogersaccesslibrary.com/forum/forum46.html

    They will help.....

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 11
    Last Post: 04-29-2015, 01:38 PM
  2. Problem with requery statement
    By crowegreg in forum Forms
    Replies: 2
    Last Post: 09-15-2014, 09:26 AM
  3. IF/THEN statement problem
    By sfgiantsdude in forum Access
    Replies: 5
    Last Post: 01-06-2012, 03:50 PM
  4. Problem with a IIF statement
    By Genzo in forum Access
    Replies: 10
    Last Post: 08-31-2011, 10:46 AM
  5. Problem With IF Statement
    By MuskokaMad in forum Programming
    Replies: 0
    Last Post: 03-14-2010, 05:26 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums