ms通过表单访问更新记录的vba代码

时间:2016-11-07 09:58:10

标签: vba access-vba ms-access-2007 access

我是MS Access 2007及其新手。我对vba几乎一无所知。 ...我有一个表名F,字段为F1,F2,F3,F4,F5,其中前三个是数字字段&最后两个是文本字段。在具有文本框的形式:txtF1,txtF2,txtF3,txtF4,txtF5和命令按钮cmdUpdate,我想更新表F中的F5,其中F1 = txtF1,F2 = txtF2,F3 = txtF3和F4 = txtF4条件满足。 ..请帮助完整的代码。

1 个答案:

答案 0 :(得分:0)

在cmdUpdate按钮的click事件中,您需要放置一个生成sql命令的过程,然后执行它。

Private Sub cmdUpdate_Click()

'Create a string variable to hold your sql command string
Dim strSQL As String

'Fill the variable with your sql command, plucking values out of your 
'form as shown using the Me. operator.  When using text values in your 
'string, wrap them with the Chr(34)'s, which will insert quotation marks in 
'your sql string when it's resolved.
strSQL = "UPDATE F SET F.F5 = " & Chr(34) & yourtextvaluehere & Chr(34) & _
            " WHERE F1=" & Me.txtF1 & _
            " AND F2=" & Me.txtF2 & _
            " AND F3=" & Me.txtF3 & _
            " AND F4=" & Chr(34) & Me.txtF4 & Chr(34)

'Execute the sql statement you've built against the database
CurrentDb.Execute strSQL, dbSeeChanges + dbFailOnError

End Sub