I'm exporting from an Access DB via VBscript and want to try using a Schema.ini file. I have a batch program that fires off a vbscript file that opens the Access DB and does the export. My vbscript filename is export.vbs.
If I have:
DoCmd.TransferText acExportDelim, "", "table", "C:\output\table.csv"
I notice while the export is happening a file gets automatically created "export.ini".
If I have
DoCmd.TransferText acExportDelim, "Schema.ini", "table", "C:\output\table.csv"
I get an error talking about "The text file spec 'Schema.ini" does not exist." even though in the same directory this is all happening in I have a Schema.ini file there. What am I missing?
Thanks!
You need to go about it a different way ( Create comma separated file (csv) from access - scheduled daily from windows )
Set cn = CreateObject("ADODB.Connection")
cn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=z:\Docs\test.accdb"
sSQL = "select * into "
sSQL= sSQL & "[text;database=z:\docs\;FMT=Delimited;HDR=Yes].[csvfile.csv]"
sSQL= sSQL & " from table1"
cn.Execute sSQL
Related
I am making one form, where you enter a code. Using that code I get the exact Date of that record (e.g Code"100346" goes for record on 14.03.2013, and so on). When I have the record date, I have to open a DBF file (the actual record) with a name formed by the date.
(e.g Date is 14.03.2013 and the file name will be N140313.DBF)
How can I program access to open/import that exact file, so I can work with it in access?
The answer may depend on how (what methods) you want to work with the DBF file, and I don't know what you have in mind there.
Perhaps you would be satisfied with a query to retrieve the DBF data. I have a dBase III file at this location: C:\Users\hans\Documents\F_NAMES.DBF
Then this query in Access 2007 gives me an editable result set ... meaning I can not just view but also alter the stored data.
SELECT *
FROM [dBase III;DATABASE=C:\Users\hans\Documents\].F_NAMES;
If you can create a similar query, you could use it as the record source for a form and view and edit your data in that form.
Here is a code fragment that I found googling... Its worth a try. I can't verify it because I have no DBF files...
Dim cn As Object
Dim rs As Object
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
Dim oneSQL As String
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\;" & _
"Extended Properties=dBASE IV;User ID=Admin;"
' works also
'strCon = "Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=F:\;"
cn.CursorLocation = adUseClient ' allows you to see number of records returned
cn.Open strCon
oneSQL = "select * from [data.dbf];" ' F:\data.dbf
rs.Open oneSQL, cn, , , adCmdText
I want to export record set "myTableRS" from Access 2010 into .xlsx file via VBA, but its showing the error as " The expression you entered is the wrong datatype for one of its argument". If I access any field value from the record set in msgbox using Msgbox(myTableRS![Field3]) so its working fine. Even when i export Access Table so the below code is working fine but not working for myTableRS record set.
I am using the code :
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, _
myTableRS, "D:\MAS.xlsx", True
Can anyone tell how to fix it?
How do I create a saved query? I have a table from which I want to search a particular record and save that particular record only at the same time of search in xlsx
If you look at TransferSpreadsheet, you will see that table name is a string.
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, _
"myTableRS", "D:\MAS.xlsx", True
This means that you cannot export a recordset using TransferSpreadsheet, however, you can create a saved query and export that.
Create a saved query
sSQL = "SELECT * FROM MyTable WHERE Id = " & thenumber
If Not IsNull(DLookup( _
"ExportQuery", "MSysObjects", "[Name]='ExportQuery' AND Type= 5")) Then
''Query exists, overwrite the sql permanently
CurrentDb.QueryDefs("ExportQuery").SQL = sSQL
Else
CurrentDb.CreateQueryDef "ExportQuery", sSQL
End If
I'd like to import all Excel files (with different data and columns) from some directory into MS Access 2010 database, creating new table for each file. I've found the code to import files into one table:
Option Compare Database
Option Explicit
Function DoImport()
Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean
' Change this next line to True if the first row in EXCEL worksheet
' has field names
blnHasFieldNames = True
' Replace C:\Documents\ with the real path to the folder that
' contains the EXCEL files
strPath = "C:\Documents and Settings\myName\My Documents\Access Test\"
' Replace tablename with the real name of the table into which
' the data are to be imported
strTable = "tablename"
strFile = Dir(strPath & "*.xls")
Do While Len(strFile) > 0
strPathFile = strPath & strFile
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
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
But I need to create new table each time. Is it possible in VBA?
I think all you need to do is change the destination table name (the value of strTable) each time before you do DoCmd.TransferSpreadsheet.
In a comment you said you want the table name to be derived from the workbook file name. And, each time through your loop, another variable (strFile) contains the file name. So I think you could strip the file extension from that file name and use it as the Access table name.
Here is an Immediate window example which demonstrate how that can be done ...
strFile = "foo.xls"
strTable = Left(strFile, Len(strFile) - 4)
? strTable
foo
If that approach is suitable, revise the loop in your VBA code like this (untested) code snippet ...
strFile = Dir(strPath & "*.xls")
Do While Len(strFile) > 0
strPathFile = strPath & strFile
strTable = Left(strFile, Len(strFile) - 4)
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
strTable, strPathFile, blnHasFieldNames
strFile = Dir()
Loop
I used to be a MOS Access 2003. Now everyone is using 2010 but many things have not changed.
When you do a manual import or export, you can save the layout as a specification.
This process can be automated by a macro.
Check out the link below for more details and steps.
http://office.microsoft.com/en-us/access-help/run-a-saved-import-or-export-operation-HA001226020.aspx?CTT=5&origin=HA001226307
As for the other stuff, buttons, modules, etc, please read the on line help / documentation first.
We are here to help but not do the work for you.
J
Okay, I do not know if it is an issue with my computer not being on the current office CU.
http://msdn.microsoft.com/en-us/library/office/ff192475(v=office.14).aspx
Here is a link to how to use the ImportExport Macro. Use to be in the macro section.
I did read that you had to trust the location. So I tried both my location c:\msdn plus the default for the wizards.
Still was not able have it the option come up.
I tried creating a specification to see if one was needed for the option to show, no dice.
However, there is a DoCmd.TransferText and DoCmd.TransferSpreadSheet.
Both will allow you to import.
Create a function. Call the function from a macro (RunCode). Another way is to create a main menu form. Have a button. On the click command, run the code.
Please tell me if you ever get the ImportExportData Macro to show. I think it is a bug. I will need to bring down the latest Cumulative Updates and try again.
I currently use the VB6 CompactDatabase method to compact a .mdb (Access DB) file and have searched high and low for a way to reapply the Share Permissions on Original DB using VB6.
The CompactDatabase code:
Set jro = CreateObject("jro.JetEngine")
If IsObject(jro) Then
jro.CompactDatabase _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sourcePath & _
";Jet OLEDB:Database Password=" & DBPassword, _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & tmpPath & _
";Jet OLEDB:Database Password=" & DBPassword & _
";Jet OLEDB:Encrypt Database=True;Jet OLEDB:Engine Type=5;"
Else
compactDB = "Compact Failed: MDAC not installed correctly - missing JRO.JETENGINE"
End If
This compacts to new DB and sequence is to Delete the original and rename new to old.
Problem with this is that new file does not have original Share Permissions, Currently set to 'Everyone' with 'Full Control'. I have found code on how to set permissions on folders but not directly to files.
Any help would be appreciated.
For anyone who is interested I have found the resolution.
In original code; by using a temp path "C:\temp" to create the new '.mdb' and then copying it over to my Program Files directory; I actually inherited the Permissions of "temp" folder.
I changed the Temp file name to be in same directory as current DB and when process is complete new .mdb has full permissions as with Old.mdb.
Cheers
I have a personal DB app that was initially designed using the mdb format in Access 2007. For security reasons I've converted it to .accdb. All functions converted fine except the change DB password function. This function is done in VBA because the Db has all the tool bars turned off. In mdb format... this works fine
DBPath = [CurrentProject].[FullName]
' Create connection string by using current password.
strOpenPwd = ";pwd=" & OldPswd
' Open database for exclusive access by using current password. To get
' exclusive access, you must set the Options argument to True.
Set dbsDB = OpenDatabase(Name:=DBPath, _
Options:=True, _
ReadOnly:=False, _
Connect:=strOpenPwd)
' Set or change password.
With dbsDB
.NewPassword OldPswd, Pswd2
.Close
End With
Me.DB_Pswd = Pswd2
Set dbsDB = Nothing
I found something from this very forum that comes close for the .accdb but it only works for another .accdb file not the current project....
strAlterPassword = "ALTER DATABASE PASSWORD [" & NwPswd& "] [" & OldPswd & "];"
Set ADO_Cnnct = New adodb.Connection
With ADO_Cnnct
.Mode = adModeShareExclusive
.Provider = "Microsoft.ACE.OLEDB.12.0"
' Use old password to establish connection
.Properties("Jet OLEDB:Database Password") = OldPswd
'name current DB
DBPath = [CurrentProject].[FullName] <- this does not work: get a file already in use error
.Open "Data Source= " & DBPath & ";"
' Execute the SQL statement to change the password.
.Execute (strAlterPassword)
End With
'Clean up objects.
ADO_Cnnct.Close
Set ADO_Cnnct = Nothing
So is there a way to do this in VBA for .accdb files? Basically it would be automating the tool bar function of first Decrypt and the encrypt with a new password. I know the tool bar can do so I know there must be a VBA way to do it.
I found the fix for this or maybe just a work around. By removing the ADO library the first method will work for .Accde format files. It will not work for the .accdb format file, but you don't want to distribute those anyway.