尝试从另一个表单引用文本框的访问 VBA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4464941/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:00:07  来源:igfitidea点击:

Access VBA trying to reference a textbox from another form

ms-accessvbaaccess-vba

提问by MAW74656

I'm using Access VBA, and I'm trying to modify existing code to add a popup box. This popup box is another form, and when a user clicks on a button on this form, I want a textbox on the base form to be populated. After that the popup disappears and then I need to access that value from the textbox on the base form.

我正在使用 Access VBA,并且正在尝试修改现有代码以添加一个弹出框。这个弹出框是另一种表单,当用户点击这个表单上的按钮时,我想要填充基本表单上的一个文本框。之后弹出窗口消失,然后我需要从基本表单上的文本框中访问该值。

The sequence should be like this:
1) Base form button click calls modal popup
2) Click in button on popup saves value to base form's textbox, then returns control.
3) Base form then uses this value to do something.

顺序应该是这样的:
1) 单击基本表单按钮调用模态弹出窗口
2) 单击弹出窗口上的按钮将值保存到基本表单的文本框,然后返回控制。
3) 基础形式然后使用这个值做某事。

I have some code, but its not working properly. Can anyone see whats missing here and help me correct?

我有一些代码,但它不能正常工作。任何人都可以看到这里缺少什么并帮助我纠正吗?

Base Form

基本形式

Sub base()
    DoCmd.OpenForm "PaperType", , , , , acDialog

    MsgBox Me.TheAnswer           'This line gives a null error
End Sub

Popup Form

弹出窗体

Private Sub btnRolls_Click()
    'Me.Tag = 1
    Forms!ReceiptDetail_sfrm!TheAnswer = 1
    Me.Visible = False
End Sub

Private Sub btnSheets_Click()
    'Me.Tag = 4
    Forms!("ReceiptDetail_sfrm").TheAnswer = 4
    Me.Visible = False
End Sub

回答by Mindflux

You can do it that way, you probably need something like:

你可以这样做,你可能需要这样的东西:

Private Sub btnRolls_Click() Forms!ReceiptDetail_sfrm!TheAnswer = 1 Forms!ReceiptDetail_sfrm.Refresh Me.Visible = False End Sub

我已经做了很多次你正在做的事情。我现在不在工作中检查我的代码作为示例,但它是可行的。

回答by RolandTumble

What I tend to do is hide the popup, then read from it:

我倾向于做的是隐藏弹出窗口,然后从中读取:

Main Form:

主要形式:

Private Sub base()
   DoCmd.OpenForm "PaperType", , , , , acDialog
   'code waits for modal hide here
   Me!TheAnswer = Forms("PaperType").SomethingOnThatFormThatStoresTheValue
   DoCmd.Close acForm, "PaperType", acSaveNo
   MsgBox Me.TheAnswer
End Sub

Popup

弹出

Private Sub btnRolls_Click
   'may be hidden control
   Me.SomethingOnThatFormThatStoresTheValue = TheValueToRead
   Me.Visible = False
End Sub

Make sure that either A) Your popup can't be closed from the form itself, or B) Your calling code will handle the error of trying to read something that's not longer loaded (or both).

确保 A) 您的弹出窗口无法从表单本身关闭,或 B) 您的调用代码将处理尝试读取不再加载的内容(或两者兼有)的错误。

回答by MAW74656

Apparently the correct answer is: "Don't do it this way!". Instead of trying to pass data between the forms, which is a pain, I made a table in Access which only has 1 field in 1 record. Then I store the value in there using DoCmd.RunSQL, and retrieve it from the other form using DLOOKUP().

显然正确的答案是:“不要这样做!”。我没有尝试在表单之间传递数据,这很痛苦,而是在 Access 中创建了一个表,该表在 1 条记录中只有 1 个字段。然后我使用 DoCmd.RunSQL 将值存储在那里,并使用 DLOOKUP() 从另一个表单中检索它。