473,503 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create Automatic Parameter Query?

58 New Member
Hi there?
I have a PARAMETER QUERY for selecting a certain records on user request, works fine currently; But on every new table I have to set the parametric properties manually inorder for my users to use it. I want it be automatic, just after table selection there should be a way of setting a parameter without going to the design view or whatsoever. The tables can be selected from combobox.
How can I achieve this?
any hint please!
Nov 14 '10 #1
14 3104
colintis
255 Contributor
If you're doing this in through form's combobox, then you'll need to change the query criteria pointed to the form's combo box ([Form]![<form name>]![<combo box name>])

If the above method is not understandable, please tell us more details on what have you done so far. Pasting code (with CODE TAG) would be better if you are not sure how its done properly
Nov 16 '10 #2
Moah Scout
58 New Member
Thanks! What you have explained works even in my db, but in my db I have two tables set as example, the user of this DB will have their own table exported from external sources; After his export he want imediately start using the resources without calling somebody from remote area for assistance on change the query criteria pointed to the form's combo box ([Form]![<form name>]![<combo box name>]), it doent sound good.
What if we build this SQL (query criteria) in a form for whatever selection of a table from combobox.
I have a Combobox shows a list of TablenamesAndQueries in the db, (the queries doesnt have to have the Creteria). The selection criteria should be made in the forms through SQL or VBA.
How could this be achieved?
Please help
Nov 26 '10 #3
colintis
255 Contributor
I'm not understanding quite well on what you tried to say...Can you tell me more about what is this external source? is it another access database table or some other database files? If so you can simply make a link table to that file source, so whenever he gets the updates he doesn't need to change things again again and again. But exclude sources such as SAP, as they do need manual export first somehow.

On the other hand with more VBA parts, you can predefine a set of SQL code, then simply ask an input of the new table name (if structure is the same), and put this name into the FROM clause to run the query at the end as a whole.
Nov 26 '10 #4
Moah Scout
58 New Member
Yeah!
Its a kind of Access Tables from another DB.
Now how to create this linktable to that source? so whenever he gets the updates he doesn't need to change things again again and again
I might get lost if exposed alone to VBA-JUNGLE bymyself.
Please help in both cases!
Nov 26 '10 #5
colintis
255 Contributor
compare to other programming languages such as c++, VBA is simply a small forest.

To create a linked table, I had replied that in your other question thread, see if it works.

The VBA part I mentioned requires to work with the link table, as soon you have the new table linked (with different names), then you have everything prepared in the SQL first except the FROM clause table name. Once the user puts in the new name of the table, everything will go automatically to finish the job.
Nov 26 '10 #6
Moah Scout
58 New Member
That thread could work on exporting Exel2007.
The SQL and VBA are the ones looking for.
Let me be clear!
I have set Connection via ODBC Connection String as follows:
Expand|Select|Wrap|Line Numbers
  1. Sub openDB_DAO()
  2. Dim db As DAO.Database
  3. Dim dbName As String
  4. Dim c As Container
  5. Dim doc As Document
  6. dbName = InputBox("Enter a name of an existing database:", "Database Name")
  7. If dbName = "" Then Exit Sub
  8. If Dir(dbName) = "" Then
  9. MsgBox dbName & " was not found."
  10. Exit Sub
  11. End If
  12. Set db = OpenDatabase(dbName)
  13. With db
  14. ' list the names of the Container objects
  15. For Each c In .Containers
  16. Debug.Print c.Name & " container:" & _
  17. c.Documents.Count
  18. ' list the document names in the specified Container
  19. If c.Documents.Count > 0 Then
  20. For Each doc In c.Documents
  21. Debug.Print vbTab & doc.Name
  22. Next doc
  23. End If
  24. Next c
  25. .Close
  26. End With
  27. End Sub
