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

Whre to place code ?

Status
Not open for further replies.

greendragon51

Technical User
Jul 29, 2002
8
0
0
US
Ok.... I'm a newbie at the coding stuff. But everbody had to start somewhere, right? I need to have a fields default value be the same as the previous records untill the user changes it. I have researched in these forums and found the following...
Private Sub Dept_Division_GotFocus()
;If this is a new record, default to the value
;of Dept_Division of previous record.

Dim NewRec As Integer
Dim Frm As Form
Set Frm=Screen.ActiveForm ;Set Frm to the active form.

;Is the current record a new record?

NewRec=Frm.NewRecord
If NewRec=True Then
If IsNull([Dept_Division]) Then
SendKeys("^'")
End If
End If

The above code will place the value of the last record into the field Dept_Division as soon as Dept_Division gets focus.

Thanks very much to MrBILLSC for this work. ( Dec of 01 )

Now.... just where do I insert this code to make it work?
( Access 2000, Win XP and Win98SE )

Thanks so very much for helping a new guy!
Old Guy
 
I always recommend in the fields after update_event
but rather then the code above I prefer

me!yourfieldname.defaultvalue = "'" & me!yourfield.value &"'"

as soon as a record is changed the default value changes to what ever you value is in the field

it is important to remember the single quotes and also be sure it is placed in as code not a macro
 
Thanks yo so very much for your prompt reply gol4. I sure do appreciate it. However you assume too much methinks. I understand place the code in after_update event... but please bear with this newbie to Access.... is this a property of a Form, or available in Design View of a table... where. Please explain... in what windows do I open a form, or a table... then what? ( I am from the Alpha 5 school where this feature was part of a "calculated" field, I see in Access that I MUST run a querry to create "Calculated" fields... I'll get to that later... I hope...)
Thanks so very much...
Old Green Dragon guy..
 
while in the forms design view
open the properties window
click on the field that you want to have the value brought forward. It should show that field name in the top of the property box
there are tabs in the properties box and one of there is the events. Click on the events tab
close to the top of the top it shoud have a field labeled After update
left click your mouse on the field as soon as you do that off to the right of the field 3 dots will appear. Click on those dots. Select code builder from the 3 choices that are offered
place you code inbetween the two lines

Private Sub field1_AfterUpdate()
Code goes here
End Sub

again you will need to replace the field names in the code to match what ever your field names are. So if you fields anme is field1 the code would look like this

Private Sub field1_AfterUpdate()
me!field1.defaultvalue = "'" & me!field1.value &"'"
End Sub

hope that answers your questions

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top