'**************************************
' Name: an Example of how to use RDO and
' SQL
' Description:This code will show you ho
' w to use RDO and SQL. It is very simple
' to understand. You will need to learn SQ
' L statements your self, this only shows
' you how to use SQL with RDO. This can be
' implemented into a platform, or server s
' ide application.
' By:
'
' Inputs:refrence to Microsoft Remote Da
' ta Object 2.0
refrence To ODBC Driver & data source name functions
'
'This code is copyrighted and has
' limited warranties.Please see http://w
' ww.Planet-Source-Code.com/xq/ASP/txtCode
' Id.9085/lngWId.1/qx/vb/scripts/ShowCode.
' htm
'for details.
'**************************************
Dim EN As rdoEnvironment
Dim CN As rdoConnection
Dim CL As rdoColumn
Public mySQL As String
Public Sub RegisterDataSource()
'What this sub does is registers a datas
' ource in the
'ODBC object in Control Panel. This is s
' o the RDO can
'Talk to the SQL server.
On Error Goto Handle
Dim strAttribs As String
Dim x
'This will build a string containing the
' attributes
'Description - Just a general desc. of t
' he server
'OemToAnsi - Just keep it on NO
'Server - The name of the server your co
' nnecting to
'Network - Type of connection. DBMSSOCN
' = TCP/IP. The only other one
' you should use would be Named Pipes, b
' ut only if your on a Lan
'Database - Name of the database to conn
' ect to
strAttribs = "Description=" _
& "Description of server" _
& vbCrLf & "OemToAnsi=No" _
& vbCrLf & "SERVER=Server_Name" _
& vbCrLf & "Network=DBMSSOCN" _
& vbCrLf & "Database=database_name" _
& vbCrLf & "Address=255.255.255.255"
' Create new registered DSN.Leave this
rdoEngine.rdoRegisterDataSource "Table_name", "SQL Server", True, strAttribs
'only thing you should change in this wo
' uld be the Table_Name, and Read Only
Exit Sub
Handle:
MsgBox Err.Number & vbCrLf & Err.Description
End Sub
Private Sub Form_Load()
'We will set up everything we need here
Dim max As Long
'Set the environment for the rdo engine
Set EN = rdoEngine.rdoEnvironments(0)
'Define the cursor driver
EN.CursorDriver = rdUseOdbc
'Open a connection to the SQL database
Set CN = EN.OpenConnection("Table_Name", dbDriverNoPrompt, False, "driver=SQLServer; database=Database_Name; uid=User_ID; pwd=PASSWORD;")
'Set the SQL command - For now, we will
' select all of the fields
'from the record if the UserID field is
' equal to 'Dustin'
mySQL = "SELECT * FROM Users WHERE UserID='Dustin'"
'Users = The table name
'This will query the database with the S
' QL command you gave above.
Set RS = CN.OpenResultset(mySQL, rdOpenKeyset, rdConcurReadOnly, rdAsyncEnable + rdExecDirect)
'this will get the total # of records it
' found.
max = CInt(CN.LastQueryResults.RowCount)
'The results are stored in the CL variab
' le. We will have to set them
Set CL = RS.rdoColumns(0)
text1.Text = CL.Value
Set CL = RS.rdoColumns(1)
text2.Text = CL.Value
'...
'easy huh?
CN.Close 'Close connection With DB
EN.Close 'Close the environment
Exit Sub 'gotta Do this so it wont go streight through and Error out
'if there was no err.
Err:
If Err.Number = 40006 Then 'this will popup if nothing was found
MsgBox "No records Found"
Else
MsgBox Err.Description
End If
End Sub
|