Solved

accessing sql from vb code

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

707 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

18 Experts available now in Live!

Get 1:1 Help Now