VBA Userform Listbox into SQL temp table - sql

I have userform in Excel VBA that contains two multiselect listboxes. Essentially its the add/remove concept where the box on the left gives me a lits of analysis names from SQL and you select the ones you want into the box on the right.
What I'm now trying to do is take the information in the list box that the user wants to use and put that into a temp table in SQL so that I can run a query that loops through the values in the temp table.
I was going to create a .csv file save if and then inser the temp table into SQL. However that seemed long winded and made me think there must be a more direct way, however this is beyhond my SQL/VBA skills.
Many Thanks

Related

Search SQL table using excel to return only matching values

I have a large table in my SQL database with a few million rows so I can't just load it into excel. I'd like to setup a data connection in Excel to the database which I can do now, but I want to be able to open the excel sheet, type my search parameter in a cell and then have the matching rows returned from the database table into the excel sheet.
Does anyone know if that's possible? I can't find a way to do it without manually adjusting the search term in the connection properties because I don't know how to pass along what I typed in the cell to be the search term.
Ok figured it out, microsoft has a great guide for it here:
https://support.microsoft.com/en-us/office/create-a-parameter-query-in-microsoft-query-c67d9af7-c8a0-4bf7-937c-087cb25f7ad3

Query MS-Access Form ComboBox

as the title suggests I am writing SQL out of excel vba to query Forms contents out of a MS-Access db. The SQL works fine however, the fields where combo boxes are, the SQL returns their index instead of the text field.
I spent some time googling this but most of the results are asking how to display on the form in the combo box, I am just trying to return the text display form the combo box with my SQL.
I will go ahead and say the person who designed this did a bad job, and the tables relating to these drops down have nothing in common(the tables are just a list of the drop down values and ID's).
My question is what is the best way to return this value? Can I join based off the drop down index?
This link should help you get started.
http://access.mvps.org/access/forms/frm0031.htm
Something like this, I presume...
Forms!Mainform.RecordSource

Access - Create Select Statement via Excel Template

to export data from an Access 2010 Database I would like to use an Excel template as seen below.
In the first row I can define columns that are in the database. In the second line I can define exactly 1 filter which will be used in the "where" statement.
When pressing a button in Access, a query that is based on the defined columns and filters should be executed and the result will be exported to the Excel.
I am honest, I am not that skilled in VBA. What would be the best point to start from.
Your question is way too broad and as such it should not even be here, but let me give you some points to look at.
First you'll need to get your data from the Excel file. If you are not skilled with VBA, then the easiest way would be opening it through VBA using an Excel.Application object and Workbooks.Open method, and get your values from your Range.
Create an SQL Select statement from your values.
Do not use just a SELECT query, make it a SELECT INTO query (a "make table query" as Access likes to call it). That way your results will be inserted into a (new) table, making it easier to output.
Export your new table to Excel. There are several ways to do this, search for DoCmd.OutputTo or DoCmd.TransferSpreadsheet.

Trying to use VBA to delete an entire row if one field in null/blank

So I am working on a project where I import an excel file into Access, but when I do the importing( the tables in excel have the same tables headings as in Access) I tend to get a bunch of extra rows because in my excel file I have functions behind most of the cells, so that is why, even tho it appears to be empty, Access transfers the rows even tho nothing is actually entered into it.
So my question is, is there a way by using VBA in Access that I can automatically, once the excel file is imported, It could loop through all the rows in a specific table and delete an entire row based on specific criteria. Like if within a row there is an empty field, it will delete the whole entire row. This will save me a lot of time, instead of manually searching through the table for blank fields and deleting the row myself.
I do have knowledge working with VBA but im unsure of how to go about doing this, I was trying to use a DELETE SQL statement, but couldn't figure out how to do it properly since I need it to be In VBA.
Create a SQL query that pulls back the rows you want to delete.
Once the query is finished to your liking, go into SQL View for
the query, and remove everything before the FROM clause.
Instead, type a DELETE there. Save the query. So it should look like "DELETE FROM blah WHERE blah, blah, blah"
So now you have a query that will delete rows that you want it to. Now, to get it to run through VBA (note that you can run it manually just by double-clicking it in the Queries pane), just put the following line in where you want it to run.
docmd.OpenQuery "yourDeleteQueryName"
Note that yourDeleteQueryName should be replaced with your Delete query name.

collecting SQL statements using Excel

in my everyday work, I am receiving data in Excel spreadsheets, which I need to insert into relational database.
To accomplish this, I prepare formulas which generate "insert" statement (I am using both insert and select statement for example to choose ID of all elements with specific label).
Because those spreadsheets are complex, they contain SQL commands in more than one column.
This is the point where problems begin - I cannot simply select all cells, copy them and paste into SQL Server (it will concatenate information from cells in the same row).
In most cases I'm preparing additional sheet where I'm collecting all statements in one column
(using simply formula which rewrite text from other cells). Unfortunately preparing such sheet is time consuming and might causing an error (for example if I forgot about column or I add rows).
Is there any more convenient way to do it?
I thought about writing a macro which collect all values from selected range.
Is it good idea or can I use something better?
You can do all that using VBA.
You know what are the rules so you have the business logic in your head. Now, just type the code to do it :)
If you want you can do the insert in the Excel using something like this.