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!

Default Values on a form 2

Status
Not open for further replies.

RDS2

Technical User
Jun 6, 2003
44
US
How can I get a form to automatically enter data in a control (or multiple controls) based on the last entered record?

For example the form has a date field. I want that entered date to automatically populate the date field on the succeeding record after the current record is saved.
 
there several ways to do this but the easiest is in the controls after update event place code similar to this

me.textboxname.defaultvalue = "'" & me.textboxname.value & "'"

if it is a date enclose it in # signs
 
Can't seem to get this to work. What is the "me" within the code??
 
The "ME" is a shortcut for the form you are working on.
 
This is the code I've entered in the TextBox AfterUpdate property (my form name is "Tbl_Trans"):
Tbl_Trans.tran_date.defaultvalue = "'"& Tbl_Trans.tran_date.value&"'"

This produces the following error message:
"The object doesn't contain the Automation object 'Tbl_Trans' "
 
Tbl_Trans.tran_date.defaultvalue = "'"&

Try the following:

[Tbl_Trans.tran_date].defaultvalue = "'"&

An investment in knowledge always pays the best dividends.
by Benjamin Franklin
 
That doesn't seem to work either. Is there a way to set a variable to the last data entered and have the control default value set to the variable?
 
Sorry about the mistake I made. I can't guarnatee the formula, but you should put brackets [ ] to inclose your filed names if there is any type of spacing.

[Tbl_Trans].[tran_date].defaultvalue = "'"& [Tbl_Trans].[tran_date].value&"'"




An investment in knowledge always pays the best dividends.
by Benjamin Franklin
 
RDS2!

gol4's suggestion works, you cannot reference the way you do. You MUST either qualify with Forms!Formname, use the Me keyword, or access allows also usage of only the control name on current form, but not formname.controlname. So (using my preferred syntax, assuming the control name is correctly spelled):

[tt]Me!tran_date.defaultvalue = Me!tran_date.value[/tt]
or
[tt]Me!tran_date.defaultvalue = """" & Me!tran_date.value & """"[/tt]
or of course gol4's initial suggestion.

Roy-Vidar
 
Thank for the suggestions. Although when this code is placed in the control's property box and data is entered Access dipalys an error message RE: the non-existence of a macro ??
 
Aaah now I see, this is VBA code, it should be entered in the VBE;-)

When in the property box in the after update "line", click the button at right with three dots, select code builder. That would bring you to VBE within this event, where the expression should be entered. Then it should work.

Roy-Vidar
 
Thanks to those who submitted suggestions,esp. Roy and Gol4
Incidentally what do the quote, single quote items represent in the following example:
" ' " &Me!txt_box.value& " ' " ??
 
Hi again and thanx for the star!

In a lot of cases, we need to tell access what datatype we're passing. That's done thru using qualifiers. Text qualifyiers are single quotes ('), date qualifiers are hash (#), numerics doesn't need any.

The "four quotes after eachother" is a technique called "double quoting" including a quote character in the string (another version, is using the chr() function - chr(34) also produces a quote)

Here we're passing a value to a controls defaultvalue property, then one might consider telling Access what kind of value it is. What we're passing, is (in at least two of the samples) a concatinated string. Then we need quotes to "surround" string literals, and the string literals in this case, is the single qutoe.

To try it out, hit CTRL+G and try the following (press enter on each line - ensure the form is open, and that there's a value in the tran_date control):
[tt]? Forms!Tbl_Trans!tran_date.value
? """" & Forms!Tbl_Trans!tran_date.value & """"
? "'" & Forms!Tbl_Trans!tran_date.value & "'"[/tt]

To see how concatinating the string impacts the result. Try experimenting with other things too:
[tt]? Hello World
? "Hello World"
? "Hello" & " - " & "World"[/tt]
- the first one should'nt produce anything (or an error)

Roy-Vidar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top