Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228

    VBA message box

    I’m using an append query on my form which only works when the same "name" exists in two tables.


    It’s for a linked table to the outlook calendar and an appointments table used to manage these appointments.

    The query looks for a match in the name, and runs a macro for whatever matches.
    If something has been changed on the calendar then these names won’t match so I want some VBA to check if the name does exist and then pop up a message box when it doesn’t.

    In the database the tabes and fields are:

    Calendar - subject
    tblappointment - appt

    a screenshot of the first query in the macro:

    Click image for larger version. 

Name:	AbortQ.png 
Views:	24 
Size:	47.4 KB 
ID:	24171

    I'm not sure how to reference query results in VBA so if someone could help me that would be great.

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    I do not use macros, and I do not use/have Outlook on my PC.

    To reference query results in vba use a recordset.

    In overview:

    Dim a recordset, and a string to represent the query name or SQL
    Open the recordset
    Set up a loop to process all records returned by the query

    sample code----------------------------------

    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : sampleRSWithQuery
    ' Author    : mellon
    ' Date      : 29/03/2016
    ' Purpose   : sample vba to show processing a select result as a recordset
    ' and print some output to the immediate window.
    '---------------------------------------------------------------------------------------
    '
    Sub sampleRSWithQuery()
    
        Dim rs As DAO.Recordset   'recordset
        Dim db As DAO.Database   'database
        Dim sSQL As String            'query sql
        Dim iCnt As Integer               'display a number to identify a line of print
    
        'this is the sql of query52
    10  On Error GoTo sampleRSWithQuery_Error
    
    20  sSQL = "SELECT Animal.AName, AnimalLocs.SightingDate, AnimalLocs.GPSLat, AnimalLocs.GPSLong " _
               & " FROM Animal INNER JOIN AnimalLocs ON Animal.AnimalId = AnimalLocs.AnimalId;"
    
    30  Set db = CurrentDb  '
    40  Set rs = db.OpenRecordset(sSQL)          'establish the recordset based on the query SQL
    50  Do While Not rs.EOF                      'set up a loop to process all records in the recordset
    60      iCnt = iCnt + 1                      'increment the line counter
    70      Debug.Print iCnt & "  " & rs!AName & "   " & rs!SightingDate & "  " & rs!GPSLat   'do something ith the record
                                                                           ' --print a few fields to the immediate window
    80      rs.MoveNext                               'move to the next record in the recordset
    90  Loop
    100 rs.Close                                  'close the recordset
    110 Set rs = Nothing                          ' release the recordset
    
    120 On Error GoTo 0
    130 Exit Sub
    
    sampleRSWithQuery_Error:
    
    140 MsgBox "Error " & Err.Number & " in line " & Erl & " (" & Err.Description & ") in procedure sampleRSWithQuery"
    End Sub
    using these tables and query
    table Animal
    AnimalId AName
    1 Spot
    2 Jim
    3 Sam
    4 David
    5 BlueEyes
    6 Capitan
    7 Johnny

    table

    animalLocs
    SightingDate GPSLong GPSLat CaptureDateX AnimalId
    27/03/2009 77.9 47.3 21/02/2008 1
    28/03/2009 77.34 46.8 21/02/2008 1
    02/12/2009 78.44 45.32 21/02/2008 1
    13/03/2009 76.33 48.9 27/02/2008 2
    29/03/2009 77.45 48.8 27/02/2008 2
    21/04/2010 78.53 47.54 27/02/2008 2
    24/08/2009 76.2 49.4 03/12/2008 3
    21/04/2010 78.23 47.52 29/05/2008 4
    23/04/2010 78.666 47.66 29/05/2008 4
    14/09/2010 77.7 49.3 04/02/2008 5
    17/09/2010 77.334456 48.9 04/02/2008 5


    query:

    sql
    Code:
    SELECT Animal.AName, AnimalLocs.SightingDate, AnimalLocs.GPSLat, AnimalLocs.GPSLong
    FROM Animal INNER JOIN AnimalLocs ON Animal.AnimalId = AnimalLocs.AnimalId;
    Data:
    AName SightingDate GPSLat GPSLong
    Spot 27/03/2009 47.3 77.9
    Spot 28/03/2009 46.8 77.34
    Spot 02/12/2009 45.32 78.44
    Jim 13/03/2009 48.9 76.33
    Jim 29/03/2009 48.8 77.45
    Jim 21/04/2010 47.54 78.53
    Sam 24/08/2009 49.4 76.2
    David 21/04/2010 47.52 78.23
    David 23/04/2010 47.66 78.666
    BlueEyes 14/09/2010 49.3 77.7
    BlueEyes 17/09/2010 48.9 77.334456

  3. #3
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    Thanks for that, It will take me some time to get working but it looks like I can use that.

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

Similar Threads

  1. Replies: 7
    Last Post: 03-17-2016, 05:53 PM
  2. Replies: 2
    Last Post: 01-23-2014, 12:40 PM
  3. Replies: 15
    Last Post: 11-01-2013, 03:24 PM
  4. Replies: 9
    Last Post: 09-26-2012, 12:20 PM
  5. Message box
    By tomneedshelp in forum Forms
    Replies: 1
    Last Post: 03-22-2012, 10:27 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