Consulting

Results 1 to 5 of 5

Thread: VBA Rookie help Request

  1. #1
    VBAX Newbie
    Joined
    Mar 2006
    Posts
    2
    Location

    VBA Rookie help Request

    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

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

    End Sub[/vba]


    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.
    Last edited by Ken Puls; 03-02-2006 at 03:52 PM.

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    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):

    [vba]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[/vba]

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

    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  4. #4
    VBAX Newbie
    Joined
    Mar 2006
    Posts
    2
    Location
    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!

  5. #5
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Quote Originally Posted by Doral
    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.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •