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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change default value to = last entry 1

Status
Not open for further replies.

bosun

Technical User
Feb 14, 2002
20
US
I am working on a data entry form where many of the fields remain the same. I am looking to capture the value entered in each field and use that for the default value when I go to a new record.

I have the following code:

Private Sub Comments_AfterUpdate()
'declares a variable that will be used to set the
'default value
Dim strComments As String

'sets the variable equal to the current field
strComments = Forms![Software Assets]![Comments]

'sets the default value equal to the variable
Me![Comments].DefaultValue = strComments
End Sub

I am having trouble with some of the fields on the form coming back with "Name?" I have double checked, and have the correct names for each control. I also verified the data type.

One control is a text box, another is a combo box. I made sure that the combo box is retaining it's value.

Any other ideas?

I love deadlines. I especially like the wooshing sound they make as they fly by.
-Douglas Adams
 
Doug,

Your code looks OK and should work. Make sure that you're matching the code against the name property of each control, as opposed to the ControlSource property (I usually make them the same anyway).

Another thing; if your form has a lot of controls, you might want to write a single generic function to assign a default value to a nominated control. The function could take two arguments - a Control Name, and the DefaultValue to assign. Food for thought?

HTH,


Steve Lewy
Solutions Developer
steve@lewycomputing.com.au
(dont cut corners or you'll go round in circles)
 
Thanks Doug

I did check to make sure I was using the name property in the code for each control. That is why I was confused as to why it doesn't work.

There are about 35 controls on the form. Could you tell me a bit more about the about the generic function?

Thank you again for your help.

I love deadlines. I especially like the wooshing sound they make as they fly by.
-Douglas Adams
 
Hey, my name is not Doug; your name is Doug - no wonder your program does'nt work :) only kidding

Here's some sample code that you could use to write a simple 'generic' function:

Sub SetControlDetault(YourControl AS String)

Dim CurrentControlValue AS Variant
CurrentControlValue = Me(YourControl)
Me(YourControl).DefaultValue = CurrentControlValue

End Sub

Then in the after update event of the various fields, you would simply call the sub; eg. for the Comments control, the call would be:

SetControlDefault("Comments")

I've declared the CurrentControlValue variable as a variant, as it can notionally take on different data types (eg. text, boolean) depending on the control.

Give it a go, and let me know how you fare,



Steve Lewy
Solutions Developer
steve@lewycomputing.com.au
(dont cut corners or you'll go round in circles)
 
I apologize Steve, but my name isn't Doug either. My quote comes from Douglass Adams, the author of The Hitchikers Guide to the Galaxy.

I will putter with that over the weekend, but it looks simple enough.

Thanks again for your help.

Bill Lyle

I love deadlines. I especially like the wooshing sound they make as they fly by.
-Douglas Adams
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top