Results 1 to 5 of 5

Thread: Help with VBA SQL Statements

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2003
    Location
    State College, PA
    Posts
    1

    Help with VBA SQL Statements

    Hi...

    I'm writing some code that takes data out of Access and puts it inot Excel to plot a graph using SQL statements. It works fine, but if I use 'WHERE' in my SQL statement, it does not show anything. Otherwise it works just fine. As a result, I have TOO many values toi plot. I need to use 'WHERE' in the SQL statement to reduce the number of records that are pulled out of Access and put intot Excel.

    Can someone help me with this problem?

    kaspar

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    post your code
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  3. #3
    Lively Member MileOut's Avatar
    Join Date
    Nov 2001
    Location
    Glasgow
    Posts
    83
    Here's the code [edited by alex_read - 05/06/2003]

    VB Code:
    1. Private Sub CommandButton3_Click()
    2.  
    3. Dim cnn As ADODB.Connection
    4. Dim rsTemp As ADODB.Recordset
    5. Dim cmdCommand As ADODB.Command
    6.  
    7. Dim i As Integer
    8.  
    9. i = 2
    10.  
    11. ' Open the connection.
    12. Set cnn = New ADODB.Connection
    13. With cnn
    14. .ConnectionString = _
    15. "Provider=Microsoft.Jet.OLEDB.4.0"
    16. .Open ThisWorkbook.Path & "\x.mdb"
    17. End With
    18.  
    19. ' Set the command text.
    20. Set cmdCommand = New ADODB.Command
    21. Set cmdCommand.ActiveConnection = cnn
    22. With cmdCommand
    23. .CommandText = "SELECT [Trapping Information].Week, [Mouse-Main].Chip " & _
    24. "FROM [Mouse-Main] INNER JOIN [Trapping Information] " & _
    25. "ON [Mouse-Main].ID = [Trapping Information].[Mouse-Main_ID] " & _
    26. "WHERE ((([Mouse-Main].Chip)=201-87E3));"
    27. .CommandType = adCmdText
    28. .Execute
    29. End With
    30.  
    31. ' Open the recordset.
    32. Set rsTemp = New ADODB.Recordset
    33. Set rsTemp.ActiveConnection = cnn
    34. rsTemp.Open cmdCommand
    35.  
    36. 'Move to start of recordset if not there already
    37. If Not rsTemp.BOF Then rsTemp.MoveFirst
    38.  
    39. Do While Not rsTemp.EOF
    40.  
    41. Cells(i, 1).Value = rsTemp("Chip")
    42. Cells(i, 2).Value = rsTemp("Week")
    43.  
    44. i = i + 1
    45. rsTemp.MoveNext
    46. Loop
    47.  
    48. Set cmdCommand = Nothing
    49. rsTemp.Close
    50. cnn.Close
    51.  
    52. End Sub
    Last edited by alex_read; Jun 5th, 2003 at 05:43 AM.

  4. #4
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    What if you used a like clause & not an equal to, does his show anything...

    WHERE ((([Mouse-Main].Chip) LIKE 201%

    (not sure if that's percentage or * there, one works with access & not SQL server & visa versa.

    Also, are you searching a string column & need to enclost the figure in single quotes?

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  5. #5
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Btw - kaspar, I've edited your post to include the code - it's easier for everyone reading to place the code here with your subject rather than having two posts and using links between them...

    Ephesians, sorry mate, since I've merged the two posts into 1 it means you've lost a whole post from your count when I deleted the other post!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width