Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Rico is offline Novice
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    Perth Australia
    Posts
    29

    I need to change CRITERIA in a Query when the program is running.

    My DoctorAndPodiatristTB contains 3 fields, ResidentsName, AppointmentTime and DoctorOrPodiatrist. My DoctorAndPodiatristQ contains the same 3 fields.
    The Doctors records will have a "D" in the
    DoctorOrPodiatrist field and the Podiatrist records will have a "P" in the DoctorOrPodiatrist field.



    When I send my email: DoCmd.SendObject acSendQuery, "
    DoctorAndPodiatristQ", acFormatRTF, "rwshea220540@outlook.com", , , "Test", "Sending an email from VBA"

    If I am sending the email to the doctor, I need code to change the DoctorAndPodiatristQ to select only the records with "D" in the DoctorOrPodiatrist field before I send it.
    If I am sending the email to the Podiatrist, I need code to change the DoctorAndPodiatristQ to select only the records with "P" in the DoctorOrPodiatrist field before I send it.

    Any help would be greatly appreciated. Rico

  2. #2
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    Use a parameter query where you enter the DoctorOrPodiatrist field value at runtime

    See
    Use parameters to ask for input when running a query (microsoft.com)
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,936
    You could also use a tempvar as criteria and set it to the required value before sending?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    Rico is offline Novice
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    Perth Australia
    Posts
    29
    Thanks isladogs, that's really interesting and I will store that info away for future use, but I am already in the part of my program where I know whether it is "D" or "P", so I don't need to ask the user.

    Thanks Welshgasman, a tempvar seems the answer to my problems. could either of you give me some simple code I can use?

  5. #5
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    In that case use an If...Else..End If block

    Code:
    If Me.DoctorOrPodiarist ="D" Then
       'run query filtered to doctors only
    Else
        'run query filtered to podiatrists only
    End If
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,936
    Mine would just be
    Code:
    Tempvars("ReportType")= Me.DoctorOrPodiarist
    Run your report
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    Rico is offline Novice
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    Perth Australia
    Posts
    29
    Isladogs and Welshgasman: Not really what I wanted, I was hoping for some code something like:
    Dim qdf As DAO.QueryDef
    Set qdf = CurrentDb.QueryDefs("DoctorAndPodiatristQ")
    and then somehow code to get in to make the DoctorOrPodiatrist parameter D or P
    I don't want to make multiple query's because I will also want to change AppointmentTime as well.

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,936
    You do not create multiple queries with my method?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    Rico is offline Novice
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    Perth Australia
    Posts
    29
    Welshgasman: I can't read your last code. I can see - (Mine would just be - Code and then there is something in the box but it is unreadable.
    I'm new to this forums so I could be not viewing it properly. I have been doing some work on it, am I on the right track?
    ''My DocAndPodTB contains 3 rows, ResidentsName, AppointmentTime and DorP. My DocAndPodEmailQ contains the same 3 rows.
    Dim mydb As DAO.Database
    Set mydb = CurrentDb()
    Dim qdf As DAO.QueryDef
    Set qdf = CurrentDb.QueryDefs("DocAndPodEmailQ")
    With qdf
    .Parameters("DorP") = "D"
    .Execute
    End With
    qdf.Close
    Set qdf = Nothing
    DoCmd.SendObject acSendQuery, "DocAndPodEmailQ", , "rwshea220540@outlook.com", , , "Test", "Sending Doctors email (with table) from VBA"

  10. #10
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    Nor would my approach need multiple queries.
    In fact your code appears to use a parameter as I originally suggested
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  11. #11
    Rico is offline Novice
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    Perth Australia
    Posts
    29
    Well you blokes in the UK are up early, with the high temperatures we have been hearing about, you obviously don't have airconditioning in your bedrooms.
    The code I am working with gives me - Item not found in collection, on the line .parameters("DorP") = "D"

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,936
    Have you declared the parameter in the query?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  13. #13
    Rico is offline Novice
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    Perth Australia
    Posts
    29
    Welshgasman: sorry, but you are talking to a novice here, what do you mean by - Have you declared the parameter in the query?

  14. #14
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,936
    I am not too hot on Parameters.
    I did get a query working with just using the one parameter and using the index number.

    However there is a Paremeters button that allows you to specify not only the parameter, but it's type.

    Then the sql looks like this

    Code:
    PARAMETERS CMS Long, CMSDATE DateTime;
    SELECT Transactions.*, Transactions.Description, Transactions.TransactionDate
    FROM Transactions
    WHERE (((Transactions.Description) Like "*" & [CMS] & "*") AND ((Transactions.TransactionDate)=[CMSDATE]));
    So without that statement, I am not sure Access would know it had parameters?

    See if this helps at all https://docs.microsoft.com/en-us/off...collection-dao

    As I mentioned before, I have just used TempVars as criteria. Much easier it appears now.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  15. #15
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    @Rico,
    Maybe you would post a copy of your dB with around 3 Doctors and 3 Podiatrist in the table for analysis? Change the names to protect the guilty...

    It sounds like you have 1 table and 2 queries? ("DocAndPodEmailQ" and "DoctorAndPodiatristQ")
    Not sure how you are running the command "DoCmd.SendObject".

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

Similar Threads

  1. Replies: 2
    Last Post: 06-22-2019, 02:24 AM
  2. Running SAS program in a Macro
    By PhatRam32 in forum Macros
    Replies: 2
    Last Post: 03-21-2017, 07:31 AM
  3. Replies: 2
    Last Post: 05-31-2016, 01:52 PM
  4. Replies: 1
    Last Post: 03-25-2014, 08:42 AM
  5. Running Query on only 1 row of table.. Criteria?
    By JMac in forum Database Design
    Replies: 5
    Last Post: 04-16-2012, 11:27 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