I am using an ADODB.Connection object with "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb;Jet OLEDB:Database Password=test;Persist Security Info=False;".
The speed of db_obj.Open(db_conn_string) is very different.
local: 0.05 sec
over LAN network: 1 - 1.5 sec
After that i am using a select statement.
I testet every line per speedcheck. My only slow down is the open command.
Any ideas how to increase speed over network?
Thanks
Just out of curiousity, why are you so worried about the difference between mere seconds in connection speed?
Is the issue that you are creating these connections and dropping them constantly throughout your code, therefore the extra second or so adds up to several minutes?
If so, your goal might need to be refining your connection usage itself. Instead of constantly dropping the connection, maybe just keep it active.
Also, on a side note, you may want to rethink keeping the password saved in the connection string (assuming this is not just personal use or whatever).
Those things aside, your method of connection is about as good as it gets from a single shot perspective.
There are some other options, such as opening the connection as a recordset, but the speed would not see any significant increase on the opening side.
Dim rs1 As New ADODB.Recordset
Dim sConn As String
Dim sSQL As String
FilePath = "[File Location]"
sConn = "Provider=Microsoft.jet.oledb.4.0;Data source=" & _
FilePath & "\[DatabaseName].[Extension]"
conn.Open sConn
Set conn = CreateObject("ADODB.Connection")
Set rs1 = CreateObject("ADODB.Recordset")
sSQL = "SELECT * FROM Table1"
rs1.CursorLocation = adUseClient
rs1.Open sSQL, conn, adOpenDynamic, adLockPessimistic 'lock depending on user requirements
rs1.MoveFirst
Do While rs1.EOF = False
'Code here
rs1.MoveNext
Loop
'For additional manipulation
rs1.MoveFirst
Do While rs1.EOF = False
'Code here
rs1.MoveNext
Loop
rs1.Close
Set rs1 = Nothing
Set conn = Nothing
To the best of my knowledge, other than that, your only real solution would be to increase the speed of the physical network itself (if plausible).
Related
This is kind of a follow-up post to this question.
I am trying to put data from a .csv into a .mdb (MS Access 2000).
This script works perfectly fine if the DB is stored on my hard drive, but it is on a different drive I access over a network. I have full rights there and I can insert new data sets by hand without any problems.
'There are several other Subs in this .hta-file
'these two are specified along with some other public variables
'in the beginning
Const adOpenStatic = 3
Const adLockOptimistic = 3
Sub CSVImport
connStr = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=S:\folderpath with blanks ß and ,commas\somedatabase.mdb"
'Define object type
Set objConn = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
'Open Connection
objConn.open connStr
objRecordSet.Open "SELECT * FROM SomeTable", _
objConn, adOpenStatic, adLockOptimistic
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("P:\someFile.csv")
Do Until objFile.AtEndOfStream
strVKBL_Stamm = objFile.ReadLine
arrVKBL_Stamm = Split(strVKBL_Stamm, ";")
objRecordSet.AddNew
objRecordSet("ID") = arrVKBL_Stamm(0)
objRecordSet("LastName") = arrVKBL_Stamm(1)
objRecordSet("Date") = CDate(arrVKBL_Stamm(2))
objRecordSet("More") = arrVKBL_Stamm(...)
objRecordSet("andMore") = arrVKBL_Stamm(...)
objRecordSet.Update
Loop
MsgBox "All done"
Set objRecordSet = Nothing
Set objFSO = Nothing
Set objFile = Nothing
Set objConn = Nothing
End Sub
My .csv-File looks like this:
50009900;Doe;01.01.12;foo;bar
I don't get any errors, while the script is running. I checked the strings stored in arrVKBL_Stamm(...) and they all are alright. My last MsgBox pops up, the script stops, nothing happened in my DB.
Any thoughts?
I replied on your first thread but I want to reiterate here that you are reinventing a wheel that works very well as it is. I wouldn't recommend using VBScript for what you are trying to do. You could write this same code in VBA directly inside of Access itself. You don't even need to do that because SAP uses a standard database engine in the back end. You can set up linked tables that pull directly from SAP in to Access. No export\import. It's a live table.
I hate to give you "don't do that" as an answer but that is most definitely the correct answer for you.
So use a linked table directly to SAP or move this code in to Access VBA and run it from there.
This line jumps out at me for being a static recordset adOpenStatic as that won't allow record updates on the DB, only the client side recordset:
objRecordSet.Open "SELECT * FROM SomeTable", objConn, adOpenStatic, adLockOptimistic
A dynamic or another type of cursor is what you would need:
objRecordSet.Open "SELECT * FROM SomeTable", objConn, adOpenDynamic, adLockOptimistic
I've run across this error when attempting to query the database with ADO. From searching around I've found that this probably means there is an issue with the SQL that I am using.
Dim rs As Object
Dim varGetRows As Variant
Dim sqlStr As String
sqlStr = "SELECT qryEmpRewardsDetail.PBRID, qryEmpRewardsDetail.ActualReward, qryEmpRewardsDetail.ProratedActual, qryEmpRewardsDetail.Adjust, qryEmpRewardsDetail.Total, qryEmpRewardsDetail.Prorated FROM qryEmpRewardsDetail WHERE qryEmpRewardsDetail.ReviewYearID=8 AND qryEmpRewardsDetail.EmployeeID=30 AND qryEmpRewardsDetail.TypeID=1;"
Set rs = CreateObject("ADODB.Recordset")
rs.Open sqlStr, CurrentProject.Connection
varGetRows = rs.GetRows()
I've re-written it in a couple of different ways, checked the spelling a hundred times and copied and pasted it into an access query which ran fine.
For the life of me I can't figure out what could be wrong with it. Any ideas on what it could be or some suggestions on how to narrow down the issue?
Thanks!
Does this run?
SELECT * FROM qryEmpRewardsDetail WHERE qryEmpRewardsDetail.ReviewYearID=8 AND qryEmpRewardsDetail.EmployeeID=30 AND qryEmpRewardsDetail.TypeID=1
If it does, then you likely spelt a field wrong, add one by one till happy.
Otherwise, try removing each WHERE condition and test between each.
Define your recordset fully just to be safe:
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
rst.ActiveConnection = CurrentProject.Connection
rst.CursorType = adOpenDynamic
rst.LockType = adLockReadOnly
rst.Open sqlStr
If rst.BOF and rst.EOF Then
'no records returned
End If
Do While Not rst.EOF
'Doing stuff with row
rst.MoveNext
Loop
I use the typical ADO Jet OLE 4.0 connection with VB6. For example:
Set CONN = New ADODB.Connection
With CONN
.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\DATABASE\QA1.mdb;" & _
"Jet OLEDB:Engine Type=4;"
.Properties("Jet OLEDB:Max Locks Per File") = 25000000
End With
And I open the recordset with:
Set RSTX = New ADODB.Recordset
RSTX.CursorLocation = adUseClient
SQL = "SELECT ecc, ecc1, ecc2 from tab1 where ecc='2'"
RSTX.Open SQL, CNT, adOpenStatic, adLockReadOnly, adCmdText
But the query is very very very slow. Possibly I need to add cursor type or completely completly change my strategy. Note: All fields for WHERE clause are indexed.
I've successfully connected to my Access database from excel, and can return the contents of the database in a string Using GetString on my RecordSet. GetString prints all of the contents of the table into a message box as I expect it to(in comments below), but GetRows ignores one of the columns(GCAT in this case), which happens to be the only text field in the database. I am trying to print a particular instance of this field into my excel sheet, but at array position(0,1), where the GCAT field should be, it prints the third item of the record, and not the second as I expect. What am I missing? Does it have something to do with it being a text field? Maybe i'm using the wrong library or database engine? Every other column in the database is returned normally.
Sub Connect()
Dim oConn As ADODB.Connection
Dim oRs As ADODB.Recordset
Dim sConn As String
Dim sSQL As String
Dim arrayString As String
sConn = "Provider='Microsoft.ACE.OLEDB.12.0';Data Source='<path_to_db>'; Persist Security Info='False';"
' Open a connection.
Set oConn = New ADODB.Connection
oConn.ConnectionString = sConn
oConn.Open
' Make a query over the connection.
sSQL = "SELECT ID, GCAT, Min_Years, Max_Years, Contract_Price FROM GCAT"
Set oRs = New ADODB.Recordset
CursorLocation = adUseClient
oRs.Open sSQL, oConn, adOpenStatic, adLockBatchOptimistic, adCmdText
GCATArray = oRs.GetRows()
Sheets("Calculations").Range("D6").Value = GCATArray(0, 1)
'GCATString = oRs.GetString()
'MsgBox GCATString
' Close the connection.
oConn.Close
Set oConn = Nothing
End Sub
This is my first foray in VB so I'm both confused and struggling with the language to being with.
Can't see any obvious faults in your code, have you tried debugging yourself? You can loop the fields in the recordset, and display their names for testing like so:
For i = 0 To oRS.Fields.Count -1
debug.print oRS.Fields(i).Name
Next
That way, you can see whether the field you are looking for is actually there in the first place. Next, you can access the field your're after by doing:
Do While Not oRS.EOF
Debug.Print oRS!GCAT
'Exit Do 'if you want to display only the first, break out of the loop here
oRS.MoveNext
Loop
You don't need the GetRows() in this case, that should give you a performance boost too (very noticible on larger recordsets).
two things:
first: I have googled everywhere, including stackoverflow. Just about questions regarding sql vs vb6 are about connection string. I have this down pat. Any reference to mysql queries are for the query itself - but not in tangent with vb6 or any language
second: I am very proficient in PHP/MySQL so that aspect of help I am not seeking.
what I am stuck on, is how vb6 handles sql queries a little (lot) different than php. So once I get connected, how to I tell vb6 to look up a field.
php version
$sql = "SELECT * FROM table field = data where something = that";
$query = mysql_query($sql) or die("bad query: <br>$sql<br>".mysql_error());
then either use a fetch array or work with this.
how is this accomplished in vb6?
I saw some source referring to rdoQry. Can someone shed some light on this with example code? I don ont need the connection part. have that set.
my connection is this:
Dim cnMySql As New rdoConnection
cnMySql.CursorDriver = rdUseOdbc
cnMySql.Connect = "uid=root;pwd=root;" _
& "server=127.0.0.1;" _
& "driver={MySQL ODBC 3.51 Driver};" _
& "database=mydatabase;dsn=;"
cnMySql.EstablishConnection
works perfect.
ADO is the successor to RDO. I use code similar to this to query MySQL from Visual Basic using ADO.
Dim conn As New ADODB.Connection
conn.Open "connection string"
Dim cmd As New ADODB.Command
With cmd
.ActiveConnection = conConnection
.CommandText = "SELECT fields FROM table WHERE condition = ?"
.CommandType = adCmdText
End With
Dim param As New ADODB.Parameter
Set param = cmd.CreateParameter("condition", adVarChar, adParamInput, 5, "value")
cmd.Parameters.Append p
Dim rs As New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open cmd, , adOpenStatic, adLockOptimistic
Dim temp
Do While Not rs.EOF
temp = rs("field")
rs.MoveNext
Loop
rs.Close
conn.Close
Typically, with VB6 (gosh, are people still using this??) you would connect to a database using ADO. ADO is a common database class which allows you to use the same syntax for any database.
The connection code you've provided is using RDO, which is a predecessor to ADO (and since VB6/ADO is pretty old now, that means RDO is historic). For your purposes, the two should work fairly similarly, but I'd suggest switching to ADO now if you have a chance, before you've written too much code.
This thread seems to be pointing someone else in the right direction in writing the connection code: http://www.vbforums.com/showthread.php?t=654819
Once you've got a connection, you need to run your queries. The process for this should make sense if you're used to querying from PHP; it's basically the same process, although you typically need to mess around with configuring a few more options than with PHP. It would look something like this:
Set rs = New ADODB.Recordset
rs.ActiveConnection = adoconn
rs.CursorLocation = adUseClient
rs.CursorType = adOpenDynamic
rs.LockType = adLockOptimistic
rs.Open "SELECT blah blah blah"
While Not rs.EOF
Text1.Text = rs("Name")
rs.MoveNext
Wend
rs.Close
Hope that helps.