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.
Related
I have a little problem with downloading data from Oracle DB and moving it to the MySQL table.
On Oracle i have only a read privilege. The only task I could execute is Select.
On MySQL i have select/insert/update privilege. Also, there's no problem to change anything in mysql structure - i also have access to mysql root.
Each week I have to download huge amount of data from Oracle and move it to MySQL (for further processing). The best (and the only) solution in my case is to use MS Access.
My vba code snippet looks like (code is usually put into a loop, where I perform some inserts, each for different business site. Executing it in one query sometimes meet no end).
Public Sub DownloadData()
On Error GoTo ErrorTrans
DoCmd.SetWarnings False
Dim strWhere1 As String, strWhere2 As String, strQueryName As String
Dim qdDaneObrRabE As QueryDef, qdDaneObrRabTmp As QueryDef
Dim cnADO As ADODB.Connection
DoCmd.OpenQuery ("qryDaneObrRabCzysc") 'Calling truncate data procedure on mysql table
Set cnADO = CurrentProject.Connection
cnADO.CommandTimeout = 300 '5 minut
Set qdDaneObrRabE = CurrentDb.QueryDefs("qryDaneObrRabE")
strQueryName = "tmpObroty_" & GenerateHash(8)
Set qdDaneObrRabTmp = CurrentDb.CreateQueryDef(strQueryName, "select * from table2;") 'this select is just dummy statement, replaced some lines below
qdDaneObrRabTmp.Connect = qdDaneObrRabE.Connect
qdDaneObrRabTmp.SQL = Replace(qdDaneObrRabE.SQL, "{WHERE1}", strWhere1)
qdDaneObrRabTmp.SQL = Replace(qdDaneObrRabTmp.SQL, "{WHERE2}", strWhere2)
cnADO.Execute "INSERT INTO mysqltable SELECT * FROM " & strQueryName & ";"
Call QueryDefsCleanUp("tmpObroty_")
Call MsgBox("Success")
Set cnADO = Nothing
Exit Sub
ErrorTrans:
Call ActivityLog(Environ("USERNAME"), Now, "DownloadData", True, Err.Number, Err.Description)
End Sub
Please let me know if executing these types of inserts via ADODB is good.
I also tested DAO but i don't see difference.
All of used tables/querydefs are linked tables or pass-through querydefs.
Afternoon all,
I have a simple problem that has me stumped. I am trying to change the connection strings for my pass-though queries. My issue is, it seems to work. The connection strings do get updated to the new connection string, however, when I run it, it is clearly using the previous connection string.
Code:
Sub UpdatePassthroughConnecStrings()
Dim qdf As QueryDef
For Each qdf In CurrentDb.QueryDefs
If qdf.Type = dbQSQLPassThrough Then
qdf.Connect = ConnectString
End If
Next qdf
End Sub
Function ConnectString() As String
Dim strYear As String
strYear = "LIMS" & Form_frm_Main.Year
ConnectString = "ODBC;DSN=LIMS;UID=" & strYear & ";PWD=1234;"
End Function
Basically, I am updated the UID portion so I can connect to the correct Oracle db tablespace (it's split by year). When I check the connection strings after the update it is correctly updated but just doesn't connect correctly. Interestingly, this code works in Excel just fine. Any help would be appreciated!
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 am using the following code in Access to try to open a recordset:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("fieldHistory")
I consistently get the error "Too few parameters."
fieldHistory is a query with the SQL code as follows:
SELECT Date, User, Type
FROM Inventory
WHERE ((Inventory.Type) In ("Insert","EditTo"));
I have looked into this some - I have found that if I use the code db.OpenRecordset("Select * from Inventory") I do not get an error.
Also, I found this forum which seemed to be on to something but I couldn't understand how to create the appropriate querydef objects to create my query.
http://access.mvps.org/access/queries/qry0013.htm
Thank you very much!
Date, User, and Type are all special words or reserved words in MS Access. DO NOT use them for your field names.
You may have some success by enclosing each of them in brackets but I highly recommend you change the field names instead.
Create a new database.
Open the new database and make sure you have DAO included in the references.
Insert a new standard module, making sure to include Option Explicit in the Declarations section.
Then paste in this code and run it.
Public Sub CreateTableZack()
Dim strSql As String
strSql = "CREATE TABLE tblZack (" & vbNewLine & _
vbTab & "id COUNTER CONSTRAINT pkey PRIMARY KEY," & vbNewLine & _
vbTab & "foo_text TEXT(255)," & vbNewLine & _
vbTab & "date_assigned DATETIME);"
Debug.Print strSql
CurrentProject.Connection.Execute strSql
End Sub
Assuming the table is created successfully, create qryZack with this as its SQL:
SELECT *
FROM tblZack;
Then try your code to open a DAO recordset based on qryZack.
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("qryZack")
Does it work?
If not, your Access installation may be hosed ... you may need to repair or reinstall it. You could also try this on another machine which has Access available, if you can.
If it works with the new database, but not your old one, the old one may be corrupted. Make a backup copy first, then try Compact & Repair.
Another issue is your field names. Date, User and Type are all Access reserved words. See Problem names and reserved words in Access. I can't say those names are causing problems here, but using reserved names for database objects (tables, fields, queries, etc.) can have dramatic consequences ... like it confuses the crap out of Access. So I fastidiously avoid them.
Edit: You should also check the references in your old database. Missing/broken references also confuse the crap out of Access.
Set up an Access Project to be opened via Citrix. However, there is some VBA code that prevents it from working:
Dim rs As ADODB.Recordset
Set rs = CurrentProject.Connection.Execute("Query")
The .Connection bit is highlighted. This works when it is not opened via Citrix though (i.e. just on users desktop).
Problem was due to the Citrix server not having the latest Jet Service Pack for Access.
I don't have a lot of experience with ADO but I'm thinking there's a references problem of some sort. Or that you are referencing a newer version of ADO on your system than is avaialble on the Citrix box. Run the following code and post back the results.
Sub ViewReferenceDetails()
Dim ref As Reference
For Each ref In Access.References
Debug.Print ref.Name & " - " & ref.Major & "." & ref.Minor & " - " & ref.FullPath
Next ref
End Sub
Also when you state Access project do you mean an ADP against SQL Server or an MDB/ACCDB against an Access data file?
Your code looks wrong to me. Should the rs Object not be a Recordset?
i.e.
Dim rs As ADODB.Recordset
'Instead of
Dim rs As ADODB.Connection
The code should not run at all - you should receive a Type Mismatch error.