Solved

accessing sql from vb code

Posted on 2004-10-13
4
155 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
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

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…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

770 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