Excel workbook as database, insert update issue - vba

There is a excel workbook which is used as a database kept on shared folder. The users have other workbook which has the userform which allows the insert and update into the database.
I am using connection.Execute method for insert and Update and use recordsaffected for my confirmation.
Although the recordsaffected gives the correct, sometimes insert or update never happens.
What can I do to fix this?
Set oCn = CreateObject("ADODB.Connection")
oCn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & shtSetting.Range("filePath").Value & ";" & _
"Extended Properties=Excel 12.0;"
strSQL = "Insert Into [Sheet1$] (" & _
"sr_no, process,employee_id,employee_name) Values ( " & _
"'" & sr_no & "','" & process & "','" & emp_id & "','" & emp_name & "')"
oCn.Execute strSQL, r_status
oCn.Close
Set oCn = Nothing

Unless there's a strong reason to store the data in an Excel spreadsheet, though, I'd consider offloading the database management portion of it to an Access database (even if you don't have Access, you can still create an mdb/accdb file). If nothing else, that's going to give you access to indexing, which can improve performance tremendously. You can still mirror the Access data in Excel by using data connections (or other techniques).
If you are stuck with doing everything in Excel, one (potentially slow) solution is to query the sheet once you've performed your update, to confirm that it has made the change that you expect. If you're doing an insert, you may be able to get away with just counting the records, but it's definitely safer to look for the exact value you just inserted.
Also: if the users are making multiple updates, you might want to consider keeping the connection open until the form closes. (Though I don't recall if this causes locking issues with an Excel backend or not. It would generally improve performance in Access.)

Related

Increase Ms Access Insert Performance

I am using MS Access 2010, split in front end / back end; on a network drive (WAN) with 16+ table with one table of users (1.3 Million) which is mostly used for user information and is not insert heavy and few other tables, which will receive upto 2000+ inserts daily.
I have been able to optimize most of the read/select queries. Although 1 chunk of my code looks as below. This can be used for upto 2000 iterations daily.
Do Until rec.EOF
Dim vSomeId As Integer
vSomeId = rec!SomeId
'StrSQL = StrSQL & "INSERT INTO TransportationDetails ( TransportationId, SomeId)" & _
'"VALUES(" & vTransportationId & ", " & vSomeId & ");"
StrSQL = "INSERT INTO TransportationDetails ( TransportationId, SomeId)" & _
"VALUES(" & vTransportationId & ", " & vSomeId & ");"
DoCmd.SetWarnings False
DoCmd.RunSQL (StrSQL)
DoCmd.SetWarnings True
rec.Edit
rec!SomeBoolean = rec!SomeOtherBoolean
rec.Update
rec.MoveNext
Loop
My objective here, is to reduce the number of calls to the db to insert all the values. and MS ACCESS does NOT support having more than 1 query in a statement, as I tried in the commented part of the code. I also think the recordset upate method is a lot time consuming, and if any one can suggest a better way to update the recordset.
Is there any way I can trick Access to insert & Update in less hits to db through SQL Queries, or any other access feature. Or optimize in anyway, It can take up to 30 mins some time. Decreasing it to At least 2 - 5 mins will be appropriate.
P.S.
I can not switch to SQL Server, It is JUST NOT POSSIBLE. I am aware it can be done in way more optimal way through sql server and Access shouldn't be used for WAN, but I don't have that option.
Solution:
I went with Andre's and Jorge's solution. The time decreased by 17 times. Although Albert's Answer is correct too as I found my main issue was with the sql statements in a loop. Changing the edits in the recordset to sql didnt impact much on the time factor.
I should point out that in the case of inserting rows, you will find FAR better performance by using a recordset. A SQL “action” query will ONLY perform better if you operating on a set of data. The instant you are inserting rows, then you don’t have a “set” insert, and using a DAO recordset will result in MUCH better performance (a factor of 10 to 100 times better).
If you have now
S = "SELECT SomeId, SomeBoolean, SomeOtherBoolean " & _
"FROM recTable WHERE someCriteria"
Set rec = DB.OpenRecordset(S)
change your statements into
"INSERT INTO TransportationDetails (TransportationId, SomeId) " & _
"SELECT " & vTransportationId & ", SomeId " & _
"FROM recTable WHERE someCriteria"
and
"UPDATE recTable SET SomeBoolean = SomeOtherBoolean WHERE someCriteria"
For performance, avoid looping over Recordsets where possible. Use SQL statements that operate on whole sets instead.
I recently ran into a problem where I have to import 200,000 records into 12 Access tables every 6 hours. This took too long as I was inserting each record one at a time.
I picked up a tip from a colleague who suggested using Linked Tables.
So I set up a linked table in my Access Database which was linked to a semi-colon delimited text file.
My program then created that semi-colon delimited text file every 6 hours.
You then select from the linked table into the table needed and that table is created.
This made my process immensely faster and I would definitely suggest it as an option.

MS Access: Method to process SQL without using RecordSource

I would like to be able to find the last record in a table easily for SQL INSERT INTO table statement. I was hoping there would be a MS Access object or function which could read a SQL statement without requerying the whole context just to find specific counts or records. As I have been programming the only code I know that reads SQL is a recordset, is there a dummy copy or source you could just read without repointing the record to another? Otherwise, I need a way to access the table in VBA and count all records with a method. If this is not code yet is should be, this would make it so easy to get around code dialects, other methods (unless you need to use form objects) if you know SQL.
I have tried several things, such as that cast a tbl variable but there would be a type mismatch.
This needs a last statement so I get the new record... I need to know how to get the last record in the table with a count.
CurrentDb.Execute "INSERT INTO FormsHelpTable ([ID], [HelpTitle], [Comment]) VALUES " & _
"(" & (lastRecTbl + 1) & ", '" & Me.Text53 & "', '" & Me.Comment & "')", dbFailOnError
Me.RecordSource = "SELECT * FROM FormsHelpTable"
Me.Requery
This worked to find the last record:
http://www.minnesotaithub.com/2013/08/count-records-vba-microsoft-access-2010/
Great code too.

Why Excel VBA can't query tables with the # symbol?

I'm trying to query a table, call it history#integration.
When I query the table, with this type of query:
select * from history#integration where id=5
and get the expected output.
With excel I connect to the database in this way:
cn.Open ( _
"User ID=" & userID & _
";Password=" & password & _
";Data Source=" & datasource & _
";Provider=MSDAORA.1")
but I get a runtime error of data type is not supported. I've verified using the exact same connection I can query the database with other "standard" tables like select * from history. Any thought on how I can get the "correct" type.
In Oracle (assuming this is what you're using, and it would be helpful to state that in your question) # is typically used to represent a database link: likely it's not part of the table name, but the table "history" is actually in a different database linked to by a DB link named "integration".
Not all datatypes are selectable over an Oracle DB link (LOB types for example)
# is a reserved character in SQL. Try surrounding the table name with brackets like: [history#integration].

Visual Basic.Net Insert multiple rows into a table

I am a relative newbie and trying to insert multiple rows (and data from textboxes) from one table into another and am stuck.
This SQL identifies the data to be inserted into the table
strsql = "SELECT '" & textbox1.text & "', '" & Textbox2.text & "', "
strsql = strsql & " a.TaskNum, a.StartDay, a.NumofDays FROM VETTimeLines as a"
strsql = strsql & " ORDER BY a.StartDay"
I started out along the lines of -> Insert into StudentProgram Values() code shown above, after 3 days of trying I now look forward to your advice.
Many thanks in anticipation
Peter
Inserting data using C# can be performed in variety of ways such as using Entity Framework or using ADO.NET, as you have chosen to do in this case.
Using ADO.NET you either write the insert as you did or by using DataAdapter approach. DataAdapter is capable of creating the SQL code for you, amongst other things. For example see:SQL DataAdapter. It is a good idea to not build a SQL string as you did because of sql injection threat as #Joel Coehoorn indicates in his comment above.
One way to overcome this is by using Parameters as shown in the above link. If you decide to provide the SQL for insert yourself with parameters, here is a good example:StackOverFlow-Insert using ADO.
The idea behind all of the above code is to create a connection object, create a parameter object, create a command object, open the database connection, execute the command and close the connection.
Try one of the above approaches and let's know your specific issue if any arises.

Query SQL Server 2005 Database Table From Excel Spreadsheet

Scenario
I have a database made up of 4 columns and 6 million rows.
I want to be able to be able to use Excel to query this data in the same way in which I can query it using the Sql Server Management Studio.
I've had a google around and read a few articles but in all honesty I dont really know enough about the implementation to really know what to look for in a tutorial.
Question
Could someone please explain to me the way in which this works and how I might go about implementing what seems like a fairly simple idea?
EDIT:
So for example -
I have a few simple queries that I want to run on this data.
How do I go about setting these up?
Is the idea that all of the data is first loaded into the excel sheet (or at least linked to the excel sheet so that the user can select the data to view on the fly?) - I cannot physically load in 6 million rows as Excel cannot take that level of data.....
Definitely don't pull all the data into Excel, that will bring it to its knees.
Normally, when I need an Excel (VBA) workbook to retrieve some data from a database, I will just have Excel connect to the database with the appropriate connection string, pass the SQL query string to the database, and retrieve the returned records. Below is some sample VBA code that retrieves some information from an MS Access database.
txtQueryString = "SELECT * "
& "FROM myDataTable " _
& "WHERE fld1 = '" & myCriteria & "'"
txtConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & dbPathString & ";" _
& "Persist Security Info=False"
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
rst.Open txtQueryString, txtConnectString, adOpenKeyset, adLockReadOnly, adCmdText
myData1 = rst.Fields("fld1").Value
myData2 = rst.Fields("fld2").Value
.
.
.
rst.Close
Set rst = Nothing
The connection string to SQL Server will be different. I use this website for all my connection string needs.