Results 1 to 9 of 9
  1. #1
    cesarone82 is offline Novice
    Windows XP Access 2003
    Join Date
    Jun 2009
    Posts
    25

    Access a query from VBA procedure in form button


    Hi,

    I have a button in an access form (frm_projects) with a VBA procedure, inside this procedure i need to select specific data from a join query (qry_projprof where a certain field in the query in equal to the projectnumber in the opened form which is Me!pjnumber).
    The question is: do i need to call the openrecordset inside access or there are quicker ways? (I know in word to access the database I should use the open recordset function)
    Once I am able to access the query in the VBA procedure How can I refer to the single query rows/column?

    The button is basically filling forms in a PDF file from an acccess database

    Thanks file in attached link in case
    Attached Files Attached Files

  2. #2
    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 can't look at the attachments right now, but you can use DLookup() or a recordset to get values from the query. If you're after a single value, I'd probably use the DLookup(), for multiple the recordset. Either way you'd use a criteria to find the specific record you want.

    http://www.theaccessweb.com/general/gen0018.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Some issues there. Your project contains a reference to a library that I don't have so things don't run so well. Will try to answer your question instead. You can get at record field values with DLookup (although should not do so inside of a query) or create a recordset or use the form recordset clone.

    Your query contains [Me]![codproj] as criteria. Queries cannot use Me as a reference - that is only for forms or reports to refer to themselves. Use the complete Forms syntax. I would have suggested something but can't tell which form to replace Me with. None of your forms seem to use the query as a recordsource, so that's a bit of a mystery at present.

    EDIT - None of your forms seem to have codproj in them either, so it's a mystery why you're trying to use that field reference as query criteria.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    cesarone82 is offline Novice
    Windows 11 Access 2016
    Join Date
    Jun 2009
    Posts
    25
    Quote Originally Posted by Micron View Post
    Some issues there. Your project contains a reference to a library that I don't have so things don't run so well. Will try to answer your question instead. You can get at record field values with DLookup (although should not do so inside of a query) or create a recordset or use the form recordset clone.

    Your query contains [Me]![codproj] as criteria. Queries cannot use Me as a reference - that is only for forms or reports to refer to themselves. Use the complete Forms syntax. I would have suggested something but can't tell which form to replace Me with. None of your forms seem to use the query as a recordsource, so that's a bit of a mystery at present.

    EDIT - None of your forms seem to have codproj in them either, so it's a mystery why you're trying to use that field reference as query criteria.

    Thanks pbaldi and micron.

    Yes sorry I forgot to mention but i removed the reference in the file attached, basically it was just lines of code writing database data into a pdf form.

    So two questions, I understood i need to use dlookup:

    1) In the file attached I also put in the form a subform that has the data i need to extract, basically I need to extract certain values of certain records of this subform (which is the last one of the frm_projects
    2) in case i don't have this subform on the form itself, how can i reference the query directly from the click procedure passing the pjnumber as parameter? for example, i put a second button in the form, which i d like to open the query associating pjnumber of the form to pjnumber in the query.
    3) Dlookup can allow me to extract data in sequence withous specifying a parameter? i mean first record, then second record, etc.

    Thanks in advance.
    Attached Files Attached Files

  5. #5
    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'm still not in a position to look at the attachment, but the link I posted shows how to refer to a form to get the criteria. In answer to your last question, no. Without a criteria DLookup() will always return the first record it finds.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    I would use a recordset as you have mentioned.
    Then you will have all the data for a particular project number if there are more than one record for a project.
    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
    cesarone82 is offline Novice
    Windows 11 Access 2016
    Join Date
    Jun 2009
    Posts
    25
    Quote Originally Posted by Welshgasman View Post
    I would use a recordset as you have mentioned.
    Then you will have all the data for a particular project number if there are more than one record for a project.
    Actually I have to launch multiples dlookup to find all the fields in the records, I suppose that using recordset to retrieve all the field in the record and the using array sintax to extract them would be a better implementation, do you confirm?

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Well you can retreive more than one field in a dlookup, then split it, but you can access the recordset fields by name, so no need for an array?
    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
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    1) is too vague to make any focused suggestions.
    2)how can i reference the query directly from the click procedure passing the pjnumber as parameter?
    If the query returns what you need, just use it to create a recordset based on the pjnumber by adding

    WHERE tbl_projprof.pjnumber=[Forms]![frm_projects].[pjnumber] to the query. Then create the recordset and forget about DLookups. I imagine you realize that you don't need that subform you tacked on in order to do this.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. SQL Server Stored Procedure Output to MS Access Query
    By Kody_Devl in forum Programming
    Replies: 1
    Last Post: 08-26-2020, 09:42 AM
  2. Command button (event procedure)
    By Nathan23 in forum Forms
    Replies: 5
    Last Post: 08-15-2016, 07:52 AM
  3. Replies: 11
    Last Post: 10-02-2013, 01:50 PM
  4. Access 2010 Query or Event Procedure Issue?
    By justphilip2003 in forum Queries
    Replies: 22
    Last Post: 04-19-2013, 02:39 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