Results 1 to 5 of 5
  1. #1
    billybeer is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jun 2015
    Posts
    27

    Date input error


    I have a form that has multiple text boxes, multiple comboboxes, and multiple date fields. I have a command button to runs an INSERT INTO query into a table for inputting a new record. If I fill in all the date fields the form works and the new record is created. If I don't fill in all the date fields then I get this error message. Run-time error'3075': Syntax error in date in query expression '#'. This is the SQL for one of the date fields. #" & Date_Warrant_Reviewed & "# The other date field syntax is the same with different field names. Again the problem is only with I leave some of the date fields blank in the form. Any help would be great.

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Options include testing for empty fields and only running the SQL when all are complete, building the SQL to only include fields that got filled out, and switching to using the AddNew method of a recordset, which won't barf when a textbox is empty (as long as the field isn't required).
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    billybeer is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jun 2015
    Posts
    27
    Quote Originally Posted by pbaldy View Post
    Options include testing for empty fields and only running the SQL when all are complete, building the SQL to only include fields that got filled out, and switching to using the AddNew method of a recordset, which won't barf when a textbox is empty (as long as the field isn't required).
    so I want to go with building the SQL to only include fields that got filled out. Here is the current SQL/VBA..
    Here is current SQL statement and VBA. How different would the statement be to include only fields that got filled out. (i.e.can you steer me in the right directions)

    "INSERT INTO tblPATUCASES (CTN, City_Where_Crime_Occurred, DLast, DFirst, Warrant_APA, Date_Warrant_Reviewed, Warrant_Disposition, Next_Action, Next_Date, Exam_APA, Exam_Judge, Exam_Disposition, APA, Trial_Judge, Disposition, Trial_Date, chboxRepeat, Sentence, Restitution_Ordered, Notes)" & _
    "Values (CTN.Value, City_Where_Crime_Occurred.Value, DLast.Value, DFirst.Value, Warrant_APA.Value, #" & Date_Warrant_Reviewed & "#, Warrant_Disposition.Value, Next_Action.Value, #" & Next_Date & "#, Exam_APA.Value, Exam_Judge.Value, Exam_Disposition.Value, APA.Value, Trial_Judge.Value, Disposition.Value, #" & Trial_Date & "#, chboxRepeat.Value, Sentence.Value, Restitution_Ordered.Value, Notes.Value);"
    DoCmd.SetWarnings False
    DoCmd.RunSQL strSQL
    DoCmd.SetWarnings True
    Me.Refresh

    Any help in starting me in the right direction would be appreciated.

    Mark

  4. #4
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    I'd use two variables, then in pseudo-code:

    Code:
    If TextboxFilledOut Then
      strInsert = strInsert & ", Date_Warrant_Reviewed"
      strValues = strValues &  , #" & Date_Warrant_Reviewed & "#
    End If
    Then put everything together at the end.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Something like this:
    Code:
        Dim sSQL As String
        Dim sSQLVALUES As String
    
        sSQL = "INSERT INTO tblPATUCASES ("
        sSQLVALUES = "VALUES ("
    
        If Len(Trim(Me.CTN & "")) = 0 Then
            sSQLVALUES = sSQLVALUES & Me.CTN & ", "
        End If
    
        If Len(Trim(Me.City_Where_Crime_Occurred & "")) = 0 Then
            sSQL = sSQL & " City_Where_Crime_Occurred,"
            sSQLVALUES = sSQLVALUES & Me.City_Where_Crime_Occurred & ", "
        End If
    
    '    .
    '    .
    '    you have 20 controls, so
    '    .there will be a total of 20 tests for NULL controls
    '    .
    '    .
    
    
        If Len(Trim(Me.Notes & "")) = 0 Then
            sSQL = sSQL & " Notes,"
            sSQLVALUES = sSQLVALUES & "'" & Me.Notes & "', "
        End If
    
    
        'Then remove the last comma from both sSQL & sSQLVALUES
        'and add a closing parenthisis for both sSQL & sSQLVALUES
        'Then
        
        sSQL = sSQL & " " & sSQLVALUES
        'check to see if insert statement formed correctly
        Debug.Print sSQL
    
        CurrentDb.Execute sSQL, dbFailOnError
    I don't know what the field types are, so you will need to add the proper delimiters.



    Oops, I see that Paul posted first............

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

Similar Threads

  1. Replies: 4
    Last Post: 04-21-2015, 07:12 PM
  2. Auto Date input
    By skyi in forum Access
    Replies: 6
    Last Post: 09-19-2012, 01:58 AM
  3. Input error on form
    By bibbyd01 in forum Forms
    Replies: 1
    Last Post: 04-23-2012, 09:52 AM
  4. capturing the input date
    By rmohebian in forum Access
    Replies: 8
    Last Post: 02-10-2011, 02:54 PM
  5. date entry on input form
    By dzawicki in forum Forms
    Replies: 3
    Last Post: 12-25-2009, 08:13 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