Results 1 to 7 of 7
  1. #1
    bacchi
    Guest

    Populating Fields from previous fields

    I would like to populate several fields in a record with data from other fields in the same record. As an example, I would like to fill fields for a mailing address (company, address, city, state, zip) from the fields containing the street address already entered. I would trigger such an event with a checkbox. Can't seem to find the solution to this.

  2. #2
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Populating Fields from previous fields

    Hi,
    There are several ways to do this. The easiest is to use macros. You could use the SetValue macro to set the value of the Target field to the value of the source field.
    In the macro, the "Item" argument would be the target item (new field - i.e. "[Forms]![Form1]![txtName2]") and the "Expression" argument would be the source item (original field - i.e. "[Forms]![Form1]![txtName]"). You would have to create an individual line for each text field you wanted to populate. The final step is assigning the "On Click" event of the checkbox to run the macro.

    OR If you're comfortable with code, you could set the values of the target fields with the current values of the source fields on the Click event of the checkbox.
    The code would be as follows:

    Private Sub chkPopulateSecondFields_Click()
    txtName2 = txtName 'or whatever you called your fields
    txtAddress2 = txtAddress
    txtCity2 = txtCity
    txtState2 = txtState
    txtZIP2 = txtZIP
    'and so on....
    End Sub

    Hope this helps,[img]/w3timages/icons/thinks.gif[/img]
    -MarkJ-

  3. #3
    2 Star Lounger
    Join Date
    Dec 2000
    Posts
    188
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Populating Fields from previous fields

    Even if you're not comfortable with code, check out this solution at The Access Web

    By using the method found there, which is basically setting the default value of a control (field on a form) based on what has been typed in the control, you can run the code conditionally with your check box, or what I like to do is set the code to run on the afterupdate event of the control being filled in.

    Basically it will keep using what was typed as the default value until something different is typed, then it will start using that value. Thus no need for the checkbox.

    HTH, FWIW
    The link above is to http://www.mvps.org/access/forms/frm0012.htm

  4. #4
    Mike B
    Guest

    Re: Populating Fields from previous fields

    one thing mark hasnt allowed for is what if the user changes their mind and wants to uncheck the box....this code should solve that problem:
    Private Sub chkPopulateSecondFields_Click()
    If chkPopulateSecondFields= -1 Then 'the box is checked
    txtName2 = txtName 'or whatever you called your fields
    txtAddress2 = txtAddress
    txtCity2 = txtCity
    txtState2 = txtState
    txtZIP2 = txtZIP
    'and so on....
    Else 'the checkbox is unchecked or equals 0
    txtName2 = ""
    txtAddress2 = ""
    txtCity2 = ""
    txtState2 = ""
    txtZIP2 = ""
    'and so on....
    End If
    End Sub

  5. #5
    New Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Populating Fields from previous fields

    <img src=/S/blank.gif border=0 alt=blank width=15 height=15>
    I found the idea of using a checkbox to populate various fields for a mailing address from already entered street address fields very useful.
    I tried the code suggested by Mike B and it works fine, except that when I move on to a new record, the checkbox remains either checked or unchecked, depending on what I did with the checkbox in the previous record, ie if I checked the box in the previous record, the box is checked when I move on to a new record, or if I left the checkbox blank, the box is unchecked in the new record.
    Ideally, I would like the checkbox to be unchecked when entering new records.
    I have tried setting the Default Value of the check box to 0 (unchecked) through the checkbox properties with no success.
    By the way, the checkbox is unbound, if that has anything to do with it. I am also using Access 97.
    Any ideas?

  6. #6
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Populating Fields from previous fields

    In the OnCurrent event of the form, put in a line to set the checkbox to false. Since you're using an unbound checkbox, it retains its value between records and you have to tell it to reset itself when you move to a new record.
    Charlotte

  7. #7
    New Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Populating Fields from previous fields

    Thanks Charlotte. That did the trick. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

Posting Permissions

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