Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using an expression as a default value in Access form 1

Status
Not open for further replies.

TulsaJeff

Programmer
Jan 29, 2001
870
US
In MS Access 97 I have 3 combo boxes in a form... Day, Month, and Year respectively. The boxes are pulling the info from 3 separate tables of the same names.

I set up default values in the properties of each combo box as follows:

=Day(Now())
=Month(Now())
=Year(Now())

This should show the current info but it does not. It shows nothing. Ihave to click on the down arrow to drop the list down before I see anything.

What is the problem? Any Ideas? Ya' Gotta Love It!:)X-)
 
If they are really pulling info from a table, you don't want a default value - the value shown on the combobox will be the corresponding value for the record you are displaying.

If you are creating a new record, it will be blank - there is no data for that record in the table.

There is a risk that you will trash your own data - let's say you created a record last week, and entered the date then, if you set the default value for the combo box as now(), it may overwrite your original data.

I think what you want to do is have the default value for new records, but without affecting data in old records? This would some kind of 'default if blank', rather than just a default value?

If that sounds right, let me know and I'll give you some ideas.

 
Yes...we are strictly talking about entering new data. Populating a database with daily info. I just figured instead of having to do the drop-down and specify april...it would already know this is April.

The table it is pulling data from is just a list of the 12 months in a year to allow the user to choose one instead of typing it in. Ya' Gotta Love It!:)X-)
 
OK, so what we want to do is have the current month entered for a blank record, but ignored if it's already there.

What needs to be bourne in mind is that the combobox and the field it is linked to are two different objects. Have you tried setting the default value for the field, rather than the combobox? I assume there is a field linked to it - if not you're in big trouble!! I think that might work.

If that doesn't work, provided that the month is not the first field you work with, you can put the following code on an event on a field you use before the month:

if isnull (monthfield.value) = true then monthfield.value now()

(try it with afterupdate on a preceeding field).

You could also substitute the fieldname with the combobox name, but I suspect it would display the value, but not enter it.










 
Sorry, misstyped - should read

if isnull (monthfield.value) = true then monthfield.value = now()

(= was missing).

(And I know I'll get loads of abuse for improper use of 'if 'endif', but I can read it OK . . .)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top