Create a form to make default selected item

siamandm
BronzeLounger
Posts: 1267
Joined: 01 May 2016, 09:58

Create a form to make default selected item

Post by siamandm »

Hi
i have a list box and im using this code on load form to make some default value selected

Code: Select all

Dim i As Long
    For i = 0 To Me.lstItm.ListCount - 1
        Select Case Me.lstItm.ItemData(i)
            Case 24, 30 ' 
                Me.lstItm.Selected(i) = True
        End Select
    Next i
    Call FilterSubForm
24, and 30 are the selected as default selected row for the list box

i would like to make a button and when clicking it opens the list box and allow me to modify the default values in the code above


regards

User avatar
HansV
Administrator
Posts: 78620
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Create a form to make default selected item

Post by HansV »

Create a new table in design view.
Add a single field named Item of type Number (Long Integer).
Save the table as tblDefaultItems.

Change the code in Form_Load as follows:

Code: Select all

    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset
    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset("tblDefaultItems", dbOpenForwardOnly
    Do While Not rst.EOF
        Me.lstItm.Selected(rst!Item) = True
        rst.MoveNext
    Loop
    rst.Close
Create an On Click event procedure for the command button with the following code:

Code: Select all

    Dim dbs As CurrentDb
    Dim rst As DAO.Recordset
    Dim itm As Variant
    Set dbs = CurrentDb
    dbs.Execute "DELETE * FROM tblDefaultItems", dbFailOnError
    Set rst = dbs.OpenRecordset("tblDefaultItems", dbOpenDynaset)
    For Each itm In Me.lstItm.ItemsSelected
        rst.AddNew
        rst!Item = itm
        rst.Update
    Next itm
    rst.Close
Note: I haven't tested the code.
Best wishes,
Hans

siamandm
BronzeLounger
Posts: 1267
Joined: 01 May 2016, 09:58

Re: Create a form to make default selected item

Post by siamandm »

the click event gives me this error :
Capture.PNG
regards
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78620
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Create a form to make default selected item

Post by HansV »

Thanks. It should be

Dim dbs As DAO.Database

as in the first piece of code that I posted.
Best wishes,
Hans

siamandm
BronzeLounger
Posts: 1267
Joined: 01 May 2016, 09:58

Re: Create a form to make default selected item

Post by siamandm »

you are Star Man ,.... thanks a lot :)
now it's working as a charm