Solved

VB to SQL Server

Posted on 2002-04-24
9
194 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
  • 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

809 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