Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    doobybug is offline Advanced Beginner
    Windows 98/ME Access 2003
    Join Date
    Jan 2009
    Posts
    39

    Insert SQL not functioning

    Hi,



    I am trying to have a button "Save" so that when clicked it will save the details from the form. It doesn't seem there are any syntax error but somehow the data is not being saved in the table. Any ideas why...I am using INSERT SQL

    Code:
    Private Sub btnSave_Click()    Dim refNo As String
        Dim volume As Integer
        Dim dateStart As Date
        Dim dateEnd As Date
        Dim shelfId As Integer
        Dim sql As String
    
    
         
        If (IsNull(Me.cmbNotary.Column(0)) Or IsNull(Me.txtVolume) Or IsNull(Me.txtDateStart) Or IsNull(Me.txtDateEnd) Or IsNull(Me.cmbShelf.Column(0))) Then
           MsgBox "Fields cannot be left blank"
            
        Else
                refNo = cmbNotary.Column(0)
                volume = Me.txtVolume
                dateStart = Me.txtDateStart
                dateEnd = Me.txtDateEnd
                shelfId = Me.cmbShelf.Column(0)
                
                sql = "INSERT INTO tblNotaryIndex(NotaryRefNo, Volume, [Date Start], [Date End], ShelfID) " & _
                "VALUES(" & refNo & "," & volume & ",'" & Format(dateStart, "dd-mmm-yyyy") & "','" & Format(dateEnd, "dd-mmm-yyyy") & "', " & shelfId & ")"
                Debug.Print sql
                On Error GoTo ErrorMessage
                CurrentDb.Execute sql
        
                
                cmbNotary.Value = Null
                Me.txtVolume = Null
                Me.txtDateStart = Null
                Me.txtDateEnd = Null
                Me.cmbShelf.Value = Null
                 
        End If
         
        
        
    Exit Sub
    
    
    ErrorMessage:
        MsgBox "Record already exists"
    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Are [Date Start] and [Date End] fields date/time type? Format function results in a string value, not the date you would expect. Also, date/time parameters for saving to a date/time field require # delimiter. Try:

    "VALUES(" & refNo & "," & volume & ", #" & dateStart & #, #" & dateEnd & "#, " & shelfId & ")"


    Best to apply formatting on forms and reports.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Did you copy the SQL and paste it into a query design window and run it successfully?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Straight copy/paste would not work because of reference to form controls. Would have to do some edits.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    doobybug is offline Advanced Beginner
    Windows 98/ME Access 2003
    Join Date
    Jan 2009
    Posts
    39
    Date start and date end are of type Date...still not working and on the immediate window it is showing:

    INSERT INTO tblNotaryIndex(NotaryRefNo, Volume, [Date Start], [Date End], ShelfID) VALUES(001,80, #01/09/1998#, #01/10/1999#, 1)

  6. #6
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I always approach these by trying to first create SQL code that will do what I want (either through the Query Builder or by writing the SQL code directly).
    Then, once I have that working, then try to write the same code via VBA (and use MsgBox to compare it to the working example code).

  7. #7
    doobybug is offline Advanced Beginner
    Windows 98/ME Access 2003
    Join Date
    Jan 2009
    Posts
    39
    I ran the insert statement in query design and is giving me the following error:

    Microsoft Access can't append all the records in the append query.
    Microsoft access set 1 field to Null due to a type conversion failure, and it didn't add 0 records to the table due to key violations, 0 due to lock violations and 0 records due to validation rule violations

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    There is no need to reference the combobox column index in the If conditional - you want the value of the combobox.

    If that doesn't fix, I would have to examine db to debug. If you want to provide, follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    doobybug is offline Advanced Beginner
    Windows 98/ME Access 2003
    Join Date
    Jan 2009
    Posts
    39
    I attached it below...thanks so much!

    Database1 - Copy.zip

  10. #10
    jwhite is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Dec 2012
    Location
    North Carolina
    Posts
    349
    sql = "INSERT INTO tblNotaryIndex(NotaryRefNo, Volume, [Date Start], [Date End], ShelfID) " & _
    "VALUES(" & refNo & "," & volume & ",#" & Format(dateStart, "dd-mmm-yyyy") & "#,#" & Format(dateEnd, "dd-mmm-yyyy") & "#, " & shelfId & ")"

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Why are you using an UNBOUND form?

    Open BOUND form, move to new record row, enter data - no VBA needed.

    NotaryRefNo is a text type field. Therefore need apostrophe delimiters. Why don't you make these PK/FK fields numeric? What happens when you reach 999? Placeholder zeros prefix can be handled with formatting in forms and reports.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    doobybug is offline Advanced Beginner
    Windows 98/ME Access 2003
    Join Date
    Jan 2009
    Posts
    39
    Supposedly the ref no should be a letter and 3 numbers ex: R450. In the future I am going to put an input mask on that field..that is the reason I made it short text...

    How do you create a bound form? Can I fix the current one?

  13. #13
    doobybug is offline Advanced Beginner
    Windows 98/ME Access 2003
    Join Date
    Jan 2009
    Posts
    39
    I think I managed to do a bound form...If it is a bound form how can I add Validation rules such as if a field is left blank you cannot insert a new record?

  14. #14
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    You can use the ValidationRule and ValidationText properties of textbox and combobox.

    Or set fields as required in table design and let Access nag the users.

    Otherwise, code in form BeforeUpdate event. Common topic, search forum or Google.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  15. #15
    doobybug is offline Advanced Beginner
    Windows 98/ME Access 2003
    Join Date
    Jan 2009
    Posts
    39
    I will try that...thanks a billion

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Control Wizard not functioning in reports
    By Harley Guy in forum Reports
    Replies: 2
    Last Post: 02-26-2013, 11:28 AM
  2. Combo Boxes Not Functioning
    By cdixon2098 in forum Forms
    Replies: 6
    Last Post: 08-27-2012, 02:51 PM
  3. After Update/concatenation not functioning
    By tariq1 in forum Programming
    Replies: 4
    Last Post: 08-19-2012, 11:54 AM
  4. After Insert macro not functioning
    By scottnew2ac in forum Access
    Replies: 2
    Last Post: 07-26-2012, 09:25 AM
  5. DLookup not functioning correctly - any ideas please?
    By shabbaranks in forum Programming
    Replies: 2
    Last Post: 02-02-2012, 09:09 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