单击列表框时,如何在文本框中以另一种形式从ms access 2010中的列表框中移动所选项目

时间:2017-12-19 16:14:46

标签: access-vba

如何在文本框中以另一种形式从ms access 2010中的列表框中移动所选项目,在列表框中单击每个项目的文本框。那些是表格,请帮帮我。 form1

form2

1 个答案:

答案 0 :(得分:0)

您需要VBA才能完成此任务。

在form1上,你需要检查form2是否打开,如果没有,你可以让它停止代码并要求用户打开form2,或者让代码自动打开表单。我假设你想要它自动...

Private sub List0_Click() 'Be sure to change List0 to the listbox control name you have given
Dim rst         as DAO.Recordset
Dim strSQL      as String
'Checks if current form is loaded, opens form if not
if not currentproject.allforms("form2").isloaded then
   docmd.openform "Form2",acnormal
end if
'Creates query string to find specific record on listbox
strSQL = "SELECT * " & _
         "FROM YourTableNameHere " & _
         "WHERE (((YourTableName.YourFieldName) =" & me.List0.value & "));"
'Opens the recordset from the query string created above
Set rst = currentdb.openrecordset(strsql)
'Targets form2 and places values from recordset into form
With [Forms]![Form2]
     ![Your form2 control name] = rst![YourfieldNameForValue]
     'Repeat for each field you wish to place a value in on your form2
End With
'Closes recordset and release object in memory
rst.close
Set rst = Nothing
'Validates the recordset is closed and object is released from memory
EndCode:
If not rst is nothing then
   rst.close
   set rst = nothing
end if
end sub

同样,请务必更改控件,表和字段名称以匹配当前的数据库设计和命名约定。

让我知道这是否有效,如果需要我会做出调整。