Solved

VB to SQL Server

Posted on 2002-04-24
9
197 Views
Last Modified: 2010-05-02
Can any one give me site or code  where I can see  an example to use update , delete , insert into SQL Server via VB
0
Comment
Question by:Richards
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 22

Expert Comment

by:ture
ID: 6965909
Richards,

Try http://www.able-consulting.com

Ture Magnusson
Karlstad, Sweden
0
 
LVL 22

Expert Comment

by:ture
ID: 6965912
...and click on "Technology"...
0
 
LVL 5

Expert Comment

by:raizon
ID: 6965924
Set a reference to Microsoft ActiveX Data Objects Library 2.x

Option Explicit

Dim oConn As ADODB.Connection
Dim oRs As ADODB.RecordSet

Private Sub Form_Load()
  Set oConn = New ADODB.Connection
  With oConn
      .Provider = "SQLOLEDB"
      .ConnectionString = "Server=<serverName>;UID=;Pwd=;Database=<database>;"
      .Open
  End With
  sSQL = Your SQL statment
  oRs.Open sSQL, oConn, adOpenForwardOnly, adLockReadOnly
End Sub


0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 43

Accepted Solution

by:
TimCottee earned 100 total points
ID: 6965933
'Declare objects
Private cnnSQL As ADODB.Connection
Private rstData As ADODB.Recordset

Private Sub Form_Load()
'Initialise connection and open it
  Set cnnSQL = New ADODB.Connection
  cnnSQL.ConnectionString = "Provider=SQLOLEDB.1;Data Source=<MYSERVERNAME>;Initial Catalog=<MYDATABASENAME>;Integrated Security=SSPI;Persist Security Info=False;"
  cnnSQL.Open
End Sub

Private Sub Form_Unload()
'Close and destroy the connection object
  If cnnSQL.State <> adStateClosed Then
    cnnSQL.Close
  End If
  Set cnnSQL = Nothing
End Sub

You can then use the .Execute method of the connection object:

cnnSQL.Execute "Insert Into MyTable Values(100,'Hello','Something')"

To execute standard SQL statements.

You can also use the recordset object to open a recordset:

Set rstData = New ADODB.Recordset
rstData.Open "Select * From MyTable",cnnSQL,adOpenStatic,adLockOptimistic
With rstData
 .MoveFirst
 Do
  Msgbox .Fields("MyField").Value
  .Fields("MyField").Value = "Hello"
  .Update
  Msgbox .Fields("MyField").Value
  .MoveNext
 Loop Until .Eof
End With

To perform select statements and also you can add/update/delete records from this recordset and hence the underlying table(s).
0
 

Author Comment

by:Richards
ID: 6966140
More or less Tim you code works .
Thanks ! How do I select a particular record and delete it . Secondly I want to read data from a text file which is
in comma delimeted and then insert the records into aable . During my every insert of a complete file I need to possible rename the table and create a table and then insert the fresh fields.
recordNo , Empname , DateofJoining
001      , John     , 19/04/2002
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6966181
As for the comma delimited file, use something like this to read the information in and write to the table:

If you have csv data then you should definitely look at using a DTS package in SQL server, or use the BCP utility which will achieve this far quicker than manipulating the data through VB. If you have SQL server 2000 you can create a DTS package and save it as VB code so that you can modify the way it works and use different tables etc.

As for finding a record and deleting it, you can use either

cnnSQL.Execute "Delete From MyTable Where MyKeyField = 'MyValue'"

Or

With rstData
  .MoveFirst
  .Find "MyKeyField = 'MyValue'"
  .Delete
End With

0
 

Author Comment

by:Richards
ID: 6968060
Thanks ! How to read and write it to the table
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7670182
Hi Richards,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept TimCottee's comment(s) as an answer.

Richards, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 1

Expert Comment

by:Computer101
ID: 8092514
Comment from expert accepted as answer

Computer101
E-E Admin
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

631 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