vba MS Access 表单 - 打开和保存记录后的新记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25024314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MS Access form - New record on open and after saving record
提问by JT2013
In relation to my question posted here:
关于我在这里发布的问题:
MS Access Form where user name is selected from list
I have created a form used as an employee time sheet. Users double click this form and another form pops up where they select their user name from a combo box and the main time sheet form loads up specifically for their user name. They are able to add new records, and also view their previous submissions by scrolling through the "Previous" and "Next" records.
我创建了一个用作员工时间表的表格。用户双击这个表单,另一个表单会弹出,他们从组合框中选择他们的用户名,主时间表表单专门为他们的用户名加载。他们能够添加新记录,还可以通过滚动“上一个”和“下一个”记录来查看他们以前提交的内容。
However, when a user opens up the form, their last record displays. Also, when a new record is saved, that new record continues to display unless a user clicks the "New (blank) record" button.
但是,当用户打开表单时,会显示他们的最后一条记录。此外,当保存新记录时,除非用户单击“新建(空白)记录”按钮,否则该新记录将继续显示。
Can someone help me modify the VBA code in the form to allow for:
1) A blank record to automatically load when the form is opened up
2) A blank record to automatically load when a new record is saved/modified.
有人可以帮我修改表单中的 VBA 代码以允许:
1) 打开表单时自动加载的空白记录 2) 保存/修改新记录时自动加载的空白记录。
This is the pop up window - form that pops up when you select the main data entry form:
这是弹出窗口 - 当您选择主数据输入表单时弹出的表单:
This is the code behind the pop up form (above):
这是弹出表单背后的代码(上图):
Private Sub Form_Current()
If VBA.Strings.Len(txtUN & "") = 0 Then DoCmd.OpenForm "frm_UserName", acNormal, , , , acDialog
If VBA.Strings.Len(txtUsername & "") = 0 Then txtUsername = txtUN
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim ctl As Control
On Error GoTo Err_BeforeUpdate
If Me.Dirty Then
If MsgBox("Do you want to save?", vbYesNo + vbQuestion, _
"Save Record") = vbNo Then
Me.Undo
End If
End If
Exit_BeforeUpdate:
Exit Sub
Err_BeforeUpdate:
MsgBox Err.Number & " " & Err.Description
Resume Exit_BeforeUpdate
End Sub
This is the main data entry form [Specialist - Timesheet Entry]:
这是主要的数据输入表格 [专家 - 时间表输入]:
This is the code behind the [Specialist - Timesheet Entry] form:
这是 [Specialist - Timesheet Entry] 表单背后的代码:
Option Compare Database
Private Sub cboUserName_AfterUpdate()
Forms![Specialist - Timesheet Entry].txtUN = cbousername
Forms![Specialist - Timesheet Entry].Filter = "user_full_name = '" & cbousername & "'"
Forms![Specialist - Timesheet Entry].FilterOn = True
'Forms![Specialist - Timesheet Entry].Requery
'DoCmd.Close
End Sub
Private Sub Form_Unload(Cancel As Integer)
If (VBA.Strings.Len(cbousername & "") = 0) Then
MsgBox "You must supply a user name before proceeding.", , "ERROR: Missing Info."
Cancel = True
End If
End Sub
Once the [Specialist - Timesheet Entry] is selected, the [form_UserName] pops up. You must select a user name from the list, and hit the "X" button. Once the "X" button is selected than the data entry form [Specialist - Timesheet Entry] will appear for a user to enter the necessary fields. I am also looking for the user to not have to hit the "X" button in order to initiate the [Specialist - Timesheet Entry] form. Ideally once the user selects the user name from the drop down list in the pop up, i'd like the form to close automatically and generate the [Specialist - Timesheet Entry] form.
一旦选择了 [专家 - 时间表条目],就会弹出 [form_UserName]。您必须从列表中选择一个用户名,然后点击“X”按钮。一旦选择了“X”按钮,就会出现数据输入表格 [专家 - 时间表输入],供用户输入必要的字段。我还希望用户不必点击“X”按钮即可启动 [专家 - 时间表条目] 表单。理想情况下,一旦用户从弹出的下拉列表中选择了用户名,我希望表单自动关闭并生成 [专家 - 时间表条目] 表单。
回答by Dan Romano
I think I have something for you here.
我想我在这里有一些东西要给你。
I share the same sentiment as OverMind - I'm iffy about the Me.Dirty code. I don't think it's necessary. All you need to do is click the dropdown, select a name and open a form that is filtered using the selected name. Same thing goes for the Form_BeforeUpdate event. You're not actually modifying any data, you're just clicking and selecting. I recommend removing both of those events.
我和 OverMind 有相同的看法——我对 Me.Dirty 代码持怀疑态度。我认为没有必要。您需要做的就是单击下拉列表,选择一个名称并打开一个使用所选名称过滤的表单。Form_BeforeUpdate 事件也是如此。您实际上并没有修改任何数据,您只是点击并选择。我建议删除这两个事件。
If you don't mind, I reworked this a little since I don't have the entire application in front of me. Try the following (there's a few parts):
如果您不介意,我会稍微修改一下,因为我面前没有整个应用程序。尝试以下操作(有几个部分):
1.) frm_UserName
1.) frm_用户名
Comment your existing code in the cboUserName_AfterUpdate
event and add this line:
在cboUserName_AfterUpdate
事件中注释您现有的代码并添加以下行:
DoCmd.OpenForm "Specialist - Timesheet Entry"
'Open the main timesheet form and set the filter based on the dropdown selection in the previous form.
Forms![Specialist - Timesheet Entry].Filter = "Username = '" & Forms![frm_UserName].cboUserName.Column(1) & "'"
'Turn the filter on.
Forms![Specialist - Timesheet Entry].FilterOn = True
'Set the username textbox to the selected record.
Forms![Specialist - Timesheet Entry].txtUN = Me.cboUserName.Column(1)
'Jump to a new record even though the form is filtered.
Forms![Specialist - Timesheet Entry].SetFocus
DoCmd.GoToRecord , , acNewRec
'Close the previous form - we no longer need it.
DoCmd.Close acForm, "frm_UserName"
This will open the Timesheet - Specialist Entry
from frm_UserName
form "automatically load [a blank record] when the Timesheet
form is opened up" using a the selection as the filter.
这将使用选择作为过滤器打开Timesheet - Specialist Entry
来自frm_UserName
表单“打开表单时自动加载[空白记录] Timesheet
”。
2.) Specialist - Timesheet Entry
2.) 专家 - 时间表条目
Create a new procedure in the Form Open event and add this code:
在 Form Open 事件中创建一个新过程并添加以下代码:
Private Sub Form_Open(Cancel As Integer)
DoCmd.OpenForm "frm_UserName", acNormal, , , , acDialog
End Sub
Next, create a new procedure in the Save button Click
event and add this code:
接下来,在 Save 按钮Click
事件中创建一个新过程并添加以下代码:
Private Sub cmdSave_Click()
'Save the record.
RunCommand acCmdSaveRecord
'Load a new record after the save.
DoCmd.GoToRecord , , acNewRec
End Sub
This will automatically load a blank record when a new record is saved/modified.
这将在保存/修改新记录时自动加载空白记录。
I do hope this helps. If you have any questions at all, please let me know. I'd be glad to help even further. I also have a working copy available...
我希望这会有所帮助。如果您有任何问题,请告诉我。我很乐意提供更多帮助。我也有可用的工作副本...