import worksheet according worksheet number - ms-access

I have to import one excel file having 8 worksheet in it, but i need to import only second worksheet. Worksheet name will not be same every time as i always need to import Sheet2
If i am giving the name of the worksheet then it is importing successfully but i don't want to import through specific name, i want to import according to worksheet number 2
DoCmd.TransferSpreadsheet acImport, 8, "P_TEMP", strFName, False, "Sheet2!A2:N"
P_TEMP is table name, strFName is file name

Not sure if there is a more elegant solution, Access not being my area of knowledge, but you need to ascertain the worksheet's name from it's index, and at the very least this should be possible if you instantiate Excel so that you can use it's methods/properties to get the sheet's name:
Dim shtName as String
With CreateObject("Excel.Application")
shtName = .Workbooks.Open(strFname).Sheets(2).Name
.Quit
End With
DoCmd.TransferSpreadsheet acImport, 8, "P_TEMP", strFName, False, shtName & "!A2:N"

Related

import multiple CSV file into Access database and save them into different tables based on the file name

I've got about 100 CSV files that I'm trying to import them into Access and then rename the tables based on the file names.
Here is the code I've found but the "tablename" should be my file name. however, I can't get it to work as I'm new to scripting.
Function Import_multi_csv()
Dim fs, fldr, fls, fl
Set fs = CreateObject("Scripting.FileSystemObject")
Set fldr = fs.getfolder("D:Files\")
Set fls = fldr.files
For Each fl In fls
If Right(fl.Name, 4) = ".csv" Then
DoCmd.TransferText acImportDelim, , "TableName", "D:Files\" & fl.Name, False
End If
Next fl
End Function
Also, I have three columns in my files and I want the third column to be imported as a double.
Any help will be appreciated.
It should be this:
DoCmd.TransferText acImportDelim, , "[" & fs.GetBaseName(fl.Name) & "]", "D:Files\" & fl.Name, False
As for your second question, you could create, save, and use an import specification.

Importing CSV into MS-Access using form button, confusing error

I'm trying to import a CSV file that is created from a web form I developed. When the form submits it creates a record in my CSV with a multitude of customer information.
As per requirements I needed to put it into a CSV, and then separately have it import into an Access database for others to use (Two steps required for server security).
The way I'm trying to do it is with a simple form with a button on it inside Access, that simply says Import, that will pull an update of the CSV whenever the user needs it.
My error is confusing me as it's stating
"Field 'F1' doesn't exist in destination table 'Applications' "
I do not have a field in my CSV labeled F1, or even any record that contains 'F1', and there is no field named F1 in my access table Applications (obviously).
Here is my VB module code from Access
Option Compare Database
Sub ImportingCSV()
Function Import()
On Error GoTo Macro1_Err
DoCmd.TransferText acImportDelim, "", "Applications", "C:\Users\ALee\Documents\formTesting22.csv", False, ""
Import:
Exit Function
Macro1_Err:
MsgBox Error$
Resume Macro1_Exit
End Function
And here is my CSV file format (spaced out for your readability)
OPUCN#WVQNAJT4PD,
2017.05.03,
test,
v,
90545452929,
4062033985,
No,
VM#TEST.VMTEST,
10003937683827,
test,
test,
689 395 3967,
2048 2983999,
No,rle#don.ca,
111 e Streeth south,
12,
Temporary,
Commercial,
100,
200,
300,
208/120V,
Three-Phase,
Underground (UG),
Ganged Position*,
23,
"dsbsdhfbslhfbshfbsdhlfbgshdfgsfslfgljshgfljshgfljshgflsj"
The error is telling me that the field for the second phone number ("4062033985" in the CSV) doesn't have a field in the table Applications, but it does! "F1" in the CSV is Customer Mobile. When I import manually through Access's import wizard this works fine.
Hopefully someone can point me in the right direction, not familiar with VB script or macros in access.
Don't import the file.
Link the csv file as a table. Then create a query to read and convert (purify) the data.
Use this query as source for further processing of the date like appending data to other tables.
a CSV file is a spreadsheet... try...
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml,[YourDestinationTable],"C:\YourFileDirectoryPath, filename, and extension",true,[Spreadsheet name if multiple sheet names]
There are all kinds of ways to do this sort of thing. This is certainly the simplest method.
Private Sub Command0_Click()
DoCmd.TransferText acImportDelim, "", "Book1", "C:\your_path_here\Book1.csv", True, ""
End Sub
Let's say you want to import several CSV files, all of the same type, into the same table. Just run the script below.
Option Compare Database
Option Explicit
Private Sub Command0_Click()
DoImport
End Sub
Function DoImport()
Dim strPathFile As String
Dim strFile As String
Dim strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean
' Change this next line to True if the first row in CSV worksheet
' has field names
blnHasFieldNames = True
' Replace C:\Documents\ with the real path to the folder that
' contains the CSV files
strPath = "C:\your_path_here\"
' Replace tablename with the real name of the table into which
' the data are to be imported
strFile = Dir(strPath & "*.csv")
Do While Len(strFile) > 0
strTable = Left(strFile, Len(strFile) - 4)
strPathFile = strPath & strFile
DoCmd.TransferText acImportDelim, , strTable, strPathFile, blnHasFieldNames
' Uncomment out the next code step if you want to delete the
' EXCEL file after it's been imported
' Kill strPathFile
strFile = Dir()
Loop
End Function
You can do all kinds of other thins too; navigate to a file using the msoFileDialogFilePicker; you can loop through record sets and load them, one by one, into your table. As Gustav suggested, you can link to your file (staging) and write records into a table (production). You should probably try all of these methods, and play around with

need to dynamically insert attachment to an access form from a local system and save the address into a table

Hey i am new to access database.
I am creating a form in which i need to attach a excel file from the local system. I tried to use the attachment control to attach the document. But i am not able to store it into a table. I need to use that excel document for my further processing. I need to get the path from which the data is selected from my local system.
I hard coded the path and i was able to do my operation but now i need to dynamically fetch the data from the location.
thanks in advance
My code for hard coding looks like this
Private Sub Command4_Click()
Dim dbs As DAO.Database
Set dbs = CurrentDb
If (ifTableExists("featuretable") = True) Then
dbs.Execute "Delete * from featuretable"
End If
Dim filepath As String
filepath = "C:\Users\jolly#iese.fhg.de\Desktop\featurevalues.xlsx"**
DoCmd.TransferSpreadsheet acImport, , "featuretable", filepath, True
fmfeaturesubform.Form.Requery
End Sub
"Attach" and "import" are completely different things. i guess you want to import the excel sheet.
one way would be use the Application.FileDialog:
http://msdn.microsoft.com/en-us/library/office/ff196794(v=office.15).aspx
another way would be search your current folder and import matching filenames:
Dim mBaseFolder As String
Dim mFname as string
mBaseFolder = "C:\test\" ' or application.CurrentProject.Path
mFname = Dir(mBaseFolder & "*.xls")
Do While fname <> ""
DoCmd.TransferSpreadsheet acImport, , "featuretable", mFname , True
mFname = dir()
Loop

Import data in access from excel

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

docmd.TransferSpreadsheet Access --> Excel //// specify destination worksheet AND range

I have to write some Access VBA to export data from an Access query into a specific range of cells in an Excel document that has several worksheets.
I am having trouble finding the right way to specify the worksheet AND range.
Here is what I have so far:
docmd.TransferSpreadsheet(TransferType:=acExport, SpreadsheetType:=acSpreadsheetTypeExcel8, TableName:=qry_Main, _
FileName:="c:\test.xlsm", _
HasFieldNames:=False, _
Range:="Main!J9:J10")
The broken piece is Range:="Main!J9:J10"
What's the proper way to make this reference?
You can use CopyFromRecordset and automation:
Sub XLTrans()
''Reference: Microsoft ActiveX Data Object x.x Library
Dim rs As New ADODB.Recordset
Dim xl As Object ''Excel.Application
Dim wb As Object ''Workbook
Set xl = CreateObject("Excel.Application")
''Pick one
''1. New book
Set wb = xl.Workbooks.Add
''2. Existing book
Set wb = xl.Workbooks.Open("z:\docs\book1.xlsx")
''Connection relevant for 2007 or 2010
rs.Open "MyTableOrQuery", CurrentProject.AccessConnection
wb.Sheets("Sheet1").Cells(4, 5).CopyFromRecordset rs
xl.Visible = True
End Sub
Note that this will not include column headings, but you can add them as well, for example:
For i = 0 To rs.Fields.Count - 1
Worksheets("Sheet1").Cells(3, i + 5) = rs(i).Name
Next
http://msdn.microsoft.com/en-us/library/office/ff844793.aspx
http://msdn.microsoft.com/en-us/library/office/aa141565(v=office.10).aspx
You cannot use RANGE for exporting:
"
Range Optional Variant. A string expression that's a valid range of cells or the name of a range in the spreadsheet. This argument applies only to importing. Leave this argument blank to import the entire spreadsheet. When you export to a spreadsheet, you must leave this argument blank. If you enter a range, the export will fail.
"