Now how can I go around this to retrive tables from this Connections and set the parameters so that to link to a Criteria Query?
Nov 26 '10 #7
colintis
255 Contributor
As from the other post we talked earlier, the processes you need is to create link table with Excel. So first I'm point out the thing you need to do, I'll be back to post some example codes later.
  1. Ask user to give input of their excel file
  2. Check if this worksheet name exist in the link table, if yes remove it
  3. create a new link table with that user's excel file
  4. finish the job.
Nov 26 '10 #8
Moah Scout
58 New Member
Yes! We are heading the same direction!
Nov 26 '10 #9
Moah Scout
58 New Member
It can be database or just excel sheet, all have the same meaning for me!
If you give one I will finish the next
Nov 26 '10 #10
colintis
255 Contributor
I'll just simply post the major part you need, you'll sort the rest which it won't be too difficult. Make sure you place them as a module in vba

1. So we're asking for an user input for a file.
Expand|Select|Wrap|Line Numbers
  1. Public strFilePath As String 'This will first from your user input things
  2.  
  3. Function XlsPath() As String
  4.  
  5.     Dim dlg As FileDialog
  6.     Dim VrtSelected As Variant
  7.  
  8.     'Set up the File Dialog for FILE picker
  9.     Set dlg = Application.FileDialog(msoFileDialogFilePicker)
  10.  
  11.     With dlg
  12.  
  13.         'Allow user to make multiple selections in dialog box.
  14.         .AllowMultiSelect = False
  15.  
  16.         'Set the title of the dialog box.
  17.         .Title = "Please Select Database File"
  18.  
  19.         'Clear out the current filters, and add our own.
  20.         .Filters.Clear
  21.         .Filters.Add "Excel Workbooks 2007", "*.xlsx"
  22.  
  23.         'Set location the dialog will show to database's root directory
  24.         .InitialFileName = Left(CurrentDb.Name, Len(CurrentDb.Name) - Len(Dir(CurrentDb.Name)))
  25.         .InitialView = msoFileDialogViewList
  26.  
  27.         'Show the dialog box. If the .Show method returns True, the
  28.         'user picked at least one file. If the .Show method returns
  29.         'False, the user clicked Cancel.
  30.         If .Show = True Then
  31.  
  32.             'Loop through each file selected and add it to the list box.
  33.             For Each VrtSelected In .SelectedItems
  34.                 XlsPath = VrtSelected
  35.             Next
  36.  
  37.         End If
  38.  
  39.     End With
  40.  
  41.     Set dlg = Nothing
  42.  
  43. End Function
From here, you'll need to think of some ways to extract the file name for step 2 from XlsPath

2. Check if the name exists already in the table list
Expand|Select|Wrap|Line Numbers
  1. Dim db As DAO.Database
  2. Dim td As DAO.TableDef
  3. Set db = CurrentDb
  4. On Error Resume Next
  5. Set td = db.TableDefs(strFileName)
  6. If Err.Number = 0 Then
  7.     MsgBox "Table found."
  8.     db.TableDef.Delete strFileName
  9.     Err.Clear
  10. Else
  11.     MsgBox "Table not found."
  12.     Err.Clear
  13. End If
  14.  
  15. Set db = nothing
3. Create new linked table that user selected earlier
Expand|Select|Wrap|Line Numbers
  1. Dim db As Database
  2. Dim linktbldef As TableDef
  3. Dim sourceTable As String
  4.  
  5. sourceTable = tblName & "$"   'Get the worksheet name you want to link
  6.  
  7.     Set db = CurrentDb
  8.     Set linktbldef = db.CreateTableDef("tmptable")  'create temporary table definition
  9.     linktbldef.Connect = "Excel 12.0;HDR=YES;IMEX=2;DATABASE=" & FilePath                  'set the connection string for Excel 2007
  10.     linktbldef.SourceTableName = sourceTable        'attach the source table
  11.     db.TableDefs.Append linktbldef                  'add the table definition to the group
  12.     db.TableDefs.Refresh                            'refresh the tabledefinitions
  13.  
  14.     linktbldef.Name = sourceTable                   'rename the tmptable to original source table name
