Results 1 to 7 of 7
  1. #1
    coach32 is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Aug 2011
    Posts
    47

    Using VBA to run SQL for a make table query

    I am try to write VBA to run SQL that will create a new table based on the criteria.
    I have the following code, but it I get a run-time error '3067': Query input must contain at least one table or query.
    Code:
    DoCmd.RunSQL ("SELECT tbl_Scores.RAN, tbl_Scores.TEAM, tbl_Scores.DVN, tbl_Scores.FOR, tbl_Scores.RLT, tbl_Scores.AGT INTO 1963" & _
    "FROM tbl_Scores" & _
    "WHERE (((tbl_Scores.RAN)<19) AND ((tbl_Scores.YEAR)=1963) AND ((tbl_Scores.AGE)=15));")
    What I am trying to do is take scores less than 19, kicked in 1963 by team 15 from 'tbl_Scores' and create a table name 1963 with those results.


    Could someone please tell me what I am doing incorrectly?

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    You do not have proper spacing between your lines of VBA. The following includes a space prior to closing quotes.

    Code:
    Dim strSQL As String
    
    strSQL = "SELECT tbl_Scores.RAN, tbl_Scores.TEAM, tbl_Scores.DVN, tbl_Scores.FOR, tbl_Scores.RLT, tbl_Scores.AGT INTO 1963 " & _
                  "FROM tbl_Scores " & _
                  "WHERE (((tbl_Scores.RAN)<19) AND ((tbl_Scores.YEAR)=1963) AND ((tbl_Scores.AGE)=15));"
    
    Debug.Print strSQL

  3. #3
    coach32 is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Aug 2011
    Posts
    47
    Thank you very much ItsMe.
    I have copied that code exactly as written, but nothing is happening.
    I guess it will have something to do with how I have tried to execute it, but I'm at a loss.
    I have placed the code as follows:
    Code:
    Private Sub Command57_Click()
    Dim strSQL As String
    strSQL = "SELECT tbl_Scores.RAN, tbl_Scores.TEAM, tbl_Scores.DVN, tbl_Scores.FOR, tbl_Scores.RLT, tbl_Scores.AGT INTO 1963 " & _
                  "FROM tbl_Scores " & _
                  "WHERE (((tbl_Scores.RAN)<19) AND ((tbl_Scores.YEAR)=1963) AND ((tbl_Scores.AGE)=15));"
    Debug.Print strSQL
    
    End Sub
    Is it okay to apply to a command button, or must I apply it some other way?

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716
    It should Print the SQL statement to the immediate window when you click the button.

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    As mentioned, the code example I provided will display the value of the string variable I declared in the immediate window. The variable name is strSQL. A keyboard shortcut to open the immediate window is CTRL + G

    If the value of strSQL is correct, you should be able to use it in your DoCmd method.
    DoCmd.RunSQL strSQL

  6. #6
    coach32 is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Aug 2011
    Posts
    47
    Thank you very much for your help. It has helped me greatly.

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Glad to hear things are moving in a positive direction.

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

Similar Threads

  1. Replies: 2
    Last Post: 08-12-2016, 07:00 AM
  2. Replies: 2
    Last Post: 02-17-2015, 01:01 PM
  3. Replies: 1
    Last Post: 03-11-2014, 11:30 AM
  4. Replies: 1
    Last Post: 07-30-2010, 10:28 AM
  5. Replies: 2
    Last Post: 10-27-2009, 07:09 AM

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