在VBA

时间:2018-05-31 14:24:47

标签: sql vba ms-access

我正在尝试设置命令按钮以将数据输入到表中。如果记录已存在,我希望按钮更新它,如果没有,按钮需要创建它。这是表格的样本

ID    scenarioID    reductionID    Impact Variable    Variable Impact
1         1              1          Safety             4
2         1              1          Environmental      2
3         1              1          Financial          1 

为了准确定位记录,需要搜索与scenarioID配对的特定影响变量。我正在尝试使用select语句,但DoCmd.RunSQL不适用于select语句,我不知道如何做到这一点。

这是代码。我在select语句前面留下了DoCmd.SQL,因为现在还没有其他东西放在那里。

Private Sub Var1R1_Click() 'Stores appropriate values in tImpact upon click

'Declaring database and setting recordset
Dim db As Database
Dim rs As Recordset

Set db = CurrentDb
Set rs = db.OpenRecordset("tImpact")


'Declaring Variable as Scenario Choice combobox value
Dim Sc As Integer

Sc = Schoice.Value


'Stores impact variable 
Dim impvar1 As String


'Desired impact variable for column 1
impvar1 = DLookup("impactVariable", "tImpactVars", "ID = 1")



DoCmd.RunSQL "SELECT * FROM tImpact WHERE [Impact Variable] = " & impvar1 & " AND scenarioID = " & Sc



If rs.EOF Then

    DoCmd.RunSQL "INSERT INTO tImpact(scenarioID, [Impact Variable], [Variable Impact])" & "VALUES (" & Sc & ", " & impvar1 & ", 1)"

    MsgBox "Record Added", vbOKOnly


Else

    db.Execute "UPDATE tImpact SET [Variable Impact] = 1 WHERE [Impact Variable] = " & impvar1 & " AND scenarioID = " & Sc

    MsgBox "Record Updated", vbOKOnly


End If

End Sub

如果有人可以告诉我如何运行SELECT语句,或者采用其他方式,那就太棒了。

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

您可以使用记录集。在这种情况下,记录集更好,因为你只执行一次SQL,如果它返回一个记录,你“编辑”,如果没有,那么你“添加”与SAME reocrdset。这种方法的代码少了FAR,你在reocrdset中设置的值不需要杂乱的引号或分隔符等。

例如:

scenaridID = 1             ' set this to any number
impvar1 = "Safety"         ' set this to any string
updateTo = "Financial"

strSQL = "select * from tImpact where [Impact Variable] = '" & impvar1 & "'" & _
        " AND scenaridID = " & scenaridID

Set rst = CurrentDb.OpenRecordset(strSQL)
With rst
  If .RecordCount = 0 Then
     ' add the reocrd
     .AddNew
  Else
     .Edit
  End If

  !scenaridID = scenarid
  ![Impact Variable] = impvar1
  ![Variable Impact] = 1
  .Update
End With
rst.Close

因此,您可以使用相同的代码进行更新和编辑。这只是一个问题,如果您添加或编辑。

答案 1 :(得分:0)

使用OpenRecordset并检索记录的ID(如果存在)。

Private Sub Command0_Click()

    Dim aLookup(1 To 3) As String
    Dim aAction(1 To 3) As String
    Dim rs As Recordset
    Dim db As Database

    'Replace these two constants with where you get the information on your form
    Const sIMPVAR As String = "Financial"
    Const lSCENID As Long = 1

    'Build the SQL to find the ID if it exists
    aLookup(1) = "SELECT ID FROM tImpact"
    aLookup(2) = "WHERE ScenarioID = " & lSCENID
    aLookup(3) = "AND ImpactVariable = """ & sIMPVAR & """"

    'Run the sql to find the id
    Set db = CurrentDb
    Set rs = db.OpenRecordset(Join(aLookup, Space(1)))


    If rs.BOF And rs.EOF Then 'it doesn't exist, so build the insert statement
        aAction(1) = "INSERT INTO tImpact"
        aAction(2) = "(ScenarioID, ImpactVariable, VariableImpact)"
        aAction(3) = "VALUES (" & lSCENID & ", '" & sIMPVAR & "', 1)"
    Else 'it does exist, so build the update statement
        aAction(1) = "UPDATE tImpact"
        aAction(2) = "SET VariableImpact = 1"
        aAction(3) = "WHERE ID = " & rs.Fields(0).Value
    End If

    'Run the action query
    db.Execute Join(aAction, Space(1))

    rs.Close
    Set rs = Nothing
    Set db = Nothing

End Sub