4. From here then on you'll on your own trying to get the stuffs working. Good luck.
Nov 26 '10 #11
Moah Scout
58 New Member
If you run the first Code, it gives error and point to the dlg As FileDialog:
User defined-Type not Defined.
I couldnt go around that error!
Nov 26 '10 #12
colintis
255 Contributor
Just forgot, you need to reference the Microsoft Office 12.0 Object Library

In the VBA menu, tool-->reference, then find office 12.0 object library

You also need other libraries in the reference to get all your stuffs work. Look around and see.
Nov 26 '10 #13
Moah Scout
58 New Member
Well it works after setting the Microsoft Office 14 Object Library, but it just open the dialogbox with .*XLSX* file format but it cannot allow me to select the desired sheet in a Spreadsheet and yet nothing happens after Click-OK; Only dialogbox dissapears. Assume it opens Successfully!
My question is How to set the newly opened .xlsx document to my query criteria without entering them mannualy?
Nov 27 '10 #14
colintis
255 Contributor
There are many more to complete, as I told you earlier those are the major parts, not 100% of the whole picture, and as for that it is then your responsible to figure it out. This is the furthest hint I can give you, unless you actually looking for someone to do the work for you.

If you look carefully to the codes, you'll notice why it just look for .xlsx files, you can then put other more excel extension files to it if you like.

And look clearly on how the function is defined, the function itself actually return the path of the file, this is where you work it out on how it can be store as well.

After that, it is another part of the code, which you will be read through the excel file, and list out the worksheets available in it or look for a specific one you like. Below will be the last bit of code about listing worksheets from the file, make yourself work around with the module function reading excel file.
Expand|Select|Wrap|Line Numbers
  1. Dim xl As Excel.Application
  2. Dim xlsht As Excel.Worksheet
  3. Dim xlwb As Excel.Workbook
  4.  
  5. Set xl = CreateObject("Excel.Application")
  6. Set xlwb = GetObject(FilePath)
  7.  
  8. For Each xlsht In xlwb.Worksheets
  9.     'lstShts is a listbox item on a form
  10.     lstShts.AddItem (xlsht.Name)
  11. Next xlsht
Nov 27 '10 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3914
by: Greg | last post by:
I am trying to set a perameter for my query but keep getting an error: The Microsoft Jet database engine does not reconize '' as a valid field name or expression. The only difference from other...
1
3791
by: Cory | last post by:
When I run the subroutine testEmailContacts the msgbox says that there is only 1 record. The sql for qyEmailContactsQyCard is below. There is over 3000 records in the table "tbl:Contact". What am i...
1
2556
by: carrionk | last post by:
Hi, I have created a Subform which SourceObject is a parameter query. This is the Query: Qry Name:80IsscomProduct SELECT * FROM Isscomp28 WHERE Like ;
1
1371
by: biljana | last post by:
Hi! Could somebody help me to create simply parameter query? I need query like this: select from table_name; column_name should be parameter from combo box or user can type column name.
2
1762
by: Haas C | last post by:
Hey all, I created a Query (in Design View) which asks the user for an "As Of Date" which would then display relevant data. I put in the Criteria Row of the Query Design for the Date field. Is...
1
1475
by: deveshparekh | last post by:
I have created a query asking to calculate a third number using two fields in the query. However when I run the query I get a enter parameter query box asking me to enter information on one of the...
4
2901
by: GladGad | last post by:
I am not a real well versed query writer and therefore am having some problems figuring out how to write a parameter query where I want to input a partial name. I have tried a few different things,...
1
458
by: Kissi Asiedu | last post by:
How could I create a parameter query that will return values that partially or whole match to a user input. For instance; if a user inputs "stu", it should return values that have "stu" in it. Like...
2
3508
by: brat33 | last post by:
I am trying to modify some code to create a mail merged label document within Word 2007, using a Access 2007 Parameter Field. My issue comes about when I cannot see the Parameter Query within the...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7074
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7273
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6982
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.