Can't set Default from Code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to change the default of a combo box from my code. But everytime
i close it, it goes back to the previous typed in default. Bellow is my
code. I don't have any idea why it doesn't work. It changes the default,
but when i close it and open it, it doesn't keep the changes.

Thank you,

Private Sub cmb_squadron_UIC_AfterUpdate()

Debug.Print Me.cmb_squadron_UIC.DefaultValue
Debug.Print Chr(34) & Me.cmb_squadron_UIC.Value & Chr(34)

Me.cmb_squadron_UIC.DefaultValue = Chr(34) & Me.cmb_squadron_UIC.Value &
Chr(34)
Me.Refresh
DoCmd.Save acForm, "SquadronMenu"

End Sub
 
Hi Nevie and/or Phil

You need to have the form open in design view for it to "remember" the
default value you have set for a control.

The easiest way to do this is to use the form's Unload event to save the
current DefaultValue to a table, and then load it again the next time the
form opens (in the Load event).

If you don't want to use a table, you could save it as a custom database
property. Check out "CreateProperty" in the help.
 
Also, it looks like you are trying to set it to the last selected value.
Another thing you could do it create a recordsetclone, move to the last
record, get the value entered, then set that as the default for the combo
box in the Form Current event.

This will work simply because when the form it opened, it will run this code
as well, and thus make the default, the same as the last value in the
recordset.

That has it's drawbacks, too, but it's an option.

--
Troy

Troy Munford
Development Operations Manager
FMS, Inc.
www.fmsinc.com


Hi Nevie and/or Phil

You need to have the form open in design view for it to "remember" the
default value you have set for a control.

The easiest way to do this is to use the form's Unload event to save the
current DefaultValue to a table, and then load it again the next time the
form opens (in the Load event).

If you don't want to use a table, you could save it as a custom database
property. Check out "CreateProperty" in the help.
 
I failed to mention that I was making an ADP, but your advice was very
insightful and help push me over the cliff of access ignorance for this
problem. I made it so that it goes into the design mode, then changes it,
and opens back up in normal mode, and it works great for this particular
situation.

the Createproperty looks cool, but it said for jet and i'm using SQL 2000 as
my back end. I don't think i can make local tables with ADP's... (if i am
wrong, please tell me!...)

Thank you very much!



Graham Mandeno said:
Hi Nevie and/or Phil

You need to have the form open in design view for it to "remember" the
default value you have set for a control.

The easiest way to do this is to use the form's Unload event to save the
current DefaultValue to a table, and then load it again the next time the
form opens (in the Load event).

If you don't want to use a table, you could save it as a custom database
property. Check out "CreateProperty" in the help.
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

Nevie and Phil said:
I'm trying to change the default of a combo box from my code. But
everytime
i close it, it goes back to the previous typed in default. Bellow is my
code. I don't have any idea why it doesn't work. It changes the default,
but when i close it and open it, it doesn't keep the changes.

Thank you,

Private Sub cmb_squadron_UIC_AfterUpdate()

Debug.Print Me.cmb_squadron_UIC.DefaultValue
Debug.Print Chr(34) & Me.cmb_squadron_UIC.Value & Chr(34)

Me.cmb_squadron_UIC.DefaultValue = Chr(34) & Me.cmb_squadron_UIC.Value &
Chr(34)
Me.Refresh
DoCmd.Save acForm, "SquadronMenu"

End Sub
 
Back
Top