Results 1 to 7 of 7

Thread: [RESOLVED] add a value to a certain table in MS access

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    263

    Resolved [RESOLVED] add a value to a certain table in MS access

    Hi,

    Can anyone kindly help me how to possibly change/add a value in a specific table using VBA code in MS access. Let say row1,column1 of table1, I want to put a text value of "test" when the date become Jan 19, 2007.

    In excel I know how to put value since we just have to refer to Sheet Name and cell location. How about in Ms access table using VBA code?

    Thanks!

  2. #2
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: add a value to a certain table in MS access

    Tables are different in Access than they are in Excel. You'll have to update row values using SQL or by iterating through rows in a RecordSet object.

    Method 1:
    VB Code:
    1. Const SQL as String = "UPDATE MyTable SET Value1=""THIS"" WHERE Value2=""That"";"
    2.  
    3. Currentdb.Execute SQL, vbFailOnError

    Method 2:
    VB Code:
    1. Const SQL as String = "SELECT Value1 FROM MyTable WHERE Value2=""That"";"
    2. Dim rst as RecordSet
    3.  
    4. Set rst = CurrentDB.OpenRecordSet(SQL)
    5. Do Until rst.EOF
    6.   If rst!Value1 = "This" Then
    7.     rst!Value2 = "New Value"
    8.   End If
    9. Loop

  3. #3
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: add a value to a certain table in MS access

    Make sure you have a reference to Microsoft DAO Object selected under Tools->Reference

  4. #4
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: add a value to a certain table in MS access

    Dangit, I botched the 2nd method...

    VB Code:
    1. Const SQL as String = "SELECT Value1, Value2 FROM MyTable WHERE Value2=""That"";"
    2. Dim rst as RecordSet
    3.  
    4. Set rst = CurrentDB.OpenRecordSet(SQL)
    5. Do Until rst.EOF
    6.   If rst!Value1 = "This" Then
    7.     rst.Edit
    8.     rst!Value2 = "New Value"
    9.     rst.Update
    10.   End If
    11. Loop

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    263

    Talking Re: add a value to a certain table in MS access

    Many many thanks dmaruca!!! I really appreciate your help...I really picture out what I will do with my code.

    Just clarification in the code you gave me.since im new with this... on your last code posted....

    What value1 and value2 represent? is it the column name? Am i right? Is there a way that after the code assign a new value to Value2 then it cannot be change anymore? Like the value will be locked?

  6. #6
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: add a value to a certain table in MS access

    value1 and value2 are the column names, yes. if any of your names have a space, you'll have to put brackets around them.. [value 1] [value 2]

    I'm not too experienced with record locking, so I'm not the person to come to for that.

    Good luck with your code.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    263

    Re: add a value to a certain table in MS access

    Anyway!

    Thanks a lot for the code!!!

Posting Permissions

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



Click Here to Expand Forum to Full Width