I am importing data from excel that works fine.
The problem I need to resolve is that when it imports I need one column to be memo before the data goes in. At the moment not all data is importing because of the column not set to memo.
My Code:
Dim oExcel As Object, oWb As Object
Set oExcel = CreateObject("Excel.Application")
Set oWb = oExcel.workbooks.Open(FileName:=strFile, _
Password:=strPassword)
DoCmd.TransferSpreadsheet acImport, _
acSpreadsheetTypeExcel9, "_tmp_table", strFile, -1
oWb.Close SaveChanges:=False
oExcel.Quit
Set oExcel = Nothing
Dim strClsExl As String
strClsExl = "TASKKILL /F /IM Excel.exe"
Shell strClsExl, vbHide
Any help on how to do this is appreciated
If you set the import temp_table up beforehand with the appropriate field type already set to Memo, it will behave as you want it to.
Related
Good morning, I'm in Access / Vba needing to import an xlsx spreadsheet with password to an Access table, I can't, xls spreadsheet works but xlsx doesn't work. Can someone help me?
'This code does not work.
Function ImportProtected(strFile As String, _
strPassword As String)
Dim exApp As Excel.Application
Dim oExcel As Object, oWb As Object, wkb As Object
Set oExcel = CreateObject("Excel.Application")
Set oWb = oExcel.Workbooks.Open(FileName:=strFile, Password:=strPassword)
DoCmd.TransferSpreadsheet transfertype:=acImport, SpreadsheetType:=5, _
TableName:="tmpTableName", FileName:=strFile, _
Hasfieldnames:=True, Range:="Q1!C:G"
Set exApp = appExcel.Workbooks.Open(strFile)
exApp.Password = strPassword
exApp.Save
exApp.Close
oWb.Close SaveChanges:=False
oExcel.Quit
Set oExcel = Nothing
End Function
You did not tell us what is the error. Saying that is does not work does not really help. When I look at your code I do not understand why are you opening the same file twice.
This code should not be there:
Set exApp = appExcel.Workbooks.Open(strFile)
exApp.Password = strPassword
exApp.Save
exApp.Close
You are trying to open a file which is already open and locked. This may be the error.
How to export the table into csv format using access vba code?
I can able to export into csv format, but i need the output without quotation marks in the csv file.
Actual Results:
"scenario 1",17.00,100.00,"75",20.00,"Albert","Detweiler",13-04-1963 0:00:00,"123 Main","Mesa","AZ",85282.00
"scenario 2",16.00,75.00,"A3",5.24,"Benjamin","Detweiler",06-07-1973 0:00:00,"123 Main","Mesa","AZ",85282.00
Expected results:
scenario 1,17.00,100.00,75,20.00,Albert,Detweiler,13-04-1963 0:00:00,123 Main,Mesa,AZ,85282.00
scenario 2,16.00,75.00,A3,5.24,Benjamin,Detweiler,06-07-1973 0:00:00,123 Main,Mesa,AZ,85282.00
Someone please help for this question. Thanks in advance!
Sample code:
Option Compare Database
Sub exportQuery()
Dim exportSQL As String
Dim db As DAO.Database, qd As DAO.QueryDef
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogSaveAs)
Set db = CurrentDb
'Check to see if querydef exists
For i = 0 To (db.QueryDefs.Count - 1)
If db.QueryDefs(i).Name = "Sheet1" Then
db.QueryDefs.Delete ("Sheet1")
Exit For
End If
Next i
Set qd = db.CreateQueryDef("Sheet8", exportSQL)
'Set intial filename
fd.InitialFileName = "export_" & Format(Date, "mmddyyy") & ".csv"
If fd.Show = True Then
If Format(fd.SelectedItems(1)) <> vbNullString Then
DoCmd.TransferText acExportDelim, "Standard Output ", "Sheet1", fd.SelectedItems(1), False
End If
End If
'Cleanup
db.QueryDefs.Delete "Sheet1"
db.Close
Set db = Nothing
Set qd = Nothing
Set fd = Nothing
End Sub
I'm wrote the following macro which imports data from an excel file into access. The data i'm importing fluctuates however (sometimes A1:B2, sometimes A1:B5 etc...) so what I ideally would want is that it selects all the relevant data. So it should do something like:
Select cell A1
XLtoRight
XLDown
Copy this in access...
Anybody an idea on how I can achieve this?
Sub ImportExcel()
Set ExcelApp = CreateObject("Excel.Application")
Set ExcelWb = ExcelApp.Workbooks.Open("C:\Documents and Settings\aa471714\Desktop\Book1.xls")
ExcelApp.Visible = True
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel3, "Gegevens", "C:\Documents and Settings\aa471714\Desktop\Book1.xls", True, "A1:B5"
With ExcelApp
.Quit
End With
MsgBox ("De gegevens zijn ingelezen")
End Sub
Range is an optional parameter. Try skipping it to import the entire worksheet.
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel3, "Gegevens", "C:\Documents and Settings\aa471714\Desktop\Book1.xls", True
I am looking for sample "source code" to do this so I can create a button to automatically export all tables to spread sheets with the same name but with a .xls extension. I already know how to export tables manually.
I haven't tested but something like this should work for exporting to all same workbook...
Dim lTbl As Long
Dim strFile As String
Dim d As Database
'Set current database to a variable adn create a new Excel instance
Set d = CurrentDb
strFile = "c:\FolderName\FileName.xls" '## Change to file you want
'Loop through all tables
For lTbl = 0 To d.TableDefs.Count
'If the table name is a temporary or system table then ignore it
If Left(d.TableDefs(lTbl).Name, 1) = "~" Or _
Left(d.TableDefs(lTbl).Name, 4) = "MSYS" Then
'~ indicates a temporary table
'MSYS indicates a system level table
Else
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, d.TableDefs(lTbl).Name, strFile
End If
Next lTbl
'Release database object from memory
Set d = Nothing
Or this for all separate workbooks:
Dim lTbl As Long
Dim strFile As String
Dim d As Database
'Set current database to a variable adn create a new Excel instance
Set d = CurrentDb
Set xlApp = CreateObject("Excel.Application")
strFilePath = "c:\Database\" '## Cahnge to where you want to save"
'Loop through all tables
For lTbl = 0 To d.TableDefs.Count
'If the table name is a temporary or system table then ignore it
If Left(d.TableDefs(lTbl).Name, 1) = "~" Or _
Left(d.TableDefs(lTbl).Name, 4) = "MSYS" Then
'~ indicates a temporary table
'MSYS indicates a system level table
Else
Set wbExcel = xlApp.workbooks.Add
strFile = d.TableDefs(lTbl).Name & ".xls"
wbExcel.SaveAs FileName:=strFilePath & strFile, FileFormat:=56
wbExcel.Close
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, d.TableDefs(lTbl).Name, strFilePath & strFile
Set wbExcel = Nothing
End If
Next lTbl
xlApp.Quit
Set wbExcel = Nothing
Set xlApp = Nothing
'Release database object from memory
Set d = Nothing
I need help coming up with a method to allow a user to export a query's results to an xls file on a button click event.
I've tried using an Output To macro, but it doesn't work for a query containing 30,000+ records.
Thanks in advance
You might want to consider using Automation to create an Excel spreadsheet and populate it on your own rather than using a macro.
Here's a function I have used in the past to do just that.
Public Function ExportToExcel(FileToCreate As String, ByRef rst As ADODB.Recordset)
'Parms: FileToCreate - Full path and file name to Excel spreadsheet to create
' rst - Populated ADO recordset to export
On Error GoTo Err_Handler
Dim objExcel As Object
Dim objBook As Object
Dim objSheet As Object
'Create a new excel workbook; use late binding to prevent issues with different versions of Excel being
'installed on dev machine vs user machine
Set objExcel = CreateObject("Excel.Application")
Set objBook = objExcel.Workbooks.Add
'Hide the workbook temporarily from the user
objExcel.Visible = False
objBook.SaveAs (FileToCreate)
'Remove Worksheets so we're left with just one in the Workbook for starters
Do Until objBook.Worksheets.Count = 1
Set objSheet = objBook.Worksheets(objBook.Worksheets.Count - 1)
objSheet.Delete
Loop
Set objSheet = objBook.Worksheets(1)
rst.MoveFirst
'Use CopyFromRecordset method as this is faster than writing data one row at a time
objSheet.Range("A1").CopyFromRecordset rst
'The UsedRange.Rows.Count property can be used to identify the last row of actual data in the spreadsheet
'This is sometimes useful if you need to add a summary row or otherwise manipulate the data
'Dim lngUsedRange As Long
'lngUsedRange = objSheet.UsedRange.Rows.Count
'Save the spreadsheet
objBook.Save
objExcel.Visible = True
ExportToExcel = True
Err_Handler:
Set objSheet = Nothing
Set objBook = Nothing
Set objExcel = Nothing
DoCmd.Hourglass False
If Err.Number <> 0 Then
Err.Raise Err.Number, Err.Source, Err.Description
End If
End Function
Can you use VBA?
Intellisense will help you, but get started with:
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "my_query_name", "C:\myfilename.xls"
Note: you may have a different Excel version
"my_query_name" is the name of your query or table
you'll need to set the file location to the appropriate location\name .extension
More Info: http://msdn.microsoft.com/en-us/library/bb214134.aspx