Wrox Programmer Forums
|
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 28th, 2006, 08:32 AM
Authorized User
 
Join Date: Nov 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Connecting to SQL database

Hi, i am having problems connecting to a sql database, using VB to connect to dtabase, can anyone help me.
The code i have is;
Option Explicit
Dim conn As New ADODB.Connection 'This declares the name of the connection
Dim rs As New ADODB.Recordset 'This declares the name of the recordset
Dim sSQL As String 'Variable used to open the recordset
Private Sub Command1_Click()
Dim dbtest As String 'Connection string variable
Dim dbstate As Long
dbtest = "Provider=sqloledb;DRIVER={SQLServer};SERVER=serve rname;DATABASE=database name;UID=******;PASSWORD=*******" 'Connection String"
conn.Open dbtest
dbstate = conn.State
sSQL = "Select * from stafftb"
rs.Open sSQL, conn 'Open the records Staff_Name
While Not (rs.EOF) 'Until the end of the recordset is reached
MsgBox rs(0) 'displays the first field of the current record
rs.MoveNext
Wend
rs.Close
conn.Close
Set rs = Nothing
End Sub
Thanks
__________________
arshad Mehmood
 
Old November 28th, 2006, 02:40 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Have mercy on us, will you? Use some indentation!
Plus, if you click the button with the “#” on it, a beginning and ending tag will be added to the editing window at the location of the cursor, which will cause all text between the tags to be non-proportional spacing, and literal. (Very helpful.)

You know what your code is intended to do, we don't. We need to read it to figure out what's what, and if that's too eye-straining, many people will pass on even trying.

Let's see...
Code:
Option Explicit

Dim conn As New ADODB.Connection ' This declares the name of the connection
Dim rs As New ADODB.Recordset    ' This declares the name of the recordset
Dim sSQL As String               ' Variable used to open the recordset

Private Sub Command1_Click()

    Dim dbtest  As String        ' Connection string variable
    Dim dbstate As Long

    dbtest = "Provider=sqloledb;" & _
             "DRIVER={SQLServer};" & _
             "SERVER=servername;" & _
             "DATABASE=database name;" & _
             "UID=******;" & _
             "PASSWORD=*******"   ' Connection String
    conn.Open dbtest
    dbstate = conn.State

    sSQL = "SELECT * FROM stafftb"
    rs.Open sSQL, conn ' Open the records Staff_Name

    While Not (rs.EOF) ' Until the end of the recordset is reached
        MsgBox rs(0)   ' Displays the first field of the current record
        rs.MoveNext
    Wend

    rs.Close
    conn.Close
    Set rs = Nothing

End Sub
There... That's better.

Well, you say that you are having problems... Could you tell what the specific problem that you’re having is? Are you getting an error or just failure to give the anticipated results? If an error, what is the text of the error?
 
Old November 28th, 2006, 04:41 PM
Authorized User
 
Join Date: Nov 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,sorry about that, was in a rush.
Yes i get a error message saying that the connection string has an invalid attribute. The error points to the next line of code.

   conn.Open dbtest

Thanks for the tip.

arshad Mehmood
 
Old November 29th, 2006, 04:43 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

I work with Oracle, so I don’t know the proper strings and arguments for SQl Server.
Do you have other code that successfully opens a connection to SQL Server?
 
Old November 30th, 2006, 05:44 AM
Authorized User
 
Join Date: Nov 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry no.

arshad Mehmood
 
Old November 30th, 2006, 11:28 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It looks like you are trying to use both a Driver and a Provider. You need to use only one.

Instead of this:
Code:
dbtest = "Provider=sqloledb;DRIVER={SQLServer};SERVER=servername;DATABASE=database name;UID=******;PASSWORD=*******" 'Connection String"
Try this:
Code:
dbtest = "Provider=sqloledb;Data Source=servername;Initial Catalog=databasename;Uid=******;Pwd=*******;"
Of course, you will have to use your actual Data Source, Catalog, Uid, and Pwd.

Woody Z http://www.learntoprogramnow.com
 
Old December 1st, 2006, 05:37 AM
Authorized User
 
Join Date: Nov 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the help but i get an error message saying that SQL server does not exist or access denied.
Can any one help me.

Once a changed the Data Source which is a web address i get another error message that goes;
[DBNETLIB]ConnectionOpen(ParseConnectParams()).] Invalid Connection.

Arshad Mehmood
 
Old December 1st, 2006, 11:54 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by ashes0000
 Thanks for the help but i get an error message saying that SQL server does not exist or access denied.
Can any one help me.

Once a changed the Data Source which is a web address i get another error message that goes;
[DBNETLIB]ConnectionOpen(ParseConnectParams()).] Invalid Connection.

Arshad Mehmood
You seem to be describing two different issues.

The frist error message you are getting is very straight forward. Either the Sql Server does not exist or it is denying you access. Please show your actual code so we can see if you have any common typo errors. Are you actually setting the Data Source to a valid available Sql Server? Do you have the Pwd and Uid correct? This sort of thing usually happens when some small thing is incorrect.

Woody Z http://www.learntoprogramnow.com
 
Old December 10th, 2006, 05:18 PM
Authorized User
 
Join Date: Nov 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Its alright got it working.
Thanks for the help.

arshad Mehmood
 
Old April 19th, 2007, 06:11 AM
Registered User
 
Join Date: Apr 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Can you please post the updated code that worked? Would be great if you do as we're getting the exact same error and no clues what to do.

Thanks.






Similar Threads
Thread Thread Starter Forum Replies Last Post
connecting to a sql 2005 express database twisted f8 Visual Basic 2005 Basics 1 September 13th, 2007 09:49 PM
Connecting to SQL database misskaos Classic ASP Databases 8 October 23rd, 2006 05:57 PM
Connecting to SQL 2000 Database - New at this Tee88 Classic ASP Databases 14 October 2nd, 2004 04:23 AM
Connecting to sql database w/ asp Calibus SQL Server ASP 7 July 8th, 2004 08:37 AM
Connecting to SQL Database rwalker ASP.NET 1.x and 2.0 Application Design 3 December 17th, 2003 02:19 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.