Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB to SQL Server

Posted on 2002-04-24
9
Medium Priority
?
200 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 43

Accepted Solution

by:
TimCottee earned 400 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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

722 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