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

Design VBA Code to Default Previous Value

Status
Not open for further replies.

Serpica

Technical User
Jul 7, 2000
4
CA
--------------------------------------------------------------------------------<br>I need a code to represent that if I press &quot;Enter&quot; in a field on my form AND the field is left blank, Access will copy the value of the previous record for that field.<br>Example:<br><br>First Record - Field #1 - Entry - &quot;Testing&quot;<br>Second Record - Field #1 - Leave Blank & Press Enter.<br>Result - Second Record Field #1 Now Becomes &quot;Testing&quot; by Default.<br><br>Any suggestions.<br><br>
 
Set module level (private) variables for all the fields you wish to make the same. Do this in the General Declaration section of the form.<br><br>When the user goes to a new record, set the variables equal to the fields.<br><br>Now when the user enters through the field, on the On Lost Focus event of the field, you can set the field equal to it's equivalent variable.<br><br>HTH <p>Jim Lunde<br><a href=mailto:compugeeks@hotmail.com>compugeeks@hotmail.com</a><br><a href= Application Development
 
Serpica<br>How about using the DLast() function?<br><br>In your form, set the Default Value for the field you want automatically filled in to<br>=DLast(&quot;Field#1&quot;,&quot;YourTableName&quot;)<br><br>I know this works for dates. I haven't tried it for text fields. You also have to use a &quot;Single Form&quot; data entry rather than Datasheet or Continuous From for it to work well.<br>Tom<br>
 
Jimmythegreek<br><br>I have tried different codes but am unsure which code to use in the LostFocus Action.&nbsp;&nbsp;Could you please help figure out the code I would need say for the field &quot;Date&quot;.
 
Let's say you have four fields on your form: Field1, Field2, Field3, Field4<br>In your General Declaration Section you have four private variables: mintVar1, mlngVar2, mdatVar3, mstrVar4<br><br>On the BeforeUpdate code of your form put the following code:<br><br>With Me<br>&nbsp;&nbsp;&nbsp;mintVar1 = Me.Field1<br>&nbsp;&nbsp;&nbsp;mlngVar2 = Me.Field2<br>&nbsp;&nbsp;&nbsp;mdatVar3 = Me.Field3<br>&nbsp;&nbsp;&nbsp;mstrVar4 = Me.Field4<br>End With<br><br>Now on the lost focus of each of the fields, put the following, just changing the number:<br><br>Private Sub Field1_LostFocus()<br>&nbsp;&nbsp;&nbsp;If Nz(Me.Field1) = &quot;&quot; Then Me.Field1 = mintVar1<br>End Sub<br><br>The code would expand depending on how many fields you neede to do this to. Also, I would suggest giving the fields and variables more descriptive names.<br> <p>Jim Lunde<br><a href=mailto:compugeeks@hotmail.com>compugeeks@hotmail.com</a><br><a href= Application Development
 
In my form I have seven fields I need to use this code for (Date, Job #, Class, Activity, Cost Type, Account, & Comment). I have entered the following code:<br><br>Private Sub Date_LostFocus()<br>&nbsp;&nbsp;&nbsp;&nbsp;If Nz(Date) = &quot;&quot; Then Me.Date = mdatVar1<br>End Sub<br><br>Private Sub Job___LostFocus()<br>&nbsp;&nbsp;&nbsp;&nbsp;If Nz(Job__) = &quot;&quot; Then Me.Job__ = mjobVar2<br>End Sub<br><br>Private Sub Class_LostFocus()<br>&nbsp;&nbsp;&nbsp;&nbsp;If Nz(Class) = &quot;&quot; Then Me.Class = mclaVar3<br>End Sub<br><br>Private Sub Activity_LostFocus()<br>&nbsp;&nbsp;&nbsp;&nbsp;If Nz(Activity) = &quot;&quot; Then Me.Activity = mactVar4<br>End Sub<br><br>Private Sub Cost_Type_LostFocus()<br>&nbsp;&nbsp;&nbsp;&nbsp;If Nz(Cost_Type) = &quot;&quot; Then Me.Cost_Type = mcosVar5<br>End Sub<br><br>Private Sub Account_LostFocus()<br>&nbsp;&nbsp;&nbsp;&nbsp;If Nz(Account) = &quot;&quot; Then Me.Account = maccVar6<br>End Sub<br><br>Private Sub Comment_LostFocus()<br>&nbsp;&nbsp;&nbsp;&nbsp;If Nz(Comment) = &quot;&quot; Then Me.Comment = mcomVar7<br>End Sub<br><br>Private Sub Form_BeforeUpdate(Cancel As Integer)<br>With Me<br>&nbsp;&nbsp;&nbsp;mdatVar1 = Me.Date<br>&nbsp;&nbsp;&nbsp;mjobVar2 = Me.Job__<br>&nbsp;&nbsp;&nbsp;mclaVar3 = Me.Class<br>&nbsp;&nbsp;&nbsp;mactVar4 = Me.Activity<br>&nbsp;&nbsp;&nbsp;mcosVar5 = Me.Cost_Type<br>&nbsp;&nbsp;&nbsp;maccVar6 = Me.Account<br>&nbsp;&nbsp;&nbsp;mcomVar7 = Me.Comment<br>End With<br>End Sub<br><br>When I try to use the form, if I skip over date it returns a different date then the previous one.&nbsp;&nbsp;All of the other fields return an error message stating: &quot;Field...&quot; cannot be zero-length string.&quot;<br><br>Could you please advise where my problem is.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top