I have a MS Access form where it will ask users to select or type in words that will be used in the where clause for the query.
When the button is clicked, it will build the string and use that to run the query. This will be a read only and to view the subset results from a link table in SQL Server. Nothing more. I tried it with DoCmd.RunSQL but that is only for action type like update, delete, etc.
On click button event, it will have similar query.
strSQL = "Select top 10 * Where ID = ''" + textbox1.value + "'' from linked_Table_Name;"
This did not work.
DoCmd.RunSQL strSQL
Firstly, and most importantly, never concatenate user-obtained data as part of a SQL statement. Any application which uses this technique is wide open to a SQL injection attack and will also fail if the user includes a string delimiter (such as a single quote) or other reserved character in the input they provide.
Instead, use parameters.
For your scenario, I might suggest creating a saved query with the SQL:
select top 10 t.*
from linked_Table_Name t
where t.ID = Forms![Your Form Name]![textbox1]
Where Your Form Name is the name of your form containing the control textbox1.
Then, your on-click event can simply call the OpenQuery method of the DoCmd object:
DoCmd.OpenQuery "YourSavedQuery"
In my Access (2013) database I have a form of which the recordsource is set on load through code. Although the query works fine when I execute it (it returns the right records) I am not able to edit or add new records to it. The adding doesn't matter but I have to be able to edit some records. The query in question is as follows:
SELECT io.*, lpo.batchid, lpo.lydiaUserID
FROM tblInkOrd io
LEFT JOIN tblLydiaPurchaseOrder lpo ON io.becode & '.' & io.ionummer & '.' & io.iovolgnr = lpo.orderNr
WHERE becode='1SW'
ORDER BY IIF(ISNULL(levdat),0,1), levdat DESC, ionummer DESC, iovolgnr DESC
As you can see it JOINS on concatenated values and my bet is that this causes the problem. I used to work with ADP but since I migrated to Linked tables it doesn't.
Any clues how to fix it or how to make a workaround with still the same result set?
I just figured it out. I created a view of the SELECT statement in my SQL Server and linked that view to my Access database and set the right primary keys so it was updatable. Instead of the SELECT statement I used the view and this seems to work (can edit/update the records).
This is not the WHERE x IN(1,2,3) operator, but the Access-specific FROM x IN 'C:\OtherDatabase.mdb' IN clause.
I have a query which looks up a large number of tables from a separate database file (which frequently changes name and/or path with new versions).
SELECT id FROM someTable IN 'C:\OtherDatabase.accdb'
works just great. What I'd like to do is offer some flexibility to the user w.r.t. the path and file name of the other database. Consequently, I have another table with a single row which contains the full path. I've tried the following:
SELECT id FROM someTable IN DLookup("Path", "tblExternalData")
which yields Syntax error in FROM clause, then highlights the opening paren in the SQL editor.
I'd rather not individual link a dozen different tables into this database just to query them (examples provided here obviously simplified) The IN clause seems perfect for my needs, if I can change it when needed, instead of hard-coding the path into the query!
The only method I've found so far is in VBA:
strFileName = DLookup("Path", "tblExternalData")
strSQL = "SELECT id INTO tmpTable FROM someTable IN '" & strFileName & "';"
DoCmd.RunSQL strSQL
I don't find this a fantastic solution--I don't like merging strings to get executable SQL--but it works for now.
I have an Access 2007 database that will be housing tables which refer to the bill of materials for multiple products. On the main form I want a user to be able to select one of the products - OK, easy. Now, I want two queries to run once they press a button after choosing their product from a dropdown. The first query is a simple delete query to delete all information on a table. The second query is where I'm having my issue with my SQL syntax. I want the information from a static table to be appended to the table where the delete query just removed everything from.
Now, each table that houses the bill of material for each product is labeled with the product's name. So I want the dropdown (combo0) to be the reference point for the table name in the FROM clause within the SQL string. Code is as follows:
INSERT INTO tblTempSassyInfo (Concat, TableName, AddressName, PartNumber, [L/R], FeederSize, QtyPerBoard, SASSYname, RawBoard)
SELECT TableName & AddressName & PartNumber, TableName, AddressName, PartNumber, [L/R], FeederSize, QtyPerBoard, SassyName, RawBoard
FROM [FORMS]![DASHBOARD]![Combo0];
So you can see where I'm trying to reference the product name in the dropdown on the form as the table name. Please let me know if this is possible.
"... I'm trying to reference the product name in the dropdown on the form as the table name. Please let me know if this is possible."
It is not possible with Access SQL.
The db engine can only accept the actual table name --- it isn't equipped to reference a form control to find the table name nor to accept any other type of parameter to obtain the table name.
You could change the query to include your combo's value as the table name and then rewrite the SQL from the combo's after update event.
"SELECT * FROM [" & [FORMS]![DASHBOARD]![Combo0] & "]"
A similar approach could keep Access happy. But it may not be the best fit for your application.
So, the user essentially wants 2 queries to run. A DELETE * FROM Table query, and an Append query. The user wants to know what table to utilize for the Append query by using the Combobox (may just be my assumption/interpretation). That being said, why not use something along the lines of:
If IsNull(Me.[Combo0].Value) Then
MsgBox "Please select something."
Me.[Combo0].SetFocus
Cancel = True
Else
Select Case Me.Form!Combo0
Case 1
DoCmd.OpenQuery "DeleteMaterialsTableData" 'Query to delete appropriate table data dependent on Combobox selection'
DoCmd.OpenQuery "QueryNameMaterial1" 'Append records to appropriate table dependent on Combo0 selection'
Case 2
DoCmd.OpenQuery "DeleteMaterialsTableData" 'Query to delete appropriate table data dependent on Combobox selection'
DoCmd.OpenQuery "QueryNameMaterial2" 'Append records to appropriate table dependent on Combo0 selection'
This is just trying to use the users' combobox values to determine which table to run the queries against, instead of the user trying to use the Combobox's value as a table name.
You're pressing a button to do this. This implies that some VBA code is running behind the scene (the Click event of the button). In that case, the answer is a resounding Yes.
Dim strSQL as String
Dim strSQL2 as String
strSQL = "DELETE * FROM tblTempSassyInfo;"
DoCmd.RunSQL (strSQL)
strSQL2 = "INSERT INTO tblTempSassyInfo (Concat, TableName, AddressName, PartNumber, [L/R], FeederSize, QtyPerBoard, SASSYname, RawBoard)
SELECT TableName & AddressName & PartNumber, TableName, AddressName, PartNumber, [L/R], FeederSize, QtyPerBoard, SassyName, RawBoard
FROM " & [FORMS]![DASHBOARD]![Combo0].SelectedValue & ";"
DoCmd.RunSQL (strSQL2)
You may need to tweak that a bit, but it should get you pretty close.
You MAY need to use [FORMS]![DASHBOARD]![Combo0].Columns(0) or Columns(1) instead, I can't remember...
As was stated; Access (and just about any brand database) can definitely do append and delete queries.
The problem is the design. Specifically:
FROM [FORMS]![DASHBOARD]![Combo0];
From clause must be a record set (table) not a call to a control on a form.
My suggestion is to first establish a Select query that has the correct data that you want to append. Save that with a name. You need to be able to do this first.
Once that is done - then create an Append query that uses that saved Select query as its starting record set.
You then just need to trigger the Append query (the Select query will automatically run) using vba behind your button click event:
Docmd.OpenQuery "Append Query Name"
This is 100% possible in MS Access 2010 onward based on my experience. I've not used 2007, but MS says it is possible (see link below). I'm using parametrized queries in a few databases.
PARAMETERS [forms].[dash].[dt_val] DateTime;
SELECT a.F3 AS AdEnt, [forms].[dash].[dt_val] AS Expr1...
The important thing I've found is using a form the user will be interacting with and setting the Date as "DateTime" within the parameter. Here is a video from Microsoft that shows how to and says that it applies to 2007.
Use Parameters in MS Access Queries
Additionally, if you want to do a delete or append, save it as a query then place a button on the form that executes the docmd.runquery for the name of that saved delete/append query.
I often receive requests to query a SQL Server database based on data that is sent to me in an Excel spreadsheet.
I am looking for a more efficient way of completing these types of requests than my current setup:
Currently in order to complete the request I do the following:
Copy the Excel column containing the data that will eventually be placed in a WHERE clause.
Paste the data as text only into Microsoft Word.
Do a find for each paragraph marker and replace it with ', '
Then surround the entire clause with parenthesis to enter into an IN clause.
Does anyone have a suggestion for a more efficient way of accomplishing the same task?
Here are a couple of ways:
Query the excel spreadsheet directly:
SELECT *
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=C:\excelfile.xls', [Sheet1$])
Use excel to format the data:
In next empty column = A1 & "," then copy-down, or ="'"&A1&"',"
You could save the excell as a CSV comma delimited file and go from there but if this is a regular thing i would probably set up an SSIS process to do it all for you