Solved

VB to SQL Server

Posted on 2002-04-24
9
196 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to make an ADE file by code? 11 110
IF ELSE Statement in Excel Macro VBA 16 103
Problem to line 23 80
Create a list of Worksheets based on Cell Value 14 30
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

738 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