Results 1 to 7 of 7
  1. #1
    compooper is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2011
    Posts
    87

    VBA: Read table & write values to another table

    Hi folks!!

    How do you read values from a specific field in a table through vba?
    AND THEN


    Take those values and write them to another field depending on criteria like if the specific value is empty, or if it has this string, etc etc

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Common methods of getting a value include DLookup() and opening a recordset on the table. To write, common methods include the AddNew method of a recordset and executing SQL (either a saved query or SQL built in VBA).
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    my chosen method is this

    dim db as database
    dim rst as recordset
    dim ssql as string
    dim sVariable1 as string
    dim iVariable2 as long

    set db = currentdb
    ssql = "SELECT * FROM TableName WHERE (<criteria>)"
    set rst = db.openrecordset ssql
    rst.movefirst

    do while rst.eof <> true
    svariable1 = rst.fields("<textfieldname>")
    ivariable2 = rst.fields("<numberfieldname")
    loop

    For updating or inserting a record you would use something similar

    ssql = "UPDATE <UpdateTableName> SET <fieldname1> = <insert value1>, <fieldname2> = <insert value2>"
    db.execute ssql

    the add record would be similar except it would be INSERT INTO instead of UPDATE.

    If you are updating a record on the current recordset you could use

    rst.edit
    rst.fields("<Fieldname>").value = <new value>
    rst.update

    rst.update

  4. #4
    compooper is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2011
    Posts
    87
    okay thanks.
    Is there a way though to have conditions set for the values you read.
    i.e.
    If CoID = 6 Or CoID = 8 Then

    Do While rst.EOF <> True
    TTDescr = rst.Fields("Testtable.Svc_description")
    TTCode = rst.Fields("Testtable.Svc_code")
    Loop
    But I don't want to read every value in Testtable.Svc_description, only the ones that are NOT NULL.

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Have you tried to do this with an Update query? Just a thought.

  6. #6
    compooper is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2011
    Posts
    87
    Yes I have.

    I usually use Update Queries, but the problem I am having is with the inner join I want to use.

    I was hoping in the INNER JOIN I could use multiple IIF statements, but its not allowed.

    So I'm guessing VBA is the way to go

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Quote Originally Posted by compooper View Post
    Yes I have.

    I usually use Update Queries, but the problem I am having is with the inner join I want to use.

    I was hoping in the INNER JOIN I could use multiple IIF statements, but its not allowed.

    So I'm guessing VBA is the way to go
    The general syntax for what I'm suggesting (a sample from my test db)
    Code:
    UPDATE table4 AS A
     INNER JOIN table5 AS B 
    ON A.SalesCode = B.SalesCode 
    SET A.SalesValue = B.salesvalue
    WHERE (((A.SalesCode)=4)) OR
     (((A.SalesCode)=5));

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 05-11-2011, 02:51 AM
  2. Some Can Read; Some Can Write
    By cassidym in forum Security
    Replies: 3
    Last Post: 08-19-2010, 02:19 PM
  3. Replies: 1
    Last Post: 06-24-2010, 08:08 AM
  4. ODBC table read to blank Access table write
    By dibblejon in forum Access
    Replies: 2
    Last Post: 03-10-2010, 08:39 AM
  5. Replies: 2
    Last Post: 10-19-2006, 04:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums