Results 1 to 8 of 8
  1. #1
    zipaway is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    53

    Button to add text from textbox as new record in database

    So I have a form with an unbound textbox named (ProjectNumber) and a button (AddProjectNumberButton). When I click the button I want the text from the textbox added as a new record to the (Projects) table under the (Project_Number) field.



    I tried the code below but it didn't work. It screwed up on the "Dim RS As DAO.Recordset" line. Any help would greatly be appreciated.

    Code:
    Private Sub AddProjectNumberButton_Click()
    Dim RS As DAO.Recordset
    Set RS = CurrentDb.openrecordset(Projects, dbopendynaset)
    RS.AddNew
    RS("Project_Number") = Me![ProjectNumber]
    RS.Update
    RS.Close
    Set RS = Nothing
    End Sub

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Try: RS.fields("Project_Number").value = Me![ProjectNumber]

    You dont need this datatype DAO.RECORDSET...just use: Dim RS)
    (its not needed, but if you do use it, be sure you have in your REFERENCES )

  3. #3
    zipaway is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    53
    Quote Originally Posted by ranman256 View Post
    Try: RS.fields("Project_Number").value = Me![ProjectNumber]

    You dont need this datatype DAO.RECORDSET...just use: Dim RS)
    (its not needed, but if you do use it, be sure you have in your REFERENCES )
    So I did that, now the second line has an error.

    Code:
    Dim RS
    Set RS = CurrentDb.openrecordset(Projects, dbopendynaset)
    RS.AddNew
    RS.fields("Project_Number").Value = Me![ProjectNumber]
    RS.Update
    RS.Close
    Set RS = Nothing

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    You actually dont need ANY code. The button should run an update query, Using the value in the box. (Zero coding!)

  5. #5
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Code:
    dim db
    dim sSQL as string
    set db = currentdb
    sSQL = "INSERT INTO Projects (Project_Number) VALUES (" & [projectnumber] & ")"
    db.execute ssql
    set db = nothing

  6. #6
    zipaway is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    53
    Quote Originally Posted by ranman256 View Post
    You actually dont need ANY code. The button should run an update query, Using the value in the box. (Zero coding!)
    Wow I didn't think of that, but how do you link the textbox to the Update Query???

  7. #7
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    it's not actually linking if you're just appending a record

    you create an append query, add the table you want, add the field you want to append to and put [forms]![formname]![fieldname] in the FIELD row.

  8. #8
    zipaway is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    53
    Quote Originally Posted by rpeare View Post
    it's not actually linking if you're just appending a record

    you create an append query, add the table you want, add the field you want to append to and put [forms]![formname]![fieldname] in the FIELD row.
    Thanks, this worked.

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

Similar Threads

  1. Replies: 12
    Last Post: 03-27-2014, 04:49 AM
  2. Find a record using a TextBox & Button
    By ledis in forum Access
    Replies: 6
    Last Post: 02-02-2014, 02:47 PM
  3. Replies: 8
    Last Post: 06-10-2013, 01:39 PM
  4. Use cmd button to fill textbox
    By shariq1989 in forum Forms
    Replies: 1
    Last Post: 07-19-2012, 08:59 AM
  5. Replies: 6
    Last Post: 02-09-2010, 07:53 AM

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