SQL query run on VB 6.0

Asked By 7060 points N/A Posted on -
qa-featured

I have an Access database and I want to connect it to the VB 6.0 application and then run query to edit, alter , create, select records. How to do this.

SHARE
Best Answer by Allen Kenneth
Answered By 0 points N/A #83158

SQL query run on VB 6.0

qa-featured

 

You are up with a simple set of connecting databases and run SQL queries..
You can run queries using ADCDC, ADODB, ODBC etc, connections.
I shall narrate the use with ADODB connections.
Private Sub cmdXXX_click()
dim con as new ADODB.connection
dim rs as new ADODB.recordset
con.connectionstring = (“ <specify connection string>”)
con.open
con.begintrans
set rs = con.execute(“<SQL Query>”)
con.committrans
End Sub
 
The connection string is a set of the database location, username, password, locks and other parameters separated by colons.
You need to add the references by clicking Project-> references. Check the Microsoft active X 2.1 library and click OK.
 
All ADODB controls get activated on the references feature. 
Thanks.
 
Best Answer
Best Answer
Answered By 20 points N/A #83160

SQL query run on VB 6.0

qa-featured

Hello,

Below mentioned is a method that can be used to connect to an Access database using ADOdb. Follow this initial steps before working on the code.
 
First, in the Project menu, choose 'References' and check the 'Microsoft ActiveX Data Objects 2.x Library'. Also select 'Microsoft ActiveX Data Objects 2.x Recordset Library
2.6 works fine.
 
Also choose the installed version of 'Microsoft Data Access Components' 
Now work on the code:
 

Sub Get_Accountno()
    Dim Conn as ADODB.Connection 
    Dim Rs as ADODB.Recordset     
    Dim strQuery As String       
 
Set Conn = New ADODB.Connection
  With Conn
  .CursorLocation = adUseClient
  .ConnectionString= "Provider=Microsoft.Jet.OLEDB.4.0;" & _
           "Data Source=C:MyPathmyDatabase.mdb"
  .Open
  End With
   
strQuery = "SELECT Accountno FROM tblTable " & _
"WHERE  Username = 'BillK'"
   
Set Rs = New ADODB.Recordset
    With Rs
    Set .ActiveConnection = Conn
        .CursorType = adOpenStatic
        .Source = strQuery
        .Open
    End With
    
Set txtAccountno.DataSource = Rs
    With txtAccountno
        .DataField = "Accountno"
        .Refresh
    End With
    
   Rs.Close
   Set Rs = Nothing
   Conn.Close
   Set Conn = Nothing
End Sub



Hope this would be helpful. Regards!

Related Questions