简体   繁体   English

Microsoft Access插入查询

[英]Microsoft Access insert query

Access table Allowances_3_15_18 has 5 columns. 访问表Allowances_3_15_18有5列。 I want to insert a calculated field from a form EmployeeSalary) into one of the columns Amount in the table. 我想将表格EmployeeSalary)的计算字段插入表中的Amount列之一。

Each value will link with the relevant primary ID's from the form and the table which are the same JobID . 每个值将与表单和表格中相同的JobID链接到相关的主要ID。 How do I do this in VBA? 如何在VBA中执行此操作?

I currently have done it in the afterUpdate event in the property sheet. 我目前在属性表的afterUpdate事件中完成了此afterUpdate

Private Sub ProjectedDollarAmount_AfterUpdate()

    Dim strSQL As String
    Dim ProjectedDollarAmount As Currency

    strSQL = "INSERT INTO [Allowances_3_15_18] ([Amount]) VALUES (" & _
        PrepareSQLNumber(Me.ProjectedDollarAmount) & ") WHERE JobID = " & _
        PrepareSQLNumber(Me.JobID) & ";"

    Call ExecuteMyCommand(strSQL)
End Sub

You need to get away from SQL concatenation and start using parameters. 您需要摆脱SQL串联并开始使用参数。

Create a query with two parameters, the amount to be inserted and the JobId. 用两个参数创建查询,要插入的数量和JobId。 The query's SQL should be something like this: 查询的SQL应该是这样的:

PARAMETERS [prmAmount] Currency, [prmJobId] Long;
UPDATE [Allowances_3_15_18] SET [Amount] = [prmAmount]
WHERE JobID = [prmJobId];

Then in code, simply pass the parameter values and execute the above query: 然后在代码中,只需传递参数值并执行上述查询:

Sub Add()
    With CurrentDb().QueryDefs("qryName")
        .Parameters("[prmAmount]").Value = PrepareSQLNumber(Me.ProjectedDollarAmount)
        .Parameters("[prmJobId]").Value = PrepareSQLNumber(Me.JobID)
        .Execute dbFailOnError
    End With
End Sub

You need to change the qryName to the actual name of the query. 您需要将qryName更改为查询的实际名称。

You can read more about parameter queries here . 您可以在此处阅读有关参数查询的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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