Default Value in Combox on Open

D

Dkline

I've been trying to set a default value in a combobox when a form first
loads. I've tried by simply setting the default value directly in the
property sheet i.e. without using code but some process seems to overwrite
the default value in the property sheet.

So I'm using this code:

Private Sub Form_Open(Cancel As Integer)

Me![cmbProgram].DefaultValue = "Whatever"

End Sub

Is there a better way to do this?

The above code works on my machine but not on my boss's machine. I've been
trying to debug starting from the above Form_Open but that is a dead end.

How can I debug through each process that may cause the change in the
default value when they are not directly connected?
 
B

Brendan Reynolds

Access can be picky about quotes when setting the DefaultValue property. Try
....

Me![cmbProgram].DefaultValue = Chr$(34) & "Whatever" & Chr$(34)

I also have some vague memory that I might have got more reliable results
using the Load event rather than the Open event, but I couldn't swear to
that.

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
A

Allen Browne

DefaultValue is a string property, so you could try:
Me![cmbProgram].DefaultValue = """Whatever"""

If the combo is unbound, it would be easier to just assign a value to the
control:
Me![cmbProgram] = "Whatever"

If it is bound, the default value will not apply until the form moves to a
new record, i.e. DefaultValue is NOT used for existing records where the
value is Null.
 
D

Dkline

I had not considered the distinction of bound or unbound. This particular
box is unbound.

I'm glad you reminded me as I had not noticed that the combobox and
recordset were out of synch until the user made a selection in the combobox
(which had code for synchronization upon selection).

Private Sub Form_Open(Cancel As Integer)
Dim rs As Object
Me![cmbProgram] = "Whatever"
Me.Filter = ""
Set rs = Me.Recordset.Clone
rs.FindFirst "[Program] = '" & Me![cmbProgram] & "'"
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
End Sub

Thank you and to Brendan Reynolds for your replies.

Allen Browne said:
DefaultValue is a string property, so you could try:
Me![cmbProgram].DefaultValue = """Whatever"""

If the combo is unbound, it would be easier to just assign a value to the
control:
Me![cmbProgram] = "Whatever"

If it is bound, the default value will not apply until the form moves to a
new record, i.e. DefaultValue is NOT used for existing records where the
value is Null.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Dkline said:
I've been trying to set a default value in a combobox when a form first
loads. I've tried by simply setting the default value directly in the
property sheet i.e. without using code but some process seems to
overwrite the default value in the property sheet.

So I'm using this code:

Private Sub Form_Open(Cancel As Integer)

Me![cmbProgram].DefaultValue = "Whatever"

End Sub

Is there a better way to do this?

The above code works on my machine but not on my boss's machine. I've
been trying to debug starting from the above Form_Open but that is a dead
end.

How can I debug through each process that may cause the change in the
default value when they are not directly connected?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top