Solved

accessing sql from vb code

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

863 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

25 Experts available now in Live!

Get 1:1 Help Now