I need data by array
Target file is on the network and data value added in a 10 sec
Need to connect to this file (.xls) and make a real time chart
Now the problem is how do I use ADODB for this opened file
2 case happen:
1st:
dim path as string, DB as string
dim cnt as ADODB.Connection
OLEDB = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & path & DB & ";
extended properties=""excel 12.0 xmlHDR=YES"";"
Set cnt = New ADODB.Connection
cnt.Open OLEDB
Then i got "cant find ISAM installation" error
2nd:
dim path as string, DB as string
dim cnt as ADODB.Connection
OLEDB = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & path & DB &";
extended properties=""excel 8.0;HDR=YES;IMEX=1;';"""
Set cnt = New ADODB.Connection
cnt.Open OLEDB
Then I got 3706 runtime error, cant find supporter
So what is wrong with me?
This is how I usually get it running:
DbConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & path & ";" & _
"Extended Properties=""Excel 12.0; HDR=YES"";"
Where path is the full file path + file name.
You can find more Informations on Connection strings here
Worksheet name and Range then go in the SQL Statement. For example:
sqlWhere = sheetName & "$A1:$Z1000"
DbRecSet.Open "SELECT * FROM [" & sqlWhere & "]", DbConn, adOpenStatic
Edit:
If i understand you correctly,
you have a single cell that changes value every 10 seconds and you want to get that value via ADODB.
First of all, in this case, you can scrap HDR=YES from the connection string.
let's say that cell is A1
sqlWhere = sheetName & "$A1"
DbRecSet.Open "SELECT * FROM [" & sqlWhere & "]", DbConn, adOpenStatic
returnValue = DbRecSet.Fields(0)
path = test_fdr & "" & today_total & ".xls"
OLEDB = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & path &
";Extended Properties=""Excel 8.0;HDR=YES"";"
Set cnt = New ADODB.Connection
cnt.Open OLEDB
Rcdset.Open Source:="[" & TestSheetName & "$]", _
ActiveConnection:=cnt, _
CursorType:=adOpenStatic, _
LockType:=adLockReadOnly, _
Options:=adCmdTable
With Sheets(3).QueryTables.Add(Connection:=Rcdset, Destination:=Target_rng)
Related
I'm trying to query a large named range within Excel
The following connection works well but is very slow when the range contains thousands of rows. Moreover, it seems that there is a limit on the number of records.
strCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strFile & ";" & _
"Extended Properties=""Excel 8.0;"""
I'm trying to use a different connection but then, VB6 crashes!
myRangeName = "namedRangeWorksheet"
xlapp.Names.Add Name:=myRangeName, RefersTo:=TrueUsedRange
'**********************************
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
strFile = oWB.FullName
Set strCon = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
strCon.Open "Provider = Microsoft.ACE.OLEDB.12.0;" & _
"Data Source = " & strFile & ";" & _
"Extended Properties = ""Excel 12.0 Xml;HDR=YES;IMEX=1"";"
strSQL = "SELECT * from namedRangeWorksheet" ''Named range
rs.Open strSQL, strCon, adOpenKeyset, adLockOptimistic
Microsoft.ACE.OLEDB.12.0 works well on my computer when applied to Access database
I retrieve data from SQL Server and store it on a sheet in Excel and then I run an ADO UPDATE query within Excel to update data on a different sheet.
I have not come across this error during developing yet users are reporting they see this error frequently:
Note that the file is located on a network drive, but even after copying the file to a different location, it is still producing said error.
Code:
Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command
Dim cnn As New ADODB.Connection
Application.ScreenUpdating = False
Application.EnableEvents = False
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & ThisWorkbook.FullName & "';" & _
"Extended Properties=""Excel 12.0;HDR=YES;"";"
Set rs = GetOverview
shUpdateSLSheet.UsedRange.clear
WriteHeadersToSheet rs, shUpdateSLSheet.Name, 1
shUpdateSLSheet.Range("A2").CopyFromRecordset rs
If (rs.EOF And rs.BOF) Then GoTo NoData
cmd.ActiveConnection = cnn
cmd.CommandType = adCmdText
cmd.CommandText = "UPDATE [SL$] INNER JOIN [UpdateSLSheet$] " & _
"ON ([SL$].ID = [UpdateSLSheet$].ID) " & _
"SET [SL$].[CS_A] = [UpdateSLSheet$].[CS_A]" & _
", [SL$].[CS_B] = [UpdateSLSheet$].[CS_B]" & _
", [SL$].[CS_C] = [UpdateSLSheet$].[CS_C]" & _
", [SL$].[CS_D] = [UpdateSLSheet$].[CS_D]" & _
", [SL$].[CS_E] = [UpdateSLSheet$].[CS_E]" & _
", [SL$].[CS_F] = [UpdateSLSheet$].[CS_F]" & _
", [SL$].[Solved By SR] = [UpdateSLSheet$].[SolvedBySR]" & _
", [SL$].[Comments] = [UpdateSLSheet$].[Comments]"
cmd.Execute
Application.EnableEvents = True
Application.ScreenUpdating = True
Set cmd = Nothing
Set rs = Nothing
Set cnn = Nothing
As soon as cmd.Execute gets executed the error comes up.
EDIT: I have no clue why this is happening. I'm going for an iterative approach instead of using ADO in this case.
you have to use the open method for the record set and use the lock type
so the data base actual locked when some one is adding and when this person finish adding the data base will save changes and then another person can start adding
A bit late, but I had the same issue and found a solution: I tried using the OLD driver in the connection string, and it worked like a charm, even if the workbook is an XLSM !
Hope it helps someone.
sConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & wbName
sConn2 = ";Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
On Error GoTo hell
Set con = New ADODB.Connection
'con.Open sConn & sConn2
con.Open "Driver={Microsoft Excel Driver (*.xls)};" & _
"DriverId=790;" & _
"Dbq=" & wbName & ";" & _
"DefaultDir=" & wbname & ";ReadOnly=False;"
I am using ADO model to gather data from various closes workbooks. This is working well.
I now want to put this data in another closed workbook, but I would like to be able to delete a sheet content before.
How can I delete a worksheet content without opening the workbook using VBA ?
How can I transfer a record set to a closed wb ? / Copy one table to another using ADO ?
EDIT :
I was able to insert some data from one workbook to another one in a new sheet but I can't get to output data in an existing worksheet.
When I try the INSERT INTO statement, an error is raised. Update impossible, database or object readonly.
Here is the code :
Sub tranfert()
Dim ExcelCn As ADODB.Connection
Dim ExcelRst As ADODB.Recordset
Dim Cn As New ADODB.Connection
Dim Rst As New ADODB.Recordset
Dim maBase As String, maFeuille As String
Dim maTable As String, NomClasseur As String
Dim nbEnr As Long
maBase = "C:\Users\Lichar\Documents\Base.xlsx"
maTable = "[table$]"
NomClasseur = "C:\Users\Lichar\Documents\Target.xlsx"
maFeuille = "Sheet2"
'Connection to base file
Cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & maBase & ";" & _
"Extended Properties=""Excel 12.0;HDR=NO;"""
'Requète dans la table Access
Rst.Open "SELECT * FROM " & maTable, Cn
'Connection to target file
Set ExcelCn = New ADODB.Connection
ExcelCn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & NomClasseur & ";" & _
"Extended Properties=""Excel 12.0;HDR=NO;"""
'-----------------------------------------
'Create a new sheet and output data
Cn.Execute "SELECT * INTO [Excel 12.0;" & _
"Database=" & NomClasseur & "].[" & maFeuille & "] FROM " & maTable, nbEnr
'-----------------------------------------
'Trying to ouput data in existing sheet
'Cn.Execute "INSERT INTO [sheet$] IN '' [Excel 12.0;" & _
' "Database='" & NomClasseur & "'] SELECT * FROM " & maTable, nbEnr
Rst.Close
Cn.Close
Set ExcelRst = Nothing
Set ExcelCn = Nothing
**EDIT 2 **
I've found a partial solution using INSERT INTO. Here is a working code that takes data from source.xlsx in the table sheet and output it (or append) in target.xlsx in the sheet sheet :
Sub SQLQUERY()
Dim Cn As ADODB.Connection
Dim QUERY_SQL As String
Dim Rst As ADODB.Recordset
Dim ExcelCn As ADODB.Connection
Dim ExcelRst As ADODB.Recordset
SourcePath = "C:\Users\BVR\Documents\Source.xlsx"
TargetPath = "C:\Users\BVR\Documents\Target.xlsx"
CHAINE_HDR = "[Excel 12.0 MACRO;Provider=Microsoft.ACE.OLEDB.12.0;Mode=Read;Extended Properties='HDR=YES;'] "
Set Cn = New ADODB.Connection
STRCONNECTION = _
"Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source='" & SourcePath & "';" & _
"Mode=Read;" & _
"Extended Properties=""Excel 12.0 Macro;"";"
QUERY_SQL = _
"SELECT * FROM [table$] "
Cn.Open STRCONNECTION
Set ExcelCn = New ADODB.Connection
ExcelCn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & TargetPath & ";" & _
"Extended Properties=""Excel 12.0;HDR=NO;"""
Cn.Execute "INSERT INTO [sheet$] IN '" & TargetPath & "' 'Excel 12.0;' " & QUERY_SQL
'--- Fermeture connexion ---
Cn.Close
End Sub
I've noticed 2 problems. First If one of my field name contains a "." in it, the code will generate an error stating that INSERT INTO contains an unknown field name. This is problematic.
Second I cannot select only the columns I want. If I "SELECT [F1], [F2] ..." an error will raise stating that there is a circular reference. (I can however select the columns I want using field names)
I am trying to run a query to search for data in Microsoft Access and am running into a bit of a problem. The error occurs on the line
rec.Open sqlQuery, conn
and reads "Run-time error '-2147217900 (80040e14)': Automation error"
Sub query21st()
' create file path reference string for 21st data
dbPath = database_path()
' create connection string
Dim connString As String
connString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & dbPath & ";" & _
"Persist Security Info=False;"
'create connection with connection string from above
Dim conn As New ADODB.Connection
conn.Open connString
' create sql command string
Dim lookupFields As String
lookupFields = "cert.[avgle]"
Dim sqlQuery As String
sqlQuery = "select " & lookupFields & " from [Certificates] as cert " & _
"where [DOB] =" & [dob_21st] & " AND [CertificateDate] =" & [certdate_21st] & " AND [MPI1] =" & [mm] & ")"
' create recordset and get data from connection
Dim rec As New ADODB.Recordset
rec.Open sqlQuery, conn
[returnRange_21st].ClearContents
[returnRange_21st].Cells(1).CopyFromRecordset rec
' close recordset
rec.Close
'close connection
conn.Close
End Sub
The "database_path" function contains delicate info that cannot be posted but I guarantee it works just fine. Any help would be greatly appreciated.
I am working with the Microsoft OLE DB Provider for Jet to execute queries on spreadsheets in Excel using VBA. Is there a way to execute the following code on an unsaved workbook?
For example, ActiveWorkbook.FullName returns "Book1" if the workbook has never been saved. In that case the Data Source will assume the path is the active directory, and error out because the file was never saved.
Is there any way to use the Excel temporary file as the Data Source for Jet? I would like to test this but I don't even know how to return the Path and Name for the Excel temporary file.
Public Sub LocalJetQuery()
Dim objStartingRange As Range
Dim objConnection As New ADODB.Connection
Dim objRecordset As New ADODB.Recordset
Dim strDSN As String
Dim strSQL As String
Set objStartingRange = Application.Selection
If CLng(Application.Version) >= 12 Then
strDSN = "Provider=Microsoft.ACE.OLEDB.12.0;" _
& "Data Source=" & objStartingRange.Worksheet.Parent.FullName & ";" _
& "Extended Properties=""Excel 12.0 Xml;HDR=No;IMEX=1"";"
Else
strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & objStartingRange.Worksheet.Parent.FullName & ";" _
& "Extended Properties=""Excel 8.0;HDR=No;IMEX=1"";"
End If
strSQL = "SELECT * FROM [" & objStartingRange.Worksheet.Name & "$];"
objConnection.Open strDSN
objRecordset.Open strSQL, objConnection
Application.Workbooks.Add(xlWBATWorksheet).Sheets(1).Cells(1, 1).CopyFromRecordset objRecordset
End Sub
Thanks!
No. Just like David Fenton says in the comments.