简体   繁体   中英

SQL Parameter Prompt

I'm trying to run a SQL command in VBA to delete certain records from a table. When I run it I'm prompted for parameters. What is causing this? I have included the subroutine that includes the SQL.

Public Sub AddCon(newCont, svID)
    Dim daDb As DAO.Database
    Dim rst1 As Recordset
    Dim rst2 As Recordset

    Dim selContract As String

    Set daDb = CurrentDb
    Set rst1 = daDb.OpenRecordset("tblContracts")
    Set rst2 = daDb.OpenRecordset("tblContractList")
  
     rst2.AddNew
     rst2!Contract = newCont
     rst2!ID = svID
     rst2.Update
     rst2.Close

    Set rst2 = Nothing
    DoCmd.Close

    Dim strSQL As String

    strSQL = "DELETE * FROM [tblContractList] " _
    & "WHERE rst1.Contract <> newCont"
 
     DoCmd.RunSQL strSQL
    
    DoCmd.OpenForm "frmContracts"

 End Sub

As you make query,

DELETE * FROM [tblContractList] WHERE rst1.Contract <> newCont;

Microsoft Access engine will ask for you two unknowns via prompt: rst1.Contract and newCont.

So you should replace them with known values:

Public Sub AddCon(newCont, svID)
    Dim daDb As DAO.Database
        'Dim rst1 As Recordset
        Dim rst2 As Recordset
    
        ' Dim selContract As String
    
        Set daDb = CurrentDb
        'Set rst1 = daDb.OpenRecordset("tblContracts")
        Set rst2 = daDb.OpenRecordset("tblContractList")
      
         rst2.AddNew
         rst2!Contract = newCont
         rst2!ID = svID
         rst2.Update
         rst2.Close

        Set rst2 = Nothing
        'DoCmd.Close
    
        Dim strSQL As String

        '
        ' DELETE query must be run with care, as useful data may disappear!!!
        '
        strSQL = "DELETE * FROM tblContractList " _
            & "WHERE (Contract " & " <> " & newCont & ")"
        '
        ' or single quoting newCont if it is a string:
        '
        'strSQL = "DELETE * FROM tblContractList " _
        '    & "WHERE (Contract " & " <> '" & newCont & "')"
        '

         'rst1.Close
         'Set rst1 = Nothing

         Set daDb = Nothing

         DoCmd.RunSQL strSQL


        DoCmd.OpenForm "frmContracts"

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM