以另一种形式更新值';通过VBA访问文本框

以另一种形式更新值';通过VBA访问文本框,vba,forms,ms-access,reference,controls,Vba,Forms,Ms Access,Reference,Controls,希望这是有道理的。我很沮丧,我无法弄明白这一点。我有一个简单的Access 2010数据库。我在里面有一个简单的表单,可以帮助用户输入一些特定的信息。这种数据输入情况可能发生在数据库中的其他两种表单上。我希望通过使用openArgs参数传递调用它的窗体的名称,使它更通用,而不是有两个VBA代码具有硬编码控件引用的“helper”窗体副本 当需要将值传输回需要信息的表单时,助手表单会尝试这样做: Private Sub cmdOk_Click() Dim theFormName As St

希望这是有道理的。我很沮丧,我无法弄明白这一点。我有一个简单的Access 2010数据库。我在里面有一个简单的表单,可以帮助用户输入一些特定的信息。这种数据输入情况可能发生在数据库中的其他两种表单上。我希望通过使用openArgs参数传递调用它的窗体的名称,使它更通用,而不是有两个VBA代码具有硬编码控件引用的“helper”窗体副本

当需要将值传输回需要信息的表单时,助手表单会尝试这样做:

Private Sub cmdOk_Click()
    Dim theFormName As String
    Dim theForm As Form

    theFormName = Me.OpenArgs
    Set theForm = Forms.Item(theFormName)

    If Not IsNull(theForm.Name) Then
        theForm.txtLongitude.Value = Me.lblLongitude.Caption
        theForm.txtLatitude.Value = Me.lblLatitude.Caption
    End If

    DoCmd.Close acForm, Me.Name, acSaveNo
End Sub
变量
theForm
被正确填充,并且
theForm.Name
返回表单的正确名称,以便部件正常工作。问题是,
表单..值
没有。当我运行代码时,我得到一个

应用程序定义或对象定义错误(运行时错误2465)

我已经尝试了从当前打开形式到第二个打开形式的控件引用的各种排列,但是我无法获得正确的语法。我试过:

theForm!txtLongitude.Value ("..can't find the field txtLongitude..")
theForm.Controls("txtLongitude").Value ("..cant find the field...")

我有两个建议。如果一个有效,请告诉我,我会编辑我的答案,只包括有效的答案

  • 尝试更改form.txtLength.Value=Me.lblLongitude.Caption 到
    表单!形式!txtLength.Value=Me!lbllongitude.Caption
    theForm.txtratitude.Value=Me.lblLatitude.Caption
    表单!形式!txtLatitude.Value=Me!b亮度.标题

  • 如果您已经尝试过了,或者它不起作用,请尝试声明变量并在将值放入另一种形式之前从一种形式中“提取”值。(还要确保两者的数据类型相同。)

    专用子命令4_Click() 将FormName设置为字符串 将窗体变暗为窗体

        Dim txtLong As String
        Dim txtLat As String
    
        txtLong = Me.lblLongitude.Caption
        txtLat = Me.lblLatitude.Caption
    
        theFormName = Me.OpenArgs
        Set theForm = Forms.Item(theFormName)
    
        If Not IsNull(theForm.Name) Then
            theForm.txtLongitude.Value = txtLong
            theForm.txtLatitude.Value = txtLat
        End If
    
        DoCmd.Close acForm, Me.Name, acSaveNo
    
        End Sub
    

  • 谢谢你的回复。我把你的答案标记为正确,因为你的第二个选项是有效的。事实证明,我的原始代码也是如此。在经典的n00b方式中,我以为我已经将另一种形式的文本框控件重命名为代码中引用的名称,但是,唉,我没有。一旦我做了更改,原来的代码就起作用了。啊!