简体   繁体   中英

How to move selected items from a listbox in ms access 2010 in another form in text boxes when click on listbox

How to move selected items from a listbox in ms access 2010 in another form in text boxes, one text box for each item from listbox, when click on listbox. Those are the forms, please help me. form1

form2

You will need VBA in order to accomplish this.

On form1, you will need to check if form2 is open, if not, you can either have it stop the code and ask the user to open form2, or have the coding open the form automatically. I'm assuming you want it automatically...

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

Again, be sure to change the control, table, and field names to match your current database design and naming convention.

Let me know if this works or not and I will make adjustments if needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM