Setting Default Value for a control with Visual Basic

M

Mark

I am working on an Access 97 SR2 database

I wrote some code on the On Current Event of a main form
and it is supposed to change the row source and default
value of a combo box on a subform.

It works fine changing the Row Source, however whenever it
runs, rather than successfully changing the default value
it has #NAME# appear in the combo box for new blank
records.

What am I doing wrong?? (Code is below)

Thanks if you have an answer!!

Mark
Ontario, Canada

If Me.ClaimTypeID = 2 Then
Forms![FormName]![SubformName].Form!
[Supplier].RowSource = "QueryName1"
Forms![FormName]![SubformName].Form!
[Supplier].Requery
Forms![FormName]![SubformName].Form!
[Supplier].DefaultValue = "StringValue1"
Else
Forms![FormName]![SubformName].Form!
[Supplier].RowSource = "QueryName2"
Forms![FormName]![SubformName].Form!
[Supplier].Requery
Forms![FormName]![SubformName].Form!
[Supplier].DefaultValue = "StringValue2"
End If
 
A

Allen Browne

I'm guessing that you have used the lookup wizard in your table, and that it
is causing confusion about the data type of the field the combo is bound to.

Which is the Bound column of the combo?
QueryName1 probably draws values from a table.
Open that table in design view.
What is the Data Type of the field in the Bound Column of the combo?
If it is Number, you need to supply a Number as the Default Value, not text,
e.g:
Forms![FormName]![SubformName].Form![Supplier].DefaultValue = "1"

You can also drop the Requery, so just use:
With Me.SubformName.Form!Supplier
.RowSource = "QueryName1"
.DefaultValue = "1"
End With
 
J

Jim Evans

And, If the table field is a text type of field, check the length of the
field and the values you are trying to supply.

Jim Evans

Allen Browne said:
I'm guessing that you have used the lookup wizard in your table, and that it
is causing confusion about the data type of the field the combo is bound to.

Which is the Bound column of the combo?
QueryName1 probably draws values from a table.
Open that table in design view.
What is the Data Type of the field in the Bound Column of the combo?
If it is Number, you need to supply a Number as the Default Value, not text,
e.g:
Forms![FormName]![SubformName].Form![Supplier].DefaultValue = "1"

You can also drop the Requery, so just use:
With Me.SubformName.Form!Supplier
.RowSource = "QueryName1"
.DefaultValue = "1"
End With

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

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

Mark said:
I am working on an Access 97 SR2 database

I wrote some code on the On Current Event of a main form
and it is supposed to change the row source and default
value of a combo box on a subform.

It works fine changing the Row Source, however whenever it
runs, rather than successfully changing the default value
it has #NAME# appear in the combo box for new blank
records.

What am I doing wrong?? (Code is below)

Thanks if you have an answer!!

Mark
Ontario, Canada

If Me.ClaimTypeID = 2 Then
Forms![FormName]![SubformName].Form!
[Supplier].RowSource = "QueryName1"
Forms![FormName]![SubformName].Form!
[Supplier].Requery
Forms![FormName]![SubformName].Form!
[Supplier].DefaultValue = "StringValue1"
Else
Forms![FormName]![SubformName].Form!
[Supplier].RowSource = "QueryName2"
Forms![FormName]![SubformName].Form!
[Supplier].Requery
Forms![FormName]![SubformName].Form!
[Supplier].DefaultValue = "StringValue2"
End If
 

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