1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Auto-fill access fields?

Discussion in 'Business Applications' started by SabreWolf3, Jul 23, 2004.

Thread Status:
Not open for further replies.
Advertisement
  1. SabreWolf3

    SabreWolf3 Thread Starter

    Joined:
    Nov 6, 2000
    Messages:
    227
    I have a table in an Access database. This table has three columns in it, and I want to add a fourth column. I want the value of this fourth column to be "Primary" for every record.

    What's the easiest way to accomplish this? I'm an Access newbie.

    -- Richard
     
  2. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,912
    Open the Table and go to design view by clicking on the little green triangle in the top left corner. Click on the first available field opening under the column "Field Name" and add the new field name. Next, set the data type for the new field. Now right click the newly added field name and select "Primary Key."

    Rollin
     
  3. SabreWolf3

    SabreWolf3 Thread Starter

    Joined:
    Nov 6, 2000
    Messages:
    227
    No, I don't want the column to be the primary key, I want all the values for this column to be the word "Primary." I just don't know how to make Access autofill the column for me. In Excel, I could put the word "Primary" in cell D2 and copy it. Then I could scroll down to cell D20,000, hit End, Shift, Up Arrow and Paste, and it would fill cells D2 to D20,000 with the word "Primary"

    Sorry for not making it clearer the first time.
    -- Richard
     
  4. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,912
    Open your database and manually add your fourth column. Then click ALT + F11 to bring up the VB editor. Next click INSERT --> MODULE and copy and paste the code below into the blank module. Make sure to change my portion of the code where it says "YOUR_TABLE_NAME" to match the actual name of your table before pasting into the module. After pasting the code, run the macro by clicking on F5. Dont worry about the number 3 in this line --> rs.Fields(3).Value = "Primary" because the fields are 0 based which means the first column has an index of 0, the second has an index of 1, etc.


    MAKE A BACKUP COPY OF YOUR DATABASE BEFORE MODIFYING IT!!!!




    Code:
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    
    Public Sub addPrimary()
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset("YOUR_TABLE_NAME")
    rs.MoveFirst
    Do Until rs.EOF = True
    rs.Edit
    rs.Fields(3).Value = "Primary"
    rs.Update
    rs.MoveNext
    Loop
    
    End Sub


    Rollin
     
  5. ScottMan

    ScottMan

    Joined:
    Aug 21, 2004
    Messages:
    2
    Open you Table in "Design View" and type "Primary" for your value in the [Default Value] found under the General tab for your new Column. Every new record will automatically be populated with the word Primary in field associated with this column. If you need to add the word Primary to records which were pre-existing to the addition of your new column, you can run an update query.
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/253669

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice