从ms Access 2016中表单上的未绑定文本框中更新记录字段

问题描述 投票:0回答:2

我有一个名为[ADD_Individual_Info]的表格,当用户使用其身份证进行扫描并且条形码不在数据库中时,会弹出该表格。但是,我了解到有些人刚刚获得了新卡,并且新的条形码编号未存储在数据库中。我在[ADD_Individual_Info]表单上有一个文本框[txt_copyEmail],该文本框允许用户搜索其电子邮件地址以在数据库中查找其记录。这样一来,他们不必再次输入信息。在同一表格上,我还有另一个文本框[txt_EDIPI],其中包含新扫描的条形码编号。我只想使用文本框[txt_EDIPI]中的新数字更新表中的记录。表单[ADD_Individual_Info]上有一个子表单[SubIndividualInfo],它的源是[sfrm_User_Item_Info],其记录源是[qry_User_Item_Info],它仅显示用户的电子邮件地址及其名称。我在[txt_copyEmail]文本框上有更新后事件。看起来像这样:

Private Sub txt_copyemail_AfterUpdate()
Call search_email
Me.txt_UserEmail.Value = Me.SubIndividualInfo.Form.[UserEmail]
Me.txt_UserName.Value = Me.SubIndividualInfo.Form.[UserName]
Me.txt_Office.Value = Me.SubIndividualInfo.Form.[Office]
Me.txt_Rank_Title.Value = Me.SubIndividualInfo.Form.[Rank_Title]
Me.txt_PhoneNumber.Value = Me.SubIndividualInfo.Form.[PhoneNumber]
Me.txt_IndividualID.Value = Me.SubIndividualInfo.Form.[EDIPI]
End Sub

我的功能代码:

Function search_email()
Dim SQL As String
    SQL = "SELECT tbl_User_Info.UserName, tbl_User_Info.[UserEmail]," _
        & "tbl_User_Info.[Office]," _
        & "tbl_User_Info.[Rank_Title]," _
        & "tbl_User_Info.[EDIPI]," _
        & "tbl_User_Info.PhoneNumber FROM tbl_User_Info " _
        & "Where [UserEmail] LIKE '*" & Me.txt_copyemail & "*' "
    Me.SubIndividualInfo.Form.RecordSource = SQL
    Me.SubIndividualInfo.Form.Requery
End Function

这样,用户可以验证信息是否正确,如果是,则只需单击按钮[btn_SaveandClose]。它的代码如下:

Private Sub Btn_SaveandClose_Click()
Dim db As Database
Dim rec As Recordset
    Set db = CurrentDb
    Set rec = db.OpenRecordset("Select * from tbl_User_Info")
    rec.Edit
        rec("UserEmail") = Me.UserEmail
        rec("EDIPI") = Me.txt_EDIPI
        rec("UserName") = Me.UserName
        rec("Office") = Me.Office
        rec("Rank_Title") = Me.Rank_Title
        rec("PhoneNumber") = Me.PhoneNumber
        rec.Update
    rec.Close
    Set rec = Nothing
    Set db = Nothing
Me.txt_UserEmail.Value = ""
Me.txt_UserName.Value = ""
Me.txt_Office.Value = ""
Me.txt_Rank_Title.Value = ""
Me.txt_PhoneNumber.Value = ""
Me.txt_IndividualID.Value = ""
Me.txt_EDIPI.Value = ""
Me.txt_copyemail.Value = ""
Call search_email
End Sub

它不是在编辑窗体上显示的记录,它只是更新表附带的表中的第一条记录,并在文本框中输入新的数字[txt_EDIPI]。我的代码有任何帮助。或者,如果有更好的方法可以完成这项工作,那么我会开放一个更好的方法。谢谢。

ms-access access-vba access
2个回答
0
投票

使用过滤条件打开仅记录1的记录集:Set rec = db.OpenRecordset("Select * from tbl_User_Info WHERE somefield=" & input)

或者不要打开记录集,而只运行带有所需记录条件的UPDATE操作SQL,例如显示文本,日期,数字类型字段的示例:

CurrentDb.Execute "UPDATE tablename SET field1 = '" & Me.tbxName1 & "', field2 = #" & Me.tbxName2 & "#, field3 = " & Me.tbxName3 & " WHERE ID=" & input

或者不执行任何操作,仅通过表单编辑记录。如果表单的RecordSource中包含字段,则只需:

Me!fieldname = "some input"


0
投票

我在“保存并关闭”按钮上使用的代码。这是添加新的或只是更新字段。我感谢用户June7给了我有用的提示。

If Me.chkAddNewInfo = True Then
    Dim db As Database
    Dim rec As Recordset
    Set db = CurrentDb
    Set rec = db.OpenRecordset("Select * from tbl_User_Info")
    rec.AddNew
        rec("UserEmail") = Me.txt_UserEmail
        rec("EDIPI") = Me.txt_EDIPI
        rec("UserName") = Me.txt_UserName
        rec("Office") = Me.txt_Office
        rec("Rank_Title") = Me.txt_Rank_Title
        rec("PhoneNumber") = Me.txt_PhoneNumber
        rec.Update
    rec.Close
    Set rec = Nothing
    Set db = Nothing
Else
    Me!SubIndividualInfo.Form.EDIPI.Value = Me.txt_EDIPI
    DoCmd.RunCommand acCmdSaveRecord
End If
© www.soinside.com 2019 - 2024. All rights reserved.