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
Related
I have another issue now that we have decided to move things to a server since the database cannot handle the amount of data being captured.
The short of it is:
I have tried using my existing code via transferspreadsheet method to import data to a tmp file but this is taking to long now with the SQL Server. I would like to stay with doing this programmatically and have been working with the following code. I am not sure if this will work since I can't get past the syntax error. If anyone could help it would be greatly appreciated
code start:
Dim CN As ADODB.Connection
Dim strConn As String
Dim strSQL As String
Dim strXLSource As String
Dim lngRecsaff As Long
strConn = "Driver={SQL Server};Server=logistics.companyname.com;Database=ITM;Trusted_Connection=Yes;"
Set CN = New ADODB.Connection
CN.Open strConn
strXLSource = ("C:\Users\GONZW053\Documents\My Desktop Documents\Monster Database Exports\DTPM - WDW DLR\book1.xlsx;Extended Properties= Excel 12.0")
strSQL = "INSERT INTO [tmpWDWDLR] SELECT * FROM OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0','Data Source = '" & strXLSource & "')"
Debug.Print strSQL
CN.Execute strSQL, lngRecsaff, adExecuteNoRecords
Debug.Print "Records Affected: " & lngRecsaff
CN.close
Set CN = Nothing
I look forward to the help as this site and contributors have been a great source of knowledge. Thanks!
Since the Excel file is not present on the server, you can't use it in OPENROWSET or OPENDATASOURCE commands.
Using Access, however, you can use an insert query to insert data from the Excel file into SQL server:
INSERT INTO [ODBC;Driver={SQL Server};Server=logistics.companyname.com;Database=ITM;Trusted_Connection=Yes;].tmpWDWDLR
SELECT *
FROM [Sheet1$]
IN 'C:\Users\GONZW053\Documents\My Desktop Documents\Monster Database Exports\DTPM - WDW DLR\book1.xlsx'[Excel 12.0 XML;HDR=No;]
(Sheet1$ is the range from the Excel file where you're moving things from)
As far as I know, this approach has relatively little overhead. Of course, moving around large files just takes time.
I have a macro (created with the macro wizard) which runs a number of queries and then outputs a table to excel. The table has more then the 65,000 records limit for exporting formatted tables. How can I export the table without formatting in the macro? Here is the error I receive after I run the macro.
I know you are using access vba to export the records but have you thought about using a datalink to your query from excel and using the access vba to open the excel file and refresh the data table? this will definitely eliminate any issues with max rows and should not have any failure issues due to export size. If you need more info on how to do that let me know and I'll add more info here.
Here is the code requested by Anthony Griggs above. But it is a VBA solution, not a macro solution, so not directly responsive to the question as posted. This was how I worked around the problem and have had this successfully in production for a long time.
Be sure to add the reference to "Microsoft ActiveX Data Objects 2.8 Library" (or current version for you) and also the "Microsoft Excel 12.0 Object Library" (or current version for you) before using this code. The save changes and quit at the end are critical, otherwise it leaves Excel open in the background that you have to kill via task manager.
Dim rs As New ADODB.Recordset
Dim xl As New Excel.Application
Dim xlWb As Excel.Workbook
Dim xlRange As Excel.Range
xl.Visible = False
xl.ScreenUpdating = False
vcurfilename = "MyFilename.XLSX”
Set xlWb = xl.Workbooks.Open(vcurfilename, 0, False, 5, "password", "password")
rs.Open "Select * from qryMyAccessQuery", CurrentProject.Connection, adOpenForwardOnly, adLockReadOnly
Set xlRange = xlWb.Worksheets("MyExcelSheetName").Range("A1").Offset(1, 0)
xlWb.Sheets("MyExcelSheetName ").Range("a2:bq25000").ClearContents
xlRange.Cells.CopyFromRecordset rs
xl.Range("Table1").Sort key1:=xl.Range("Table1[[#All],[MyColumnName]]"), _
order1:=xlAscending, Header:=xlYes
On Error Resume Next
xl.Range("table1").Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
rs.Close
xl.Range("table1").ListObject.HeaderRowRange.Find("MyColumnName1").EntireColumn.NumberFormat = "dd-MMM-yy"
xl.Range("table1").ListObject.HeaderRowRange.Find("MyColumnName2").EntireColumn.NumberFormat = "dd-MMM-yy"
xl.Range("table1").ListObject.HeaderRowRange.Find("MyColumnName3").EntireColumn.NumberFormat = "dd-MMM-yy"
xlWb.Close SaveChanges:=True
xl.Quit
DoEvents
I have a VBscript that's opening up an Access DB, and I need to create a query from one of the tables, then export the table. I need to create the query because I need to format the number columns since in the DB they have like 10 decimal places but when exported only have 2. I need all 10. I don't control this Access DB, instead I download it so asking the source to make this query isn't an option.
The error with the below is: "ADODB.Command: Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another." what am I missing?
Dim oAccess
Set oAccess = CreateObject("Access.Application")
oAccess.OpenCurrentDatabase("dbfile here")
Dim oQry
Set oQry = CreateObject("ADODB.Command")
oQry.ActiveConnection = oAccess.CurrentProject.Connection
oQry.CommandType = 1
oQry.CommandText = "create view qryTable as select * from table"
oQry.Execute
Set oQry = Nothing
Set oAccess = Nothing
Giving .ActiveConnection a string value works for me.
oQry.ActiveConnection = oAccess.CurrentProject.Connection.ConnectionString
Beware if the Provider in that string is "Jet.OLEDB" and you're using 64 bit Windows, you must run the script in 32 bit mode; I used the version of cscript.exe from \Windows\SysWOW64.
However I'm unsure why you open an Access application instance and then create and execute an ADODB.Command. Perhaps that was an attempt to re-use the application's CurrentProject.Connection so that you wouldn't need to create one from scratch.
Whatever the reason was, if you have an Access instance anyway, you don't need the ADODB.Command. Just use the .Execute method from CurrentProject.Connection.
Dim oAccess, sSql
Set oAccess = CreateObject("Access.Application")
oAccess.OpenCurrentDatabase("dbfile here")
sSql = "create view qryTable as select * from [table]"
oAccess.CurrentProject.Connection.Execute sSql
Set oAccess = Nothing
So lets say I have some code like below to pull data from another access file:
Sub ADO_Recordset_OpenTable()
Dim rs As ADODB.Recordset
Dim cn As ADODB.Connection
Dim MyPath As String
MyPath = CurrentProject.Path
Set cn = New ADODB.Connection
cn.Provider = "Microsoft Jet 4.0 OLE DB Provider"
cn.ConnectionString = "Data Source=C:\Users\Justin\Desktop\ExampleFile.mdb"
cn.Open
Set rs = New ADODB.Recordset
rs.Open "Schedule", cn, adOpenDynamic, adLockReadOnly, adCmdTable
' I would like to at this point build a table within the currentdb file
' with the data in the recordset. Either some kind of create table or
' SQL INSERT?? Just trying to learn how to work with the data set
So within the example are my comments. Basically would like to know how to create a table out of the data contained with the recordset. I guess creating a tabledef? But this is DAO right? and I couldn't really use both DAO and ADO together in a routine right?
Thanks
Justin
You can use both ADO and DAO for different objects within the same procedure.
You could create a DAO.TableDef and examine the recordset's Fields collection, creating new TableDef fields matching each rs.Fields(i).Name and rs.Fields(i).Type
Once you have created the table structure (TableDef), you can loop through the recordset rows to build and execute INSERT statements to store the row values in your new table.
But that seems like waaaay too much work to me. I like Raj's SELECT INTO suggestion better. However, since you already know the table name and path to your MDB, I would reach first for DoCmd.TransferDatabase, and leave ADO only for tasks DAO can't do at all or can't do as conveniently as ADO.
Finally, if your primary interest on this one is exploring possibilities, take a look at the recordset's Save method. You could save with adPersistXML, then import the saved XML as a new table in your current db. See Save Method (ADO)
I have done this the ugly way - parse the incoming ADO recordset, build the CREATE TABLE statement and execute it, and then RBAR through the ADO dataset to insert into the local table.
You can also create a passthrough query which you can then use to SELECT * INTO MyNewTable FROM MyPassThroughQuery
You could try ADOX.
I have an excel spreadsheet that contains the primarky-key/id value for a record in a table in an access database.
I would like to export specific data from certain cells in the spreadsheet to certain fields in the corresponding record in the table.
Is this possible, any help would be greatly appreciated.
Many thanks
Noel
You can use ADO with Excel and Access. You can either open an Access recordset and update or add fields (columns) and records one by one, or you can use an SQL statement with IN key word or an internal connection string.
Very roughly:
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\docs\mydb.mdb"
Set cn = CreateObject("ADODB.Connection")
cn.Open strCon
strSQL="SELECT * INTO NewTable FROM [Sheet1$] IN '' " _
& "[Excel 8.0;HDR=YES;IMEX=2;DATABASE=C:\Docs\WB.xls]"
cn.Execute strSQL