Control not automatically picking up default value

G

Guest

I am attempting to set the default value of a combo box dynamically in VBA. I
do this often, but in one particular case, it is not working.

- I have verified that the default value property is being set correctly;
however, the control does not pick up the default value on new records, but
remains empty (not null, however)
- The control picks up the default value correctly if I instead set it in
the control's property sheet.
- The entire process works fine for another control on the same form.
- I have delete & recreated the faulty control, to no avail.
- The value is not being left null; I inserted the following code in
Form_Current:

If IsNull(ControlName) Then
MsgBox "Null"
Else
MsgBox ControlName.Value
End If

This does not generate "Null", but rather a runtime error 2447: "There is an
invalid use of the .(dot) or ! operator or invalid parentheses"

I can successfully set the value manually in Form_Current thus:

ControlName = ControlName.DefaultValue

Why would a particular control not be picking up its default value
automatically?
 
A

Allen Browne

The DefaultValue property is a string. Try adding quote marks to the value,
so the original value is recognised as a string value:
Me.ControlName = """" & ControlName.DefaultValue & """"
 
G

Guest

I had completely forgotten this, and the other one that was working was a
numeric value, which is why it worked without the quotes. The one that was
not working was, indeed, text. I still can't quite figure out the quadruple
sets of double quotes, but it does work.

Thanks.
 
A

Allen Browne

Embedded quotes have to be doubled-up, e.g. if you want:
This has a "word" in quotes
you use:
"This has a ""word"" in quotes"

The quadruple quotes are:
- opening quotes,
- doubled-up quotes (because embedded),
- closing quotes.
 

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