Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    swsailor is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Jan 2019
    Location
    Springboro Oh
    Posts
    9

    Insert a value into a query

    After running a match query I foune no records returned by using the DCount function. Since am going to export the data to an excel worksheet I would like to place a message in a field to the effect that no records available. The code that I am working with, without success is:



    INSERT INTO QmatQselBenefitsToTblPermBenefits ([LastName]) VALUES (No additions to report this month)

    SQL language is not my forte.

    Thanks for any suggestions

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Dont use SQL, use a query.
    Queries dont need the Dcount, the query does the counting.

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Regardless of how you determine the record count, need to place literal text between text delimiters. If you are running this SQL action within VBA:

    CurrentDb.Execute "INSERT INTO QmatQselBenefitsToTblPermBenefits ([LastName]) VALUES ('No additions to report this month')"

    But don't you need a date value inserted as well?
    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.

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    I don't think you need or want to actually add a record to the underlying table. For exporting purposes you can simply use a union query to show your "No records added this month".

    Select QmatQselBenefitsToTblPermBenefits.[LastName]. From QmatQselBenefitsToTblPermBenefits
    Union
    Select "_No additions to report this month" As [LastName] From QmatQselBenefitsToTblPermBenefits;

    This would create that entry and place it at the top of your last names if sorted ascending.

    Cheers,
    Vlad

  5. #5
    swsailor is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Jan 2019
    Location
    Springboro Oh
    Posts
    9
    I have copied the code from GIGU and June7
    separately,
    and had no success.
    Is it possible to place a column in the match query like the following:
    Constant:IIf IsNull(LastName), LastName = "Nothing to report this month"
    where LastName is a field in the empty query?

  6. #6
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Can you provide a sample db with your queries - no need for much data, just a couple of records to show us what you want.

    Cheers.
    Vlad

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Yes, that could be a calculated field in query. Provide query SQL or db as suggested by Gicu. 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.

  8. #8
    swsailor is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Jan 2019
    Location
    Springboro Oh
    Posts
    9
    The query returns nothing including the message. The code I am attempting is

    Dim varCheckCount As Integer
    varCheckCount = DCount("[LastName]", "QmatQselBenefitsToTblPermBenefits")
    If varCount = 0 Then
    MsgBox "No additions to report this month"
    SELECT QmatQselBenefitsToTblPermBenefits.[LastName] FROM QmatQselBenefitsToTblPermBenefits
    Union
    SELECT "No additions to report this month" As [LastName] FROM QmatQselBenefitsToTblPermBenefits;
    Else
    MsgBox "These volunteers will be added to the Abenity eligiblity list"
    End If

    It is likely to be a syntax error or punctuation.

  9. #9
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    You should be checking varCheckCount not If varCount = 0

    You should have Option Explicit at the top of your module Always!!!

  10. #10
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Sorry but your code doesn't make much sense. Where do you use the select statement? If you want to tell users on a report that there are no new volunteers I would use the NoData event to show the message ("No additions to report this month") then set Cancel=True to cancel the opening of the report. In my example I was just trying to show you the syntax for the Union; if the first query has no records the resultant union with itself will also have no records. You need to reference the original table containing the volunteers names:

    SELECT QmatQselBenefitsToTblPermBenefits.[LastName] FROM QmatQselBenefitsToTblPermBenefits
    Union
    SELECT "No additions to report this month" As [LastName] FROM tblYOUR_VolunterTable;

    Cheers,
    Vlad

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Can't just plop an SQL statement into the middle of VBA code like that. That code won't even compile. Was that just a copy/paste error in the post and code is not really like that?

    And as Orange pointed out, you are not correctly referencing declared variable.
    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
    swsailor is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Jan 2019
    Location
    Springboro Oh
    Posts
    9
    After a day and a half I still have not work this out. My latest code effort is:


    Dim db As DAO.Database
    Set db = CurrentDb
    Dim strSQL As String
    strSQL = "INSERT INTO QmatQselBenefitsToTblPermBenefits (LastName)" & "VALUES ('No Additions');"
    db.Execute strSQL

    No errors or messages of any kind but it does not INSERT.

    Is it possible that you can't INSERT INTO any fields in a blank query?
    I have even tried to create an update query and that has failed.

    Thanks for your patience

  13. #13
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    You can't insert into a query. Can you explain what is that you want to do?

  14. #14
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    You can INSERT into a query but it must be an editable query. And what is really happening is a new record is created in table, just passing through query.
    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
    swsailor is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Jan 2019
    Location
    Springboro Oh
    Posts
    9
    I am matching a query to a history table, which wirks beautifully. The records that don t match will be exported to an excel spreadsheet. On the occasion that the query returns no records I have to export the query anyway. What I am trying to do is put a message in the blank query stating that no records were found. (No new additions for this month). The spreadsheet then is emailed to the corporate office.

    I tried to use an update sql and that didnt work either.

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

Similar Threads

  1. Insert into query vba
    By jaryszek in forum Access
    Replies: 4
    Last Post: 07-28-2017, 05:00 AM
  2. Replies: 1
    Last Post: 08-29-2016, 07:50 PM
  3. Replies: 3
    Last Post: 05-27-2015, 01:04 PM
  4. INSERT query: insert new data only
    By drh in forum Access
    Replies: 2
    Last Post: 04-04-2014, 05:31 PM
  5. INSERT INTO query in VBA
    By jgelpi16 in forum Programming
    Replies: 5
    Last Post: 09-17-2010, 02:19 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