Results 1 to 5 of 5

Thread: The same SQLs?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    The same SQLs?

    I coded this version of a DELETE statement:
    VB Code:
    1. strSQL = "DELETE Main.Month FROM Main WHERE Main.Month <> '" & mstrMonth & "';"
    2.     'DoCmd.RunSQL (strSQL)
    instead of this query in the DB:
    VB Code:
    1. DELETE Main.Month
    2. FROM Main
    3. WHERE (((Main.Month)<>[Which month are you working on?]));
    Is the coded SQL correct? I'm suspicious of it, even though it returns the same results.

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Yep! no problems there at all.

    Just as a side note, when working with a single table, you don't need to write out Main.Month all the time, generally you only need to put the table name before the column when working with Joins in the statement, you can also use this to accomplish the same thing:

    strSQL = "DELETE Month FROM Main WHERE Month <> '" & mstrMonth & "';"

    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

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Thanks, I know I don't have to include the table name, but I was copying from an existing Access query created in designer view.
    The reason I asked is that the queries look the same to me, but the query is called by a form, and I was getting different results from a different query that's based on the results of this query depending on which way I did it.
    I use an Inputbox to get mstrMonth in the code query instead of the parameter in the Query Designer style so that I can use mstrMonth later in code.
    The results of both queries are the same, just the result of the subsequent query are different.
    Since the one query is part of the db and the other is in code, do I need to run some sort of refresh statement on the table? While the code statement returns the correct results, it doesn't seem to actually change the table data.

  4. #4
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Private Sub Command2_Click()
    Dim strSQL As String
    strSQL = "DELETE FROM Table1"

    CurrentDb.Execute strSQL
    End Sub

    Is what I'm using. Okay there's a couple of things I want to check here -

    Firstly is the month column set to a string/text data type?
    Secondly, if I understood the last message right, you're opening a recordset in your code & then running the DELETE statement upon this recordset? if so, try calling a recordsetvariable.update after the DELETE call has been made...

    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

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Here's the code in the sub. Most of it was inherited, I'm just trying to automate it a bit for my own use. I want to get the mstrMonth via Input box instead of by query parameter so I can automatically fill in an email heading and message body that gets sent later via DoCmd.SendObject. The commented out code is mine, the rest was already there. And yes, Month is a text field.
    VB Code:
    1. 'Get database info and select appropriate month
    2. Private Sub GetDataBase_Click()
    3. Dim StartTime, PauseTime As Integer
    4. Dim strSQL As String
    5. Dim rs As Recordset
    6. Dim db As Database
    7. 'On Error GoTo errMsg
    8.  
    9.   If GetData = 0 Then   'call to GetData function in Module1
    10.     Exit Sub            'no database found
    11.   End If
    12.  
    13.   'Wait, presumably to give db time to load before running query
    14.   PauseTime = 5
    15.   StartTime = Timer
    16.   While Timer < StartTime + PauseTime
    17.   Wend
    18.  
    19. 'errHandler:
    20.  ' mstrMonth = InputBox("Which month are you working on?", "Select Month")
    21.  
    22.   'If Len(mstrMonth) <> 0 Then
    23.    ' strSQL = "DELETE Main.Month FROM Main WHERE Main.Month <> '" & mstrMonth & "';"
    24.     'DoCmd.RunSQL (strSQL)
    25.     'Exit Sub
    26.   'Else
    27.    ' MsgBox "Please enter a month!", vbCritical, "Missing Month"
    28.     'GoTo errHandler
    29.   'End If
    30.   'Exit Sub
    31. 'errMsg:
    32.  ' MsgBox "Error in GetDatabase in OpCheck form"
    33.  
    34.  
    35.   'Choose which month to work on via parameter query
    36.   DoCmd.OpenQuery "MainQuery"
    37. End Sub

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