PDA

View Full Version : VBA Rookie help Request



Doral
03-02-2006, 02:41 PM
Hiya folks, I am just starting to learn VBA with no real programming experience ( 2 quarters of PASCAL).

I hope this is the proper area to ask this question

What am I trying to do is use form fields to allow a user to put a date in one field and then it automagically add it to other fields that require the date.

On a macro that will run on entry on the Date 1 form field, I have this so far

Sub CopyField()
Dim Temp As String
Temp = ActiveDocument.FormFields("Date1").Result
ActiveDocument.FormFields("Date2").Result = Temp

End Sub


What I am wondering is if I have to continue to repeat the
"ActiveDocument.FormFields("Date2").Result = Temp" and replace Date2 with Date3, Date4, etc until I have all fields accounted for or is there a way to do it that requires less code? Like I said I'm no programmer but I know least lines of code is best on such things.

Thanks for the help in advance.

Ken Puls
03-02-2006, 03:51 PM
Hi Doral, and welcome to VBAX!

I haven't tried your code, but assuming that it is working as intended, and just needs to be exapnded, try this (untested):

Sub CopyField()
Dim lFlds As Long
With ActiveDocument
For lFlds = 2 To 10 'Change 10 to the total number of fields
.FormFields("Date" & lFlds).Result = .FormFields("Date1").Result
Next lFlds
End With
End Sub

You'll need to update 10 to however many fields you have.

HTH,

TonyJollans
03-03-2006, 04:47 AM
Hi Doral,

Welcome to VBAX!

Much as I love code, and want people to learn to use it, you probably don't need it for this.

Form Fields should only normally be used for user input so unless you just want to use an input date in one field as a default in others (which can be complex if fields are later changed) then you don't want to be using Form Fields for the target dates - use REF Fields instead.

All you need to do is set the (first) date input field to "Calculate On Exit" and the REF Fields shoud update automatically whenever the Form Field is changed.

Doral
03-03-2006, 07:21 AM
Thanks for the help guys, not to dismiss your code Ken, but Tony's suggestion seems to have worked out better for this particular case. Off to track down the next series of problems building this document. Thanks again to both of you!

Ken Puls
03-03-2006, 09:39 AM
Thanks for the help guys, not to dismiss your code Ken, but Tony's suggestion seems to have worked out better for this particular case. Off to track down the next series of problems building this document. Thanks again to both of you!

What, are you kidding? Had I known what Tony does, I would have made the same suggestion. ;)

Always better to go the non-code route if it is available. :)