Results 1 to 8 of 8
  1. #1
    Adi44 is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2022
    Posts
    4

    Where to Enter VBA Code in a query

    Good Morning
    My Images.zip
    I have joined today in the hope that somebody may be able to help me with a complicated query problem that has been vexing me this week ! First off let me say that I have posted this question on the Access Programmers forum and received a partial solution, here is the link to that conversation
    https://www.access-programmers.co.uk.../#post-1819933

    This solution allows me to place two records (Images) side by side on an access form. The problem arises when I want to either sort or search the DB. The offered solution is :-
    “ If I do everything for you, you will never learn or understand how your app works. but suggest you will need to do the following:

    1. Include the sql of the query as your form recordsource
    2. I don't use macro's Assuming you are just trying to use the standard filter option of

    me.filter="myfield ='" & mycontrol & "'"
    me.filteron=true

    then use the following (I'm assuming Regn is text)
    me.recordsource=replace(me.recordsouce,"WHERE True","WHERE [Regn] = '" & [Regn] & "'")

    if you are using the filter parameter of openform, move your filter parameter (i.,e. "[Regn] = '" & [Regn] & "'") to the openargs parameter instead and in the form open event put
    me.recordsource=replace(me.recordsouce,"WHERE True",me.openargs)



    3. for sorting - you will need a to use controls to to identify which field field you want to sort on and the direction (think how websites work)
    and the code would then be something like

    me.recordsource=replace(me.recordsouce,"A.myTableP K>= B.myTablePK","A." & ctlSortbyWhat & cltSortDirection & "B."& ctlSortbyWhat)

    ctlSortdirection would probably be a combo using a value list to indicate ">=" is ascending and "<" is descending “

    I have been reading up on queries and VBA all week, but still do not have a clue how or where to insert this code into the query. I attach a copy of the DB, would somebody please put me out of my misery !!

    Best Regards

    Adrian

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,959
    You find the SQL of a query object by switching query builder to SQLView.

    A form/report RecordSource property can be set to a table name or query name or SQL statement.

    "The code" you seem to be referencing is VBA. The code does not go into query, it goes into VBA editor. You were instructed "in the form open event put ...".

    Do you know how to create VBA procedure for an event?

    http://www.accessallinone.com/wp-con...ion-To-VBA.pdf
    https://support.microsoft.com/en-us/...7-70649e33be4f
    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.

  3. #3
    Adi44 is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2022
    Posts
    4
    Quote Originally Posted by June7 View Post
    You find the SQL of a query object by switching query builder to SQLView.

    "The code" you seem to be referencing is VBA. The code does not go into query, it goes into VBA editor. You were instructed "in the form open event put ...".

    Do you know how to create procedure for an event?

    http://www.accessallinone.com/wp-con...ion-To-VBA.pdf
    https://support.microsoft.com/en-us/...7-70649e33be4f
    Good Morning

    I am sorry but the limit to my access knowledge is creating a table, a query using the wizard and making a form, so any coding or advanced queries are well beyond my skillset, hence the reason for asking for help, I have tried to read several internet sites to see if i could find anything remotely resembling the instructions, but would not have a clue how to start going about it !

    Regards

    Adrian

  4. #4
    Adi44 is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2022
    Posts
    4
    Good Morning,

    I am afraid the limit of my Access knowledge is creating tables, forms using the wizard and queries using the wizard. I have tried to read several internet sites regarding queries, but would not know how to start or where to place the query code in order for my form to sort or search. Hence the reason for asking for help., the query that I attached was kindly provided by a user on access-programmers, but alas i have not been able to modify the form as instructed.

    Regards

    Adrian

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,007
    @Adi44
    You forgot to mention that you had also crossposted at https://www.utteraccess.com/topics/2.../posts/2797828
    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

  6. #6
    Adi44 is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2022
    Posts
    4
    Ah Sorry I thought I put both Links together at the top !!

    Regards

  7. #7
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,991
    What you asked for (originally at AWF) is not really something that a beginner should normally attempt to do.
    However, you were given more than one solution at that forum.
    Suggest you try the other solution already provided based on a Web browser control.

    If you can't manage that either, then I suggest you either put this idea aside until you have the skills to do it yourself or offer to pay someone to do it for you.
    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

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,959
    Query goes in form RecordSource property. Have you ever opened form or report in design view, explored property settings? I advise not to be dependent on design
    wizards - I seldom use them as they do things I don't like.

    If you want to employ code to manage database, then you need to learn programming concepts and VBA language. You have been provided guidance and help, none of which requires more than basic skill and knowledge of Access functionality to implement.
    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.

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

Similar Threads

  1. On Enter Event code won't run
    By Roncc in forum Access
    Replies: 2
    Last Post: 03-16-2018, 10:16 AM
  2. Replies: 20
    Last Post: 02-24-2015, 10:43 AM
  3. vb code for ENTER
    By Sheba in forum Forms
    Replies: 2
    Last Post: 09-05-2014, 11:58 AM
  4. VBA code to require user to enter a Value
    By rlsublime in forum Access
    Replies: 1
    Last Post: 03-14-2012, 04:34 PM
  5. Replies: 1
    Last Post: 06-10-2010, 10:47 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