Sie sind auf Seite 1von 8

Cara 1.

Private Sub Form_Load()

'First, Project->References->Micro
' soft ActiveX Data Objects
'Now, declare the connection
Dim adoConn As New adodb.connection
'Declare the recordset
Dim adoRS As New adodb.Recordset
'Declare the querey
Dim sqlString As String
'Set the connection string:
'Driver tells it were using SQL Server
'Server says what it is named (click the
' properties of your server through Enterp
' rise Manager. If you don't have E.M. you
' must reinstall)
'Database is the database within the SQL
' Server we want
'Also tell it our login/password (which
' you can setup by adding a user to your d
' atabase, there is a button that says add
' user)
adoConn.ConnectionString = "Driver={SQL Server}; " & _
"Server=MYSQLSERVER; " & _
"Database=testdatabase; " & _
"UID=admin; " & _
"PWD=test"
'Set the querey
sqlString = "SELECT * FROM personal"
'Open the connection
adoConn.Open
'Execute the querey:
'Tell it what we want
'Tell it where to get it
'Allow the user to fully navigate the re
' cordset
'Tell it that were going to lock the rec
' ords right after we edit them
'Tell the server that the command is in
' text format
adoRS.Open sqlString, _
adoConn, _
adOpenKeyset, _
adLockPessimistic, _
adCmdText
'Loop through our recordset

While Not adoRS.EOF


lstNames.AddItem Trim(adoRS("id")) & ". " & _
Trim(adoRS("fname")) & " " & Trim(adoRS("lname"))
'Get the next record
adoRS.MoveNext
Wend
'Close the recordset
adoRS.Close
'Close the connection
adoConn.Close
'Set the objects to nothing
Set adoRS = Nothing
Set adoConn = Nothing
End Sub

Cara 2.

'**************************************
' Name: ConnectODBC
' Description:Makes a connection to the
' SQL server
' By: A Mustafa
'
' Inputs:You will need to set up ODBC fo
' r your server.
'
' Assumes:ODBC,DSN setup
'
' Side Effects:N/A
'
'This code is copyrighted and has
' limited warranties.Please see http://w
' ww.Planet-Source-Code.com/xq/ASP/txtCode
' Id.1123/lngWId.1/qx/vb/scripts/ShowCode.
' htm
'for details.
'**************************************

Public Sub CONNECTODBC()

Dim dbname As String


Dim dataset As String
Dim User3 As String
Dim pass As String
Dim db As Database
Dim X As String
User3 = "USERID"
pass = "PSWD"
dataset = "YOUR DSN NAME"
dbname = "DATABASE NAME"
X = "ODBC;database=" & dbname & ";DSN=" & dataset & ";UID=" & User3
& ";PWD=" & pass
Set db = OpenDatabase("", False, False, X)
db.Close
End Sub
Contoh ListBox Connecion

The purpose of this example is to connect/display database records (using ADO) with a simple listbox.
This is a work around to using bound controls. You will find your apps will run faster.

Windows API/Global Declarations:


Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!

'**************************************
'Windows API/Global Declarations for :Cr
' eating a simple listbox that will have t
' he same features as a bound datalist box
'
'**************************************
None.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled
format for langauges that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original
author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's
description.

'**************************************
' Name: Creating a simple listbox that w
' ill have the same features as a bound da
' talist box
' Description:The purpose of this exampl
' e is to connect/display database records
' (using ADO) with a simple listbox. This
' is a work around to using bound controls
' . You will find your apps will run faste
' r.
' By: Rob deCarle
'
' Inputs:None.
'
' Returns:None.
'
' Assumes:You will need to draw a listbo
' x on a form. You will need to create an
' Access database and an ODBC DataSource.
'
' Side Effects:None.
'
'This code is copyrighted and has
' limited warranties.Please see http://w
Cara lain :

'**************************************
' Name: dbFunctions
' Description:Connect to SQL databases a
' nd move through the data with few lines
' of code.
' By: Jerrame Hertz
'
'This code is copyrighted and has
' limited warranties.Please see http://w
' ww.Planet-Source-Code.com/xq/ASP/txtCode
' Id.22531/lngWId.1/qx/vb/scripts/ShowCode
' .htm
'for details.
'**************************************

' Uses System DSN as the ODBC Conection


' to SQL
Option Explicit
Public adoPrimaryRS As Recordset
Public mbChangedByCode As Boolean
Public mvBookMark As Variant
Public mbEditFlag As Boolean
Public mbAddNewFlag As Boolean
Public mbDataChanged As Boolean
Public db As ADODB.Connection
Public dbFields As Integer

Function ConnectToDB(ProviderType As String, DataSourceName As String, _

UserID As String, Password As String, _


dbName As String, strSQL As String)
Set db = New Connection
db.CursorLocation = adUseClient
db.Open "PROVIDER=" & ProviderType & ";dsn=" & DataSourceName & _
";uid=" & UserID & ";pwd=" & Password & ";database=" & dbName & ";"
Set adoPrimaryRS = New Recordset
adoPrimaryRS.Open strSQL, db, adOpenStatic, adLockOptimistic
mbDataChanged = False
dbFields = adoPrimaryRS.Fields.Count - 1
End Function

Function NavigateRecords(NavType As String)

Select Case NavType


Case "FirstRecord" ' Move To the first record
If Not adoPrimaryRS.BOF Then
adoPrimaryRS.MoveFirst
End If

Case "PrevRecord" ' Move To the prev record

If adoPrimaryRS.AbsolutePosition > 1 Then


adoPrimaryRS.MovePrevious
End If

Case "NextRecord" ' Move To the next record

If adoPrimaryRS.AbsolutePosition < adoPrimaryRS.RecordCount Then


adoPrimaryRS.MoveNext
End If

Case "LastRecord" ' Move To the last record

If Not adoPrimaryRS.EOF Then


adoPrimaryRS.MoveLast
End If

End Select

End Function

Das könnte Ihnen auch gefallen