Results 1 to 8 of 8
  1. #1
    ducthang88 is offline Novice
    Windows XP Access 2007
    Join Date
    Dec 2010
    Posts
    12

    Help about access INSERT INTO statement

    First of all. Ive created 2 tables
    book and borrowed (table)


    title,author (both have the same fields)
    IN the form of book, theres a "borrow" (command1) button wherin when i press it, the data of the fields (title and author) will go to the table borrowed...



    please revise my coding which sucks


    Private Sub Command1_Click()

    DoCmd.RunSQL = "INSERT INTO borrowed (Author,Title) VALUES (" & Me!Author & ", " & Me!Title & ")"


    End Sub

  2. #2
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    Use:
    Dim strSQL As String
    strSQL = "Insert into borrowed (Author,Title) Values('" & Me.Author & "','" & Me.Title & "')"
    CurrentDb.Execute strSQL, dbFailOnError

    or

    Dim strSQL As String
    strSQL = "Insert into borrowed (Author,Title) Values('" & Me.Author & "','" & Me.Title & "')"
    DoCmd.SetWarnings False
    DoCmd.RunSQL strSQL
    DoCmd.SetWarnings True

    The first option is preferred

    If this solves your problem mark it solved

  3. #3
    ducthang88 is offline Novice
    Windows XP Access 2007
    Join Date
    Dec 2010
    Posts
    12
    why is it on error the first one?

    method or data not found?

    maximus can you please add my ym? i need help pretty bad.. its anjo_00000@yahoo.com

  4. #4
    ducthang88 is offline Novice
    Windows XP Access 2007
    Join Date
    Dec 2010
    Posts
    12
    i already found the problem on my post

    now i have a new problem

    it says error on the
    error syntax in INSERT INTO statement and it points out
    ->> CurrentDb.Execute strSQL, dbFailOnError

  5. #5
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    I was offline for a while.
    when u are trying to insert strings they have to be enclosed with '.

    To use currentdb.Execute u need to set reference to the Microsoft DAO Object Libary. In the VBA editor go to Tools - Preferences - Check Microsoft DAO object Libary.

    Or try this:

    Dim dbs As DAO.Database, strSql as String
    Set dbs = CurrentDb
    strSql = "Insert into borrowed (Author,Title) Values('" & Me.Author & "','" & Me.Title & "')"
    dbs.Execute strSql, dbFailOnError

  6. #6
    ducthang88 is offline Novice
    Windows XP Access 2007
    Join Date
    Dec 2010
    Posts
    12
    it worked now! thanks! by the way.

    I have another question..

    My form (main form) has 2 subforms

    Is it possible for me to put a code INSERT INTO in my first subform then it will call the data in the main form and transfer it into the 2nd subform?

  7. #7
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    ducthang88 anything is possible please be a little more specifics will certainly help. I will be logging of soon, but don't worry there are plenty of guys who will be happy to assist you.

  8. #8
    ducthang88 is offline Novice
    Windows XP Access 2007
    Join Date
    Dec 2010
    Posts
    12
    this is the scenario... I have a form named Borrower, Books and Borrowedbooks...

    From the form Books, i have a command button which what i want is to select the current value on the textbox (BorrowerNo) in the form Borrower and put it in the (BorrowerNo) field in the Borrowedbooks...

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

Similar Threads

  1. Insert Image in Access 2010 ???
    By cowboy in forum Access
    Replies: 5
    Last Post: 03-18-2014, 08:22 AM
  2. Insert Into Statement with Line Numbers
    By Cheshire101 in forum Programming
    Replies: 1
    Last Post: 10-11-2010, 02:53 PM
  3. Replies: 4
    Last Post: 09-03-2010, 02:55 PM
  4. Access insert into query...
    By xmetisx in forum Queries
    Replies: 2
    Last Post: 04-06-2010, 02:27 PM
  5. Syntax Error In Insert Into Statement
    By frankvfox in forum Queries
    Replies: 1
    Last Post: 09-09-2008, 12:35 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