Anda di halaman 1dari 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
' ww.Planet-Source-Code.com/xq/ASP/txtCode
' Id.11604/lngWId.1/qx/vb/scripts/ShowCode
' .htm
'for details.
'**************************************

'***************************************
' *******
'*Put the following code in any function
' .
'*This code opens database connection/re
' cordset.
'*I have connected via datasource
'***************************************
' *******
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
cn.ConnectionString = "Provider=MSDASQL.1; _
Persist Security Info=False; _
Data Source=DSG_Input"
cn.Open
'***************************************
' **
'*SQL Statement to extract all customers
' *
'*from the database
'***************************************
' **
sql = "Select First_Name, Cust_ID from Customer _ Order by
First_Name"
Set rs = cn.Execute(sql)
'***************************************
' **
'**Populates the listbox**
'***************************************
' **

With List1

Do While Not rs.EOF


.AddItem rs("First_Last")
rs.MoveNext
Loop

End With

'***************************************
' *******
'*You now have a listbox containing the
' records
'*from your database
'***************************************
' *******
'***************************************
' *******
'*You will create an array that is dynam
' ic to
'*your recordset. This will keep track o
' f
'*the primary key as a boundColumn would
' in a
'*datalist box. This is for the purpose
'
'*of relational databases.
'*You will create the array the same siz
' e as the
'*listIndex count (number of records in
'
'*listbox).
'***************************************
' *******
rs.movefirst
ReDim array1(List1.ListCount) As String
'***************************************
' ******
'*This will now populate the array which
' is a
'*mirror image as the listbox, but with
' the
'*primary key.
'***************************************
' ******

For i = 0 To List1.ListCount - 1
array1(i) = rs("Cust_ID")
rs.MoveNext
Next i

'***************************************
' *******
'*We have now completed the listbox. You
' can
'*use this listbox the same way as you w
' ould a
'*datalist box. The following code will
' explain
'*how.
'***************************************
' ********
'***************************************
' *********
'*To access the primary key relating to
' each
'*record in the list, put the following
' code in
'*the listbox "Click()" event. This expl
' ains how
'*to access the primary key stored in th
' e array.
'***************************************
' *********
'***************************************
' *******
'*list1.listIndex explains with record i
' n the
'*list was clicked on. You use this to f
' ind
'*where in the array the primary key is
' stored.
'***************************************
' *******

Private Sub List1_Click()

Dim Primary_1 as String


Primary_1 = array1(list1.listIndex)
MsgBox Primary_1
End Sub

'***************************************
' ********
'*Conclusion*
'*Although this isn't as convenient as s
' etting up
'*a bound datalist control, you will fin
' d it
'*will speed up things when using a larg
' e
'*database file.
'***************************************
' *********

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