Results 1 to 12 of 12
  1. #1
    EddieNada is offline Novice
    Windows 10 Access 2007
    Join Date
    Dec 2020
    Posts
    4

    How to prompt to select a database from a directory to run a query and output a report

    I have created a query based on one database but I need to select a database depending on user input from a directory as each month a new database will be created
    i want the query to run on all database with similar name and there will be newer databases created every month

    my sql query is:
    SELECT SLS1220.BILL_NO, SLS1220.SESSION_NO, SLS1220.WAITER, SLS1220.PAY_TYPE, SLS1220.BILL_DATE, SLS1220.BILL_TIME, SLS1220.TOTAL, SLS1220.TAXES, SLS1220.AUTO_GRAT, SLS1220.DISCOUNT, SLS1220.RECEIVED, SLS1220.REV_CENTER
    FROM SLS1220
    WHERE (((SLS1220.BILL_NO)>=Nz([StartNo],0) And (SLS1220.BILL_NO)<=nz([EndNumber],100000000)) AND ((SLS1220.BILL_DATE)>=Nz([Enter Start Date],"01/01/1900") And (SLS1220.BILL_DATE)<=nz([End Date],"31/12/9999")) AND ((SLS1220.RECEIVED)>=Nz([Start Amount],0) And (SLS1220.RECEIVED)<=nz([End Amount],999999999)));

    The SLS1220 next month will be SLS0121 and so on

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Why create a new db. Why not stick to one db and just add data to it?
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Agree with Bob, unless you have a very good reason for doing so - don't.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    EddieNada is offline Novice
    Windows 10 Access 2007
    Join Date
    Dec 2020
    Posts
    4
    I do not have control of the creation of database. but they create one new database every month. I am trying to extract data for creating a report linking all databases the unique thng is the databases all start with 'SLS' followed by the numeric month and numeric year.

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by EddieNada View Post
    I do not have control of the creation of database. but they create one new database every month. I am trying to extract data for creating a report linking all databases the unique thng is the databases all start with 'SLS' followed by the numeric month and numeric year.
    So no input required?, you can calculate from when it is run?
    I would hope that they put all the DBs in one folder.?

    You could create a template SQl where you have SLSmmyy and the replace mmyy with the month and year number.?
    You would need to be able to reference that table from your DB, which I presume you have already done?

    However SLS1220 is a table not a DB?

    You could either run the sql in VBA or amend the querydef sql if the query is stored.
    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
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I think the answer to the first post is use the file dialog file picker (e.g. msoFileDialogFilePicker). User navigates to the db file and picks it and the dialog returns the path to a sub or function. You will have to construct and execute the query sql in code though, because the db name being returned will be dynamic.
    Last edited by Micron; 12-25-2020 at 03:22 PM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Please have a look at the attached sample. You could add to the code to remove the old tables, either after running the report (and saving it as an external PDF) or after you update the query's SQL from the old one to the newly linked one.

    Cheers,
    Vlad
    Attached Files Attached Files
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  8. #8
    EddieNada is offline Novice
    Windows 10 Access 2007
    Join Date
    Dec 2020
    Posts
    4
    thanks how do i get the query to prompt for the db file

  9. #9
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Have you had a look at the file? The sub takes the mmyy as an argument, you could build a table with those and have a combo\list box for the user to select one, you could use an inputbox to prompt the user to type it, etc.

    Cheers,
    Vlad
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  10. #10
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Welcome to the forum...

    How many tables are in each of the dBs? (SLS1020, SLS1120, SLS1220)
    How many records in each of the tables?

    I agree with Bob (Post #2)
    It would be much better and easier to IMPORT the data from each of the dBs into one common dB, than to try and manage picking a dB and creating 1 or more queries.
    However, I would have to see/would like to see examples of the dBs.

  11. #11
    EddieNada is offline Novice
    Windows 10 Access 2007
    Join Date
    Dec 2020
    Posts
    4
    I did, one i am trying to figure how to pass the MMYY variables to the SQL plus how to prompt the Path and filename

  12. #12
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Eddie, you don't need to prompt the path and file name if you store it in the settings table like I suggested in my sample. All you need is the mmyy then you build the file name from that. Lets say your files follow the pattern SLS_MMYY.accdb and they are all stored in C:\SLS_DB_Files, you just need to prompt the user for the desired month_year you replace MMYY in the file name with that.
    Build a table holding all your MMYY that you have data for (or if you have dates instead you can create a query that shows you the distinct MMYY by using the datepart functions) and in your form add a combo for the user to select the MMYY and a button to run the code. In the Click event of the button simply put:
    Code:
    Call vcRunReport(Me.cboMMYY) 'where cboMMYY is the combo to select the month\year
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

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

Similar Threads

  1. Replies: 7
    Last Post: 10-24-2018, 07:29 AM
  2. Replies: 1
    Last Post: 02-19-2018, 02:24 AM
  3. Replies: 2
    Last Post: 03-16-2014, 02:12 PM
  4. Replies: 10
    Last Post: 10-07-2013, 08:20 AM
  5. Replies: 14
    Last Post: 01-17-2013, 02:43 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