Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

vba - 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
question from:https://stackoverflow.com/questions/65943821/sql-parameter-prompt

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...