curtis591
asked on
Updating A Table
It has been a long time since I have done any VB so and I know you people can get me going faster than I can do this myself.
I have the following code which is opening a table and putting records inside of it. The end goal of my program is to read a spreadsheet and insert all the rows in the spreadsheet into the table. My concern with the following code is that it is opening up the entire table of records and returning them. This isn't a big problem off the start but as they gets to be thousands of records this could be a problem. My question is am I right in thinking I have a problem. If I do how do I fix it so that I am not returning all the records but I can still update the table. There is no need to ever have any records in the recordset or if there was I would have all the records for a specific load which is recalled by a key in the table.
Set RSOpt= New ADODB.Recordset
RSOpt.CursorType = adOpenKeyset
RSOpt.LockType = adLockBatchOptimistic
RSOpt.Open "my_table", "connection_string", , , adCmdTable
RSOpt.addnew
RSOpt(1).Value = 1
RSOpt(2).Value = 4
RSOpt(3).Value = 1
RSOpt(4).Value = 1000
RSOpt.UpdateBatch
RSOpt.Close
I have the following code which is opening a table and putting records inside of it. The end goal of my program is to read a spreadsheet and insert all the rows in the spreadsheet into the table. My concern with the following code is that it is opening up the entire table of records and returning them. This isn't a big problem off the start but as they gets to be thousands of records this could be a problem. My question is am I right in thinking I have a problem. If I do how do I fix it so that I am not returning all the records but I can still update the table. There is no need to ever have any records in the recordset or if there was I would have all the records for a specific load which is recalled by a key in the table.
Set RSOpt= New ADODB.Recordset
RSOpt.CursorType = adOpenKeyset
RSOpt.LockType = adLockBatchOptimistic
RSOpt.Open "my_table", "connection_string", , , adCmdTable
RSOpt.addnew
RSOpt(1).Value = 1
RSOpt(2).Value = 4
RSOpt(3).Value = 1
RSOpt(4).Value = 1000
RSOpt.UpdateBatch
RSOpt.Close
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Alternatively you can use a SQL statement which is going to run faster...
Dim sSQL as string
Dim objConn as ADODB.Connection
'Open the connection using a connection string
sSQL = "INSERT INTO MyTable (Field1,Field2,Field3,Fiel d4) VALUES(1,4,1,1000)"
objConn.Execute sSQL
Dim sSQL as string
Dim objConn as ADODB.Connection
'Open the connection using a connection string
sSQL = "INSERT INTO MyTable (Field1,Field2,Field3,Fiel
objConn.Execute sSQL
alternatively, if you feel that you MUST use a recordset :
RSOpt.Open "Select * from my_table where PK = 0", "connection_string", ,adLockOptimistic
RSOpt.addnew
RSOpt(1).Value = 1
RSOpt(2).Value = 4
RSOpt(3).Value = 1
RSOpt(4).Value = 1000
RSOpt.Update
RSOpt.Close
here PK is the NAME of the FIELD which is the PrimaryKey of the Table (and you would assign a VALUE (indicated here with 0) that IS NOT present in the table. This will create a recordset, with NO records, but will ALL of the fields defined. Be sure to use the proper LOCK parameter (adLockOptimistic).
RSOpt.Open "Select * from my_table where PK = 0", "connection_string", ,adLockOptimistic
RSOpt.addnew
RSOpt(1).Value = 1
RSOpt(2).Value = 4
RSOpt(3).Value = 1
RSOpt(4).Value = 1000
RSOpt.Update
RSOpt.Close
here PK is the NAME of the FIELD which is the PrimaryKey of the Table (and you would assign a VALUE (indicated here with 0) that IS NOT present in the table. This will create a recordset, with NO records, but will ALL of the fields defined. Be sure to use the proper LOCK parameter (adLockOptimistic).
ASKER
My spreadsheet has in the neighboorhood of 200 columns that will be changed and I think the code requires will require fewer changes with field names by referencing them by number rather than field names and I don't have to worry about commas and other characters causing me problems in trying to make the sql string.
dim Conn as ADO.Connection
Conn.ConnectionString = "Connection_String"
Conn.Open
Conn.Execute "INSERT INTO My_Table (FieldName1, FieldName2,FieldName3,Fiel
if the values to be inserted are Variables, then:
Conn.Execute "INSERT INTO My_Table (FieldName1, FieldName2,FieldName3,Fiel
Arthur Wood