I'm wondering is it possible to dynamically export specific MS Access tables to a CSV file using VBA?
I'm able to export all the tables to a CSV / Excel file but the ask I was given once this was in place, is to be able to export a specific set of tables from the MS Access database a CSV file and not the entire set of tables.
Ideally the user would access a Form which they'd be able to select the tables they would like to export and click a button that would run the script.
Is this possible using VBA or is it to complex? Any advice or resources would be appreciated. Thanks!
You can use this query to populate your listbox:
SELECT MSysObjects.Name
FROM MSysObjects
WHERE ((Left([Name],4)<>"~TMP" And Left([Name],4)<>"MSys" And Left([Name],2)<>"f_") AND ((MSysObjects.Type)=1));
To list the tables in VBA:
Public Sub enum_tables()
Dim td As TableDef
For Each td In CurrentDb.TableDefs
If Left(td.Name, 4) <> "MSys" And Left(td.Name, 4) <> "~TMP" Then
Debug.Print td.Name
End If
Next
End Sub
The matches between the selection in the listbox and the list from 'enum_tables', are the ones you want to export
Related
I can do this beautifully in SQL Assistant and the result will show up in 2 tabs which can be saved to an excel file with 2 tabs.
BT;
sel <query1>
;
Sel <query2>
;
ET
I want to know if something similar can be done in BTEQ. I am prolly hoping against hope but I thought I'd had done my asking around. Using BTEQ I can export 2 different queries to 2 different excel files , and that is fine. I want to know if, its poss. to export the o/p of 2 queries into a single excel file with 2 tabs
and a small corollary question : In SQL Assistant, is it poss to export the result of 2 queries run serially to a 2 tab , excel report by some method other than BTET
( P.S BTET in BTEQ is meaningless , I am using it only in SQL Ass. So dont mean using BTET )
In SQL Assistant there's a setting Tools -> Options -> General -> Use a separate Answer window for which is set by default to Each Query. When you run multiple Select either using F5 or F9 you get all result sets as tabs in a single window and the File -> Save then asks for Save all sheets? This is independent of BT/ET.
In BTEQ there's no way to get multiple results into one file, in fact there's no support for Excel, it's only the very old DIF format which doesn't support multiple tabs.
I have had similar problems.
What you can do is use BTEQ to export the data to a CSV and then use a VBS script to open up the Excel file and copy the CSV in a new worksheet.
First you need to delete the old one otherwise it will get renamed.
Option Explicit
Dim MyPath, objExcel, objWorkbook, objSourceData, objTargetSheet
MyPath = "C:\Users\user\"
Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Visible = True
Set objWorkbook = objExcel.Workbooks.Open(MyPath & "ExcelFile.xlsx")
objExcel.Application.DisplayAlerts = False
objExcel.Sheets("worksheetname").Delete
objExcel.Application.DisplayAlerts = True
Set objSourceData = objExcel.Workbooks.Open(MyPath & "csvfilename.csv")
objSourceData.sheets(1).Copy objworkbook.Sheets(objworkbook.Sheets.Count)
objworkbook.Save
objworkbook.Close
objExcel.Application.Quit
WScript.Quit
Recently I am trying to export the data within each list item on SharePoint to Excel using VBA. Below is the list.
And if you click on each one it looks like:
So the data I crossed out are the values that I want them to be exported to an Excel Spreadsheet.
I know how to export the whole list of to excel, the code is:
Sub ImportSharePointData()
Dim objMyList As ListObject
Dim objWksheet As Worksheet
Const SPServer As String = "http://ln- sharepoint.XXXintra.net/XXX/XXX/XXX/_vti_bin"
Const LISTNAME As String = "{06AAB69F-XXX-XXX-XXX-8F677FE38D76}"
Const VIEWNAME As String = "Agreement"
ActiveSheet.ListObjects.Add SourceType:=xlSrcExternal, Source:= _
Array(SPServer, VIEWNAME, LISTNAME), LinkSource:=True, Destination:=Range("A1")
End Sub
Now I want data in each individual item in this list e.g. Item with ID 367, exporting the field of Counterparty Name, CSID etc to excel.
Also, it looks like these data are filled in by users into forms on SharePoint that are designed using InfoPath.
Is there anyway that I can do this without using the designer level of access?
Thank you very much in advance for any solution or advice.
As long as the desired fields on the InfoPath form are directly associated with SharePoint columns, the data will be available to display and export.
Notice that you specify which view you want when you export the list to Excel.
Const VIEWNAME As String = "Agreement"
With this in mind, simply create a new public view of the list that shows all the columns of data that you want to export. Then export that list view instead.
I have an excel file that has a header row which is a row that I want to delete. The header row in thsi file are the cells of A1 to W1 merged into one. This causes a problem when I try to read the file because I am expecting column names. Proper column names exist in the second row of the file, which is why I want to delete the first.
To accomplish this I thought I'd be able to use the 'Excel Source' item in SSIS since it supports a SQL option to write a query. What I want to do is something like this:
SELECT * from ExcelFile WHERE Row > 1
My file only has data in columns A thru W.
I don't know what syntax I can use in the query to do this. The query builder that is in the Excel Source item will allow me to do many things with columns but I don't see an option for doing anything with rows. Searching online and using the help didn't get me anywhere.
None of these solutions will work because the Excel driver will be confused by the merged first line. You won't be able to use any driver features such as skip first row to do this. You need to run some script to open the Excel file and delete the row manually.
There is some basic sample script at this site:
http://www.sqlservercentral.com/Forums/Topic1327014-1292-1.aspx
The code below is adapted from the code written by snsingh at that site.
You would obviously want to use connnection manager properties, not hard coded paths
Excel needs to be installed on the SSIS Server for it to work - this is the only way to use Excel automation.
Dim filename As String
Dim appExcel As Object
Dim newBook As Object
Dim oSheet1 As Object
appExcel = CreateObject("Excel.Application")
filename = "C:\test.xls"
appExcel.DisplayAlerts = False
newBook = appExcel.Workbooks.Open(filename)
oSheet1 = newBook.worksheets("Sheet1")
oSheet1.Range("A1").Entirerow.Delete()
newBook.SaveAs(filename, FileFormat:=56)
appExcel.Workbooks.Close()
appExcel.Quit()
You don't need to use a syntax.
Go to control flow..
Pull in a data flow task.
Add a excel file source...add a conection manager
With excel sheet.
Open your connection manager and then check the box which says.
Column names In first row. That's it and add ur destination.
I'm trying to create a program that will Import a spreadsheet and append/update it to my MainDB,
I'm done with the Update Process, where if field(JobName) is found in to Main database, then I will update the record/s.
Next step is, to check if the record is existing, if not then I need a code to add that, Please help!
ConnectdbMain 'Sub Where I connect to my Maindb
Connectdbtemp 'Sub Where I connect to Tempdb which I imported to an excelfile
With rs
While Not .EOF
rst.MoveFirst
While Not rst.EOF
If .Fields(Combo.Value) = rst.Fields(Combo2.Value) Then 'the user is allowed which field is to compare and append I use combo box to filter choices.
.Edit
.Fields(Combo3.Value) = rst.Fields(Combo4.Value)
.Update
End If
rst.MoveNext
Wend
.MoveNext
Wend
End If
CloseDbMain
Set rst = Nothing
Dbs.Execute "DROP TABLE TempTable" 'I drop it like a trash since its already appended :)
ClosedbTemp
End With
Hmm... I don't know about you. But if it's a excel spreadsheet I would use OLE DB to read the spreadsheet datasource and use SQL query to select all the DISTINCT JobName in the spreadsheet first.
And I would also use for in loop for every of the spreadsheet distinct name to execute a select statement in your main database with VBA to....
select count(JobName) from table where JobName = the_job_name_in_excel
Then use an If Then to detect if the returned row count = 0 then do an update statement.
Of course there's a lot more so you need to read on how to create a database connection to your main database and spreadsheet if you want to use my method. The methods in creating these connections is easily available out there.
I have an Access database that I need to update every week based on a fixed length text file
The file contains some new records and some updates.
Currently, I am using an ADODB connection to treat the file as a recordset, looping thru its records and adding or updating my records if needed.
The problem is that this process is very slow, complicated and can generate some errors.
Is there a way to achieve the same results using Access SQL?
Since I don't believe Access has any sort of "upsert" functionality, my first inclination would be to create two queries -- an insert query and an update query -- and add a WHERE clause to each to limit the insert and the update to the appropriate records. Then you can combine then under a single transaction, something like this:
With conn 'Assuming you keep using the ADO recordset; you could use DAO instead
.BeginTrans
.Execute "UpdateQuery"
.Execute "InsertQuery"
.CommitTrans
End With
Maybe not ideal, but better than a loop.
I do quite a lot of importing from Excel and have found the easiest way is to import the file to a table and run all the updates/ inserts from this as it is materially quicker once you have the data in a local table.
You could create the table on the fly but I prefer to have the table structure all setup and use TransferText to import, where you can use an import spec.
Loosely to set this up:
Create your table with appropriate field names and data types
Manually import the data from your text file and save the import spec
Use VBA to import future text files and trigger the update/ insert queries
Code could look something like this:
' Folder where files to be imported are saved
strFilePath = "C:\myFolder\"
' Look for a .txt file in that folder
strFileName = Dir(strFilePath & "*.txt")
' Loop through all .txt files until each is imported
Do Until strFileName = ""
strFile = strFilePath & strFileName
' Import the file to a temporary table where "myImportSpec" is what you saved
' the import spec as when you did the manual import and "tbl_IMPORT_Temp"
' is the table you created to run your queries from
'NOTE: This is what i use for .csv files and haven't tested on .txt but
' I think it should work
DoCmd.TransferText acImportDelim, "myImportSpec", "tbl_IMPORT_Temp", strFile
DoCmd.OpenQuery "qryUpdateQuery", acViewNormal
DoCmd.OpenQuery "qryAppendQuery", acViewNormal
' Copy the .txt file to a new folder
FileCopy strFile, strFilePath & "Successful\" & strFileName
' Delete the original file so it isn't imported again
Kill strFile
NextFile:
' Clear your temp import table
DoCmd.RunSQL "DELETE * FROM tbl_IMPORT_Temp"
' Get the next file to be imported
strFileName = Dir(strFilePath & "*.txt")
Loop
Hope this helps. Simon