Solved

Updating A Table

Posted on 2002-07-19
5
138 Views
Last Modified: 2010-05-02
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

0
Comment
Question by:curtis591
5 Comments
 
LVL 14

Accepted Solution

by:
ajexpert earned 100 total points
ID: 7165200
Hi,
  Well you can write the select statement in RSOpt.Open


dim strSql as string
strSql = "Select * from my_table"

Modify strSql so that select statement should return 0 records

ie if you are using SQL Server then

strSql = "select top 0 from my_table"

This returns 0 records but still opens the recordset



RSOpt.Open strSql, "connection_string", , , adCmdTable

RSOpt.addnew
RSOpt(1).Value = 1
RSOpt(2).Value = 4
RSOpt(3).Value = 1
RSOpt(4).Value = 1000
RSOpt.UpdateBatch

0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 7165239
If you are ONLY inserting new records, then the MUCH better approach is to use the EXECUTE method of the connection object, and NOT OPEN a recordset at all:

dim Conn as ADO.Connection
Conn.ConnectionString = "Connection_String"
Conn.Open
Conn.Execute "INSERT INTO My_Table (FieldName1, FieldName2,FieldName3,FieldName4) VALUES(1,4,1,1000)"


if the values to be inserted are Variables, then:

Conn.Execute "INSERT INTO My_Table (FieldName1, FieldName2,FieldName3,FieldName4) VALUES(" & Value1 & "," & Value2 & "," & Value3 & "," & Value4 & ")"

Arthur Wood
0
 
LVL 4

Expert Comment

by:gencross
ID: 7165242
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,Field4) VALUES(1,4,1,1000)"

objConn.Execute sSQL

0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 7165252
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).
0
 
LVL 6

Author Comment

by:curtis591
ID: 7165256
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.
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Microsoft Access combo box help 2 47
Notepad++ how to remove delimiter : from beggning of the line? 3 106
Add a task in Outlook from access 11 40
VB6 ListBox Question 4 44
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question