ComboBox

U

Utopian

Hello!

I have a ComboBox, and I need that remember the last value chosen; remain this value ever by default, without having to unfold the combobox...

The values are taken from a table with ID(autonumber) and Description(text)

Thanks!

Me.-
 
B

Brendan Reynolds

Option Compare Database
Option Explicit

Private Sub cboTest_AfterUpdate()

Dim aob As AccessObject

'If testing in a new form, save form first, or next line will raise
error.
Set aob = CurrentProject.AllForms(Me.Name)

'The help file says that adding a property with the same name
'as an existing property will raise an error, but the help file
'is wrong - the new value silently overwrites the old value.
aob.Properties.Add "cboTestDefaultValue", Me.cboTest.Value

End Sub

Private Sub Form_Load()

Dim aob As AccessObject
Dim aop As AccessObjectProperty

Set aob = CurrentProject.AllForms(Me.Name)

'Handle situation where property does not exist - e.g. this
'is the first time the form has been opened, and the value of
'the combo box has never been changed.
For Each aop In aob.Properties
If aop.Name = "cboTestDefaultValue" Then

'Default value is a string property (regardless of underlying
'field data type) and needs quotes around it. Chr$(34) specifies
'the double quote character (").
Me.cboTest.DefaultValue = Chr$(34) & aop.Value & Chr$(34)
Exit For
End If
Next aop

End Sub

--
Brendan Reynolds (MVP)

Hello!

I have a ComboBox, and I need that remember the last value chosen; remain
this value ever by default, without having to unfold the combobox...

The values are taken from a table with ID(autonumber) and Description(text)

Thanks!

Me.-
 
Top