Solved

VB to SQL Server

Posted on 2002-04-24
9
190 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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 …
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…
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…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now