473,395 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Connect to SQL DB and edit the data

Hello,

Although i write programs in VB for a while now, i've allways used the
datacontrol and MS-Access DBs for data storage and never (and i mean never)
used SQL.

I lookt in many sample codes, red some books but i still can't connect to a
SQL server and edit the data.

Is there someone who is willing to take a little time for me and explane to
me how it works and maby give me some sample code (very, very basic) on how
to connect to a SQL DB and store, edit and remove records from it.

Preferably, don't use the standard VB datacontrol

I want to learn it so much, but all i do doesn't seem to work.

Many thanks in advance.

Kind regards,

Tino Wintershoven

The Netherlands

Jul 17 '05 #1
5 8780
T. Wintershoven wrote:
Hello,

Although i write programs in VB for a while now, i've allways used the
datacontrol and MS-Access DBs for data storage and never (and i mean
never) used SQL.

I lookt in many sample codes, red some books but i still can't
connect to a SQL server and edit the data.

Is there someone who is willing to take a little time for me and
explane to me how it works and maby give me some sample code (very,
very basic) on how to connect to a SQL DB and store, edit and remove
records from it.

Preferably, don't use the standard VB datacontrol

I want to learn it so much, but all i do doesn't seem to work.

Many thanks in advance.

Kind regards,

Tino Wintershoven

The Netherlands


Tino,

Here's lesson one...

'References: Microsoft ActiveX Data Objects 2.1 Library (or later)

'Module Code...

Option Explicit

Public gsCnn As String

Sub Main()

Dim i As Integer
Dim sSQL As String
Dim rst As ADODB.Recordset

Const sIP As String = "127.0.0.1"
Const sPort As String = "1433"
Const sDatabase As String = "Northwind"
Const sUID = "sa"
Const sPwd = ""

gsCnn = "Provider=SQLOLEDB;Network Library=DBMSSOCN;"
gsCnn = gsCnn & "Data Source=" & sIP & "," & sPort
gsCnn = gsCnn & ";Initial Catalog=" & sDatabase
gsCnn = gsCnn & ";User ID=" & sUID & ";Password=" & sPwd

sSQL = "SELECT * FROM Suppliers WHERE Country = 'USA'"

Set rst = GetSQL(sSQL)

If Not (rst Is Nothing) Then
With rst
If .BOF And .EOF Then
'no records available
Else
Do Until .EOF
Debug.Print String$(50, "-")
For i = 0 To .Fields.Count - 1
Debug.Print .Fields(i).Name & " : " &
..Fields(i).Value
Next i
.MoveNext
Loop
Debug.Print String$(50, "-")
End If
.Close
End With
Set rst = Nothing
End If

End Sub

Public Function GetSQL(ByVal sSQL As String) As ADODB.Recordset
On Error GoTo errHandler

Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset

Set cnn = New ADODB.Connection
With cnn
.Mode = ADODB.adModeReadWrite
.ConnectionTimeout = 15
.CommandTimeout = 30
.Open gsCnn
End With

Set rst = New ADODB.Recordset
With rst
Set .ActiveConnection = cnn

.CursorLocation = ADODB.adUseClient
.LockType = ADODB.adLockBatchOptimistic
.CursorType = ADODB.adOpenStatic
.Source = sSQL
'.Properties("Update Criteria").Value = ADODB.adCriteriaAllCols
.Properties("Update Criteria").Value = ADODB.adCriteriaKey 'FORCE
OVERWRITE!

.Open , , , , ADODB.adCmdText

If .State = ADODB.adStateOpen Then 'disconnect
recordset
Set .ActiveConnection = Nothing
Else
Debug.Assert False
End If
End With

Set GetSQL = rst

If Not (cnn Is Nothing) Then
If cnn.State = ADODB.adStateOpen Then cnn.Close
Set cnn = Nothing
End If

Exit Function
errHandler:
' Your choice of error handlers (I use HuntERR)
End Function

All the above does is show you how to connect, retrieve a (disconnected) ADO
recordset and dispaly the data.

Naturally you will want to add functions to Execute an SQL statement (eg to
UPDATE or DELETE a record) and to INSERT a record and return the IDENTITY
value.

You should, of course, wrap up all these functions (once you've written
them) in a Data Access class.

I could supply you with all you need, but then you wouldn't learn anything
:-)

HTH,

Jason.

P.S. Merry Christmas
Jul 17 '05 #2
Jason,

Thanks for your reaction.

Merry Christmas and a happy 2004

Tino
"Jason Keats" <jk****@melbpcDeleteThis.org.au> wrote in message
news:bs**********@possum.melbpc.org.au...
T. Wintershoven wrote:
Hello,

Although i write programs in VB for a while now, i've allways used the
datacontrol and MS-Access DBs for data storage and never (and i mean
never) used SQL.

I lookt in many sample codes, red some books but i still can't
connect to a SQL server and edit the data.

Is there someone who is willing to take a little time for me and
explane to me how it works and maby give me some sample code (very,
very basic) on how to connect to a SQL DB and store, edit and remove
records from it.

Preferably, don't use the standard VB datacontrol

I want to learn it so much, but all i do doesn't seem to work.

Many thanks in advance.

Kind regards,

Tino Wintershoven

The Netherlands
Tino,

Here's lesson one...

'References: Microsoft ActiveX Data Objects 2.1 Library (or later)

'Module Code...

Option Explicit

Public gsCnn As String

Sub Main()

Dim i As Integer
Dim sSQL As String
Dim rst As ADODB.Recordset

Const sIP As String = "127.0.0.1"
Const sPort As String = "1433"
Const sDatabase As String = "Northwind"
Const sUID = "sa"
Const sPwd = ""

gsCnn = "Provider=SQLOLEDB;Network Library=DBMSSOCN;"
gsCnn = gsCnn & "Data Source=" & sIP & "," & sPort
gsCnn = gsCnn & ";Initial Catalog=" & sDatabase
gsCnn = gsCnn & ";User ID=" & sUID & ";Password=" & sPwd

sSQL = "SELECT * FROM Suppliers WHERE Country = 'USA'"

Set rst = GetSQL(sSQL)

If Not (rst Is Nothing) Then
With rst
If .BOF And .EOF Then
'no records available
Else
Do Until .EOF
Debug.Print String$(50, "-")
For i = 0 To .Fields.Count - 1
Debug.Print .Fields(i).Name & " : " &
.Fields(i).Value
Next i
.MoveNext
Loop
Debug.Print String$(50, "-")
End If
.Close
End With
Set rst = Nothing
End If

End Sub

Public Function GetSQL(ByVal sSQL As String) As ADODB.Recordset
On Error GoTo errHandler

Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset

Set cnn = New ADODB.Connection
With cnn
.Mode = ADODB.adModeReadWrite
.ConnectionTimeout = 15
.CommandTimeout = 30
.Open gsCnn
End With

Set rst = New ADODB.Recordset
With rst
Set .ActiveConnection = cnn

.CursorLocation = ADODB.adUseClient
.LockType = ADODB.adLockBatchOptimistic
.CursorType = ADODB.adOpenStatic
.Source = sSQL
'.Properties("Update Criteria").Value = ADODB.adCriteriaAllCols
.Properties("Update Criteria").Value = ADODB.adCriteriaKey

'FORCE OVERWRITE!

.Open , , , , ADODB.adCmdText

If .State = ADODB.adStateOpen Then 'disconnect
recordset
Set .ActiveConnection = Nothing
Else
Debug.Assert False
End If
End With

Set GetSQL = rst

If Not (cnn Is Nothing) Then
If cnn.State = ADODB.adStateOpen Then cnn.Close
Set cnn = Nothing
End If

Exit Function
errHandler:
' Your choice of error handlers (I use HuntERR)
End Function

All the above does is show you how to connect, retrieve a (disconnected) ADO recordset and dispaly the data.

Naturally you will want to add functions to Execute an SQL statement (eg to UPDATE or DELETE a record) and to INSERT a record and return the IDENTITY
value.

You should, of course, wrap up all these functions (once you've written
them) in a Data Access class.

I could supply you with all you need, but then you wouldn't learn anything
:-)

HTH,

Jason.

P.S. Merry Christmas

Jul 17 '05 #3
Jason,

I read your post with interest and have filed the text away for a rainy day.

I only have access to MS Access for my database apps which at the moment tend to
be local to the PC and are merely training exercises. Can I use your approach
for remote Access databases or will I have to invest in other database
technologies?

Thanks,

Ade

Jul 17 '05 #4
Adrian Birkett wrote:
Jason,

I read your post with interest and have filed the text away for a
rainy day.

I only have access to MS Access for my database apps which at the
moment tend to be local to the PC and are merely training exercises.
Can I use your approach for remote Access databases or will I have to
invest in other database technologies?

Thanks,

Ade


If you're talking about the database being on a LAN, then all you have to do
is use a connection string like:

sDatabase = App.Path & "\data\" & "NWind.mdb"

gsCnn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & sDatabase
However, if by "remote" you mean over the internet, then look at "MS
Remote":

http://www.able-consulting.com/ADO_Conn.htm

Hope this helps.
Jul 17 '05 #5
Jason Keats wrote:
Adrian Birkett wrote:
Jason,

I read your post with interest and have filed the text away for a
rainy day.

I only have access to MS Access for my database apps which at the
moment tend to be local to the PC and are merely training exercises.
Can I use your approach for remote Access databases or will I have to
invest in other database technologies?

Thanks,

Ade


If you're talking about the database being on a LAN, then all you
have to do is use a connection string like:

sDatabase = App.Path & "\data\" & "NWind.mdb"

gsCnn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" &
sDatabase
However, if by "remote" you mean over the internet, then look at "MS
Remote":

http://www.able-consulting.com/ADO_Conn.htm

Hope this helps.


Sorry, I just noticed the dumb example I gave for a LAN connection string.
I generally use a mapped network drive to locate the database, eg
M:\MyDatabases\MyDB.mdb (which, by the way, I read from an INI file). You
could, however, connect to your data access DLL on the "server" using DCOM.

HTH
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: shank | last post by:
How do I edit this so that if there's no connect, there's a redirect? thanks <% 'REMOTE CONNECT STRING Set conn = Server.CreateObject("ADODB.Connection") conn.Provider = "sqloledb"...
5
by: mayamorning123 | last post by:
A comparison among six VSS remote tools including SourceOffSite , SourceAnyWhere, VSS Connect, SourceXT, VSS Remoting, VSS.NET To view the full article, please visit...
5
by: Mike L | last post by:
I'm able to connect to my stored procedure in my local database but not able to connect to my stored procedure in the remote database. I use several different UserID and Password that all should...
5
by: Daniel Bass | last post by:
I setup a asp.net project running on http://localhost/ which connects to a database on another server running sqlserver... I was able to connect to the database and create my application no...
8
by: danbredy | last post by:
Hi, I'm attempting to connect to an Oracle database using SQL Server 2005 Express (OS is Windows XP Professional) and having absolutely no luck. Here is the information SQL Plus gives me about...
7
by: D. Patrick | last post by:
I need to connect to an Oracle database. I've never done it before. I see that with framework 1.1 you had to download ODP.NET from Oracle's site in order for the framework classes to even work....
14
by: Marcus | last post by:
I have a function that simply returns TRUE if it can connect to a particular Sql Server 2005 express, or FALSE if it cannot. I am getting some strange error codes returned when the computer that...
4
by: valeberry | last post by:
//Index.php <html><head><title>Mailing List Administration</title></head><body> <br> <center><H1>Mailing List Administration</H1></center> Send an email to a mailing list: <form method=post...
1
by: Daniel Loose | last post by:
Hello, I try to get an asp site to work on my local machine. Using ASP Studio 2005 and mssql server 2005 eval on win xp, and ms management studio. I'm new to mssql and asp and just want to get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.