Results 1 to 5 of 5
  1. #1
    sleptema is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2013
    Posts
    6

    INSERT INTO problems

    Hey Guys.

    I am having issues with the INSERT INTO function. I have torn through google for the last 3 hours and I cannot find a resolve. Basically everytime I initiate the code, it brings up the table but it hasn't added any details in. I seriously can't figure out what is wrong.



    Code:
     DoCmd.OpenTable "tblTerrainRepairJobs", acViewNormal, acAdd
                DoCmd.SetWarnings False
                Dim CustomerString, SiteString As String
                CustomerString = cboCustomer.Value
                SiteString = cboSite.Value
                DoCmd.GoToRecord acDataTable, "tblTerrainRepairJobs", acNewRec
                Dim StrSQL As String
                StrSQL = "INSERT INTO tblTerrainRepairJobs (Customer, Site) VALUES ('" & CustomerString & "', '" & SiteString & "')"
                DoCmd.RunSQL StrSQL
                'DoCmd.Close acTable, "tblTerrainRepairJobs", acSaveYes
    I've tried changing values so often in so many combinations. I am completely out of ideas.

    And yes, I am very new to this.

    Cheers for any help.

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Not sure what you are trying to do, but if are trying to add a new record to table tblTerrainRepairJobs, with the data coming from a form, try this:

    Code:
                Dim CustomerString As String
                Dim SiteString As String
                Dim StrSQL As String
    
                CustomerString = Me.cboCustomer
                SiteString = Me.cboSite
     
                StrSQL = "INSERT INTO tblTerrainRepairJobs (Customer, Site) VALUES ('" & CustomerString & "', '" & SiteString & "')"
                CurrentDB.Execute StrSQL, dbfailonerror

  3. #3
    sleptema is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2013
    Posts
    6
    Quote Originally Posted by ssanfu View Post
    Not sure what you are trying to do, but if are trying to add a new record to table tblTerrainRepairJobs, with the data coming from a form, try this:
    Yeah I realise the way I set it out wasn't optimal, but the code I have written shouldn't be wrong?

    Still doesn't work.

    And yes, I have a few combo boxes and a few text boxes where the data needs to be transferred into the table tblTerrainRepairJobs. The data needs to be on a new line as there are going to be multiple repair jobs, hence it having the gotorecord command.

    I am just trying to get the first couple of records inserted, to make sure the function works (customer and site), I have 10 other fields that eventually need to be added to the code.

  4. #4
    sleptema is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2013
    Posts
    6
    I feel like the worlds biggest idiot.

    I just closed the table and reopened it with all the results.

    I guess I just wasn't refreshing it.

    Cheers for all your help anyway haha.

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Yeah I realise the way I set it out wasn't optimal, but the code I have written shouldn't be wrong?
    Well, not wrong, but a lot of unnecessary code. And a couple of minor errors.

    ---------------------
    Code:
    DoCmd.OpenTable "tblTerrainRepairJobs", acViewNormal, acAdd
    You don't have to open the table (and shouldn't) to do the insert (append).

    ---------------------
    Code:
    Dim CustomerString, SiteString As String
    Here you declared CustomerString as a Variant and SiteString as a string

    ---------------------
    Code:
    DoCmd.GoToRecord acDataTable, "tblTerrainRepairJobs", acNewRec
    This line is unnecessary as the SQL insert command creates a new record. You will probably end up with empty records using the above line.

    ---------------------
    Code:
    DoCmd.RunSQL StrSQL
    "DoCmd.RunSQL" is executed through Access and pops up warning messages unless you turn the messages off. I don't like turning messages off.
    "Currentdb.Execute" runs through Jet, the database engine. It won't popup error messages unless you use the argument "dbfailonerror".

    ---------------------
    Code:
    'DoCmd.Close acTable, "tblTerrainRepairJobs", acSaveYes
    I have never used this command; I believe it is for saving changes to the table structure, not saving changes to the data.


    Good luck with your project....

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

Similar Threads

  1. INSERT query: insert new data only
    By drh in forum Access
    Replies: 2
    Last Post: 04-04-2014, 05:31 PM
  2. Insert into where
    By Richie27 in forum Queries
    Replies: 31
    Last Post: 05-18-2012, 02:11 AM
  3. Insert into
    By glasgowlad1999 in forum Access
    Replies: 2
    Last Post: 10-14-2011, 02:38 PM
  4. New insert row
    By khparhami in forum Access
    Replies: 5
    Last Post: 09-06-2010, 10:37 AM
  5. SQL Insert into
    By jamin14 in forum Programming
    Replies: 15
    Last Post: 04-01-2010, 12:35 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