Access VBA:从lstAvailable移项至lstAssigned后列表框不显示求助
Hey there, let's work through why your lstAssigned list box isn't showing the items you've moved over—even though the data's successfully making it into tblComponents. Here are the most likely fixes:
1. Refresh the lstAssigned Data Source
Access list boxes don't automatically update when the underlying table changes. After inserting your record into tblComponents, you need to explicitly tell lstAssigned to reload its data. Add this line right after your CurrentDb.Execute call:
lstAssigned.Requery
This will force the list box to pull the latest data from its configured RowSource, which should include the newly inserted record.
2. Verify Your lstAssigned RowSource Configuration
Double-check that lstAssigned's RowSource is set up to include the new records you're inserting:
- Ensure the RowSource query includes all necessary fields from
tblComponents(like the component ID and name you want to display). - If your RowSource has a
WHEREclause (e.g., filtering by the selected machine subsystem), confirm that the new record matches this filter. For example, if your query uses[Machine Subsystem ID] = Forms!YourFormName!listMachineSubSystem.Column(1), make sure yourINSERTstatement is actually storing the correctnRidvalue in that field.
3. Check Your INSERT Statement Completeness
From your code snippet, it looks like your sSQL string is cut off. Make sure you're inserting all required fields into tblComponents, especially those that lstAssigned uses to display items. For example, if your table needs both the machine subsystem ID and component ID, your full INSERT should look something like:
sSQL = "INSERT INTO tblComponents ([Machine Subsystem ID], [Component ID]) VALUES (" & nRid & ", " & nEid & ")"
Use dbFailOnError with CurrentDb.Execute to catch any hidden insertion errors that might be happening (even if you think the data is saving):
CurrentDb.Execute sSQL, dbFailOnError
4. Optional: Remove Items from lstAvailable (If Needed)
If you also want the selected items to disappear from lstAvailable after moving, add this line inside your loop (after the INSERT and Requery):
lstAvailable.RemoveItem varRow
Let me know if any of these steps resolve the display issue, or if you need to share more details about your lstAssigned RowSource setup!
内容的提问来源于stack exchange,提问作者user9527824

