Question

I have a particularly difficult problem to deal with. I am working on a Form for access. This form contains a large amount of cascade data connections. It serves as a filtering, selection, review, and editing interface for the database.

Below I have included a basic description of the design; -ComboBox1 populates its data from a reference table, and cascade updates to ComboBox2. -ComboBox2 populates based on selection of ComboBox1 and then cascades information to ListBox1 which runs a query to generate a list of records. -Selecting a record in Listbox1 populates the various hidden columns of the list into matching text and comboBox fields for display/Editing. It also on click cascade Populates Listbox2 using a query to pull records from a different table that are related to the selected record from ListBox1. -I have a number of buttons to handle adding, deleting, and editing the records of the table where ListBox1 Queries its information from. Everything up to this point works correctly (thanks in part to aid from people answering my other questions) Where I am stuck now is in trying to create similar buttons that serve the same function with the Table where ListBox2 drawns its records(Add/Edit/Delete) I have added the Add button successfully already but have become stuck on the edit function.

I have a button that I am trying to build an "OnClick" event for. It needs to pull the selected record from a listbox and have the form it is opening be set to that specific record. I know I can pull the selected value from the list box with listbox.selected.value but How does that translate into the DoCmd.OpenForm syntax?

Was it helpful?

Solution

The DoCmd.OpenForm parameters you want to adjust are the optional WhereCondition and DataMode. WhereCondition accepts a SQL Where clause without the word where. This restriction is applied to the form's rowsource before the form opens. DataMode can override the form's saved settings for AllowEdits and DataEntry. It sounds like you want to use acFormEdit in this case.

Private Sub CommandFoo_OnClick()
Dim strWhere As String

'Do yourself a favor and build the WhereCondition in a string variable so that you can see what you've built. 
'Surround text values in double quotes. 
'Surround dates with # and specify in m/d/yyyy or yyyy-mm-dd
'Make sure you build in spaces in as necessary.
strWhere = "NumberField1=" & ListBox1.Columns(0) & " AND TextField2=" & Chr(34) & ListBox1.Columns(1) & Chr(34)
Debug.Print strWhere

DoCmd.OpenForm FormName:="YourFormName", WhereCondition:=strWhere, DataMode=acFormEdit
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top