Solved

accessing sql from vb code

Posted on 2004-10-13
4
160 Views
Last Modified: 2012-05-05
I am trying to access tables in a sql database using vb6 and adodb and recordset
I have only ever done this from vb to access. I am not using any forms
to make it simple lets call the data base test and the table person. in access I think I did

Dim rs As ADODB.Recordset
        Dim cn As ADODB.Connection
        Dim com As ADODB.Command
   
        'create the connection to the database.
        Set cn = New ADODB.Connection
       
        cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\shayne\Desktop\firstagain\FirstResponse.mdb;"
        cn.Open
       
        ' Get the record set.
        Set rs = New ADODB.Recordset
        rs.open "Select * from person"

Ok second half of hte question is how to I update the database with code.


Thanks in advance, loss newbie programmer




0
Comment
Question by:shayne23d
[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
4 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12304818
first off, you need a Connection String approporiate to SQL Server, not Access.  With SQL Server, you cannot use the JET engine, you wille using the SQL Server driver.

check out:

http://www.able-consulting.com/MDAC/ADO/Connection/OLEDB_Providers.htm

for the SQL Server connection strings.

AW
0
 
LVL 19

Accepted Solution

by:
Shauli earned 300 total points
ID: 12304902
To connect to SQL server use the below code. Replace databaname and sqlservername with the appropriate names.

Option Explicit
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

Private Sub Form_Load()
    Set cn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    cn.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=databasename;Data Source=sqlservername"
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
cn.Close
End Sub

To create queries using the recordset, use the same method as with access, however, using sql server open more options such as stored procedures and events. But this for another question :)

S
0
 
LVL 1

Expert Comment

by:steve92122
ID: 12306474
you can look at www.connectionstrings.com for loads of connection strings. alternatively you could set up a DSN in the ODBC and use that as your connection string: "DSN=your_dsn_name;USER=user_name;PWD=password;"


to update the database simply create your SQL string and use the database connection to execute the query.

strSql = "UPDATE persons SET Age=100 WHERE name='Bill'"

cn.Execute(strSql)

Hope this helps,
Steve
0
 
LVL 4

Expert Comment

by:AjithJose
ID: 12310739
You can use properties of Connection object instead of connection strings, it is more simple


      Dim rs As ADODB.Recordset
        Dim cn As ADODB.Connection
        Dim com As ADODB.Command
   
        'create the connection to the database.
        Set cn = New ADODB.Connection
     
With cn

      .Provider = sProvider
        .CursorLocation = adUseClient
        .Properties("Data source") = DatabaseServerName
        .Properties("User ID") = DatabaseUserName
        .Properties("Password") = DatabasePassword
        .Properties("Initial Catalog") = Database Name

        .Open
end with

'Then use the recordsets same as that of access.
       
        ' Get the record set.
        Set rs = New ADODB.Recordset
        rs.open "Select * from person"


0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…

734 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