Page 2 of 2 FirstFirst 12
Results 16 to 25 of 25
  1. #16
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Can't save that value into date type field. The Format function results in a string, not a date. Date is actually stored in a date type field as a number. I don't even see why you are using Format function.

    CurrentDb.Execute "INSERT INTO companytable (ticker, company, sector, subindustry, lastupdate, flag) " & _


    " VALUES('" & Me.cboTicker & "', '" & Me.txtCompany & "', '" & Me.txtSector & "', '" & Me.txtSubIndustry & "', #" & _
    Me.dtLastupdate & "#, " & me.cbxFlag & ")"
    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.

  2. #17
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I'll get out of the way, but the Format() function is very commonly used to ensure a valid date is passed:

    http://allenbrowne.com/ser-36.html#Format

    http://access.mvps.org/access/datetime/date0005.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #18
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Just a little hint when using VBA code to create SQL code to run.

    The method that I often use is the following:
    1. Create a query using Query Builder or SQL that is an example of what I want to do. Get it working exactly right (without errors).
    2. Once it is working correctly, switch to SQL view and copy and paste the SQL code out to a text file or Word Document.
    3. Write your VBA code to create the SQL code you want. Before executing it, write it out to a Message Box.
    4. Compare the SQL code in your Message Box to the SQL code in your text file/Word Document and look for differences and make the necessary adjustments.
    5. Once you get them to match, it should work!

  4. #19
    graccess is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Posts
    78
    Quote Originally Posted by JoeM View Post
    Just a little hint when using VBA code to create SQL code to run.

    The method that I often use is the following:
    1. Create a query using Query Builder or SQL that is an example of what I want to do. Get it working exactly right (without errors).
    2. Once it is working correctly, switch to SQL view and copy and paste the SQL code out to a text file or Word Document.
    3. Write your VBA code to create the SQL code you want. Before executing it, write it out to a Message Box.
    4. Compare the SQL code in your Message Box to the SQL code in your text file/Word Document and look for differences and make the necessary adjustments.
    5. Once you get them to match, it should work!

    Thanks Joe. Good advice. I've tried adding code to produce the exception caught in a message box but I'm apparently not inputting that correctly. How would you go about doing this?
    thx

  5. #20
    graccess is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Posts
    78
    Quote Originally Posted by pbaldy View Post
    I'll get out of the way, but the Format() function is very commonly used to ensure a valid date is passed:

    http://allenbrowne.com/ser-36.html#Format

    http://access.mvps.org/access/datetime/date0005.htm

    Thanks again. I think I'm running into trouble where I'm already assigning the field name dtLastupdate with a date (the show date picker in the format tab properties is set to "For Dates".) Not sure if I'm creating more problems by also typing in the code for this when trying to insert these values?

  6. #21
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Thanks Joe. Good advice. I've tried adding code to produce the exception caught in a message box but I'm apparently not inputting that correctly. How would you go about doing this?
    Instead of doing it like this:
    Code:
    CurrentDb.Execute "INSERT INTO companytable (ticker, company, sector, subindustry, lastupdate, flag) " & _
    " VALUES('" & Me.cboTicker & "', '" & Me.txtCompany & "', '" & Me.txtSector & "', '" & Me.txtSubIndustry & "', #" & _
    Me.dtLastupdate & "#, " & me.cbxFlag & ")"
    try it like this:
    Code:
    Dim mySQL as String
    
    ' Build SQL Code to run and store as string
    mySQL = "INSERT INTO companytable (ticker, company, sector, subindustry, lastupdate, flag) " & _
    " VALUES('" & Me.cboTicker & "', '" & Me.txtCompany & "', '" & Me.txtSector & "', '" & Me.txtSubIndustry & "', #" & _
    Me.dtLastupdate & "#, " & me.cbxFlag & ")"
    
    ' View SQL code before running to make sure it matches your sample SQL code gained from running query manually
    MsgBox mySQL
    
    ' Execute SQL code (only after sure everything is correct)
    CurrentDb.Execute mySQL
    I sometimes use:
    Code:
    DoCmd.RunSQL mySQL
    instead of:
    Code:
    CurrentDb.Execute mySQL

  7. #22
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Agree Paul, but I think the structure must be MM/DD/YYYY and delimited in the query with #. That's what Allen's function code does. The backslash forces the following character to be accepted as the literal character so it will pass within the variable and not have to be separately included in the query statement concatenation.

    However, this code won't help if the users are inputting into textbox in a structure the system is not defaulting to. As a US user, if I enter 7/4/2014 intending 7 Apr 2014, Access actually sees Jul 4 2014 and records that date. Now my data is bad. If I input 31/12/2014, Access will do me a 'favor' and flip it to 12/31/2014.
    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.

  8. #23
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by June7 View Post
    I think the structure must be MM/DD/YYYY and delimited in the query with #.
    The ISO format in the OP is perfectly valid (just tested), and preferred by some developers as being unambiguous. The # were included as required in the OP, the problem was with the additional text delimiters around the value, as evidenced in the original error. As yet we haven't seen the finished SQL.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #24
    graccess is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Posts
    78

    Cool

    Quote Originally Posted by JoeM View Post
    Instead of doing it like this:
    Code:
    CurrentDb.Execute "INSERT INTO companytable (ticker, company, sector, subindustry, lastupdate, flag) " & _
    " VALUES('" & Me.cboTicker & "', '" & Me.txtCompany & "', '" & Me.txtSector & "', '" & Me.txtSubIndustry & "', #" & _
    Me.dtLastupdate & "#, " & me.cbxFlag & ")"
    try it like this:
    Code:
    Dim mySQL as String
    
    ' Build SQL Code to run and store as string
    mySQL = "INSERT INTO companytable (ticker, company, sector, subindustry, lastupdate, flag) " & _
    " VALUES('" & Me.cboTicker & "', '" & Me.txtCompany & "', '" & Me.txtSector & "', '" & Me.txtSubIndustry & "', #" & _
    Me.dtLastupdate & "#, " & me.cbxFlag & ")"
    
    ' View SQL code before running to make sure it matches your sample SQL code gained from running query manually
    MsgBox mySQL
    
    ' Execute SQL code (only after sure everything is correct)
    CurrentDb.Execute mySQL
    I sometimes use:
    Code:
    DoCmd.RunSQL mySQL
    instead of:
    Code:
    CurrentDb.Execute mySQL

    Joe...thank you so much. Not only for the msgbox code/tip, but for correctly displaying my code, I was indeed missing my apostrophe mistakes in addition to the date syntax. Thanks again.

  10. #25
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I was indeed missing my apostrophe mistakes in addition to the date syntax. Thanks again.
    Your welcome. Glad you got it all sorted out!

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

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2013, 11:17 AM
  2. RunTime error 3075
    By Compufreak in forum Access
    Replies: 3
    Last Post: 07-25-2012, 02:18 AM
  3. Replies: 6
    Last Post: 05-30-2012, 12:32 PM
  4. Replies: 5
    Last Post: 03-27-2012, 01:40 PM
  5. Getting Error 3075
    By boywonder in forum Programming
    Replies: 4
    Last Post: 05-23-2011, 05:06 PM

Tags for this Thread